-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenumParser.hpp
More file actions
191 lines (176 loc) · 7.57 KB
/
enumParser.hpp
File metadata and controls
191 lines (176 loc) · 7.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Copyright (c) 2026 Chair of Robotics (Computer Science XVII) @ Julius–Maximilians–University
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
#ifndef ENUMPARSER_HPP
#define ENUMPARSER_HPP
#include "connect/types.hpp"
#include <regex>
#include <rclcpp/rclcpp.hpp>
#define RMW_TIME_REGEX R"(\{\s*(-?\d+)(?:[uU]*[lL]*)?\s*,\s*(\d+)(?:[uU]*[lL]*)?\s*\})"
class EnumParser {
public:
/**
* Parses a compressor of a Compression Profile into typed
*
* @param compressor string holding compressor
* @return type holding compressor
* @throws std::invalid_argument if compressor is not a known compressor
*/
static Compressor compressor(const std::string &compressor) {
if (compressor == "NONE") {
return Compressor::NONE;
} else if (compressor == "LZ4_DEFAULT") {
return Compressor::LZ4_DEFAULT;
} else if (compressor == "LZ4_HC") {
return Compressor::LZ4_HC;
} else if (compressor == "ZLIB") {
return Compressor::ZLIB;
} else {
throw std::invalid_argument("Not a compressor: \"" + compressor + "\"");
}
}
/**
* Parses the history policy of a QoS definition into typed
*
* @param policy string holding history policy
* @return type holding history policy
* @throws std::invalid_argument if policy is not a known history policy
*/
static rmw_qos_history_policy_t historyPolicy(const std::string &policy) {
if (policy == "RMW_QOS_POLICY_HISTORY_SYSTEM_DEFAULT") {
return RMW_QOS_POLICY_HISTORY_SYSTEM_DEFAULT;
} else if (policy == "RMW_QOS_POLICY_HISTORY_KEEP_LAST") {
return RMW_QOS_POLICY_HISTORY_KEEP_LAST;
} else if (policy == "RMW_QOS_POLICY_HISTORY_KEEP_ALL") {
return RMW_QOS_POLICY_HISTORY_KEEP_ALL;
} else {
throw std::invalid_argument("Not a history policy: \"" + policy + "\"");
}
}
/**
* Parses the reliability policy of a QoS definition into typed
*
* @param policy string holding reliability policy
* @return type holding reliability policy
* @throws std::invalid_argument if policy is not a known reliability policy
*/
static rmw_qos_reliability_policy_t reliabilityPolicy(const std::string &policy) {
if (policy == "RMW_QOS_POLICY_RELIABILITY_SYSTEM_DEFAULT") {
return RMW_QOS_POLICY_RELIABILITY_SYSTEM_DEFAULT;
} else if (policy == "RMW_QOS_POLICY_RELIABILITY_RELIABLE") {
return RMW_QOS_POLICY_RELIABILITY_RELIABLE;
} else if (policy == "RMW_QOS_POLICY_RELIABILITY_BEST_EFFORT") {
return RMW_QOS_POLICY_RELIABILITY_BEST_EFFORT;
} else {
throw std::invalid_argument("Not a reliability policy: \"" + policy + "\"");
}
}
/**
* Parses the durability policy of a QoS definition into typed
*
* @param policy string holding durability policy
* @return type holding durability policy
* @throws std::invalid_argument if policy is not a known durability policy
*/
static rmw_qos_durability_policy_t durabilityPolicy(const std::string &policy) {
if (policy == "RMW_QOS_POLICY_DURABILITY_SYSTEM_DEFAULT") {
return RMW_QOS_POLICY_DURABILITY_SYSTEM_DEFAULT;
} else if (policy == "RMW_QOS_POLICY_DURABILITY_TRANSIENT_LOCAL") {
return RMW_QOS_POLICY_DURABILITY_TRANSIENT_LOCAL;
} else if (policy == "RMW_QOS_POLICY_DURABILITY_VOLATILE") {
return RMW_QOS_POLICY_DURABILITY_VOLATILE;
} else {
throw std::invalid_argument("Not a durability policy: \"" + policy + "\"");
}
}
/**
* Parses the deadline policy of a QoS definition into typed
*
* @param policy string holding deadline policy
* @return type holding deadline policy
* @throws std::invalid_argument if policy is not a known deadline policy
*/
static rmw_time_t deadlinePolicy(const std::string &policy) {
if (policy == "RMW_QOS_DEADLINE_DEFAULT") {
return RMW_QOS_DEADLINE_DEFAULT;
} else if (policy == "RMW_QOS_DEADLINE_BEST_AVAILABLE") {
return RMW_QOS_DEADLINE_BEST_AVAILABLE;
} else {
return EnumParser::parseRmwTime(policy, "deadline");
}
}
/**
* Parses the lifespan policy of a QoS definition into typed
*
* @param policy string holding lifespan policy
* @return type holding lifespan policy
* @throws std::invalid_argument if policy is not a known lifespan policy
*/
static rmw_time_t lifespanPolicy(const std::string &policy) {
if (policy == "RMW_QOS_LIFESPAN_DEFAULT") {
return RMW_QOS_LIFESPAN_DEFAULT;
} else {
return EnumParser::parseRmwTime(policy, "lifespan");
}
}
/**
* Parses the liveliness policy of a QoS definition into typed
*
* @param policy string holding liveliness policy
* @return type holding liveliness policy
* @throws std::invalid_argument if policy is not a known liveliness policy
*/
static rmw_qos_liveliness_policy_t livelinessPolicy(const std::string &policy) {
if (policy == "RMW_QOS_POLICY_LIVELINESS_SYSTEM_DEFAULT") {
return RMW_QOS_POLICY_LIVELINESS_SYSTEM_DEFAULT;
} else if (policy == "RMW_QOS_POLICY_LIVELINESS_AUTOMATIC") {
return RMW_QOS_POLICY_LIVELINESS_AUTOMATIC;
} else if (policy == "RMW_QOS_POLICY_LIVELINESS_MANUAL_BY_TOPIC") {
return RMW_QOS_POLICY_LIVELINESS_MANUAL_BY_TOPIC;
} else if (policy == "RMW_QOS_POLICY_LIVELINESS_BEST_AVAILABLE") {
return RMW_QOS_POLICY_LIVELINESS_BEST_AVAILABLE;
} else {
throw std::invalid_argument("Not a liveliness policy: \"" + policy + "\"");
}
}
/**
* Parses the livelinessLeaseDuration policy of a QoS definition into typed
*
* @param policy string holding livelinessLeaseDuration policy
* @return type holding livelinessLeaseDuration policy
* @throws std::invalid_argument if policy is not a known livelinessLeaseDuration policy
*/
static rmw_time_t livelinessLeaseDurationPolicy(const std::string &policy) {
if (policy == "RMW_QOS_LIVELINESS_LEASE_DURATION_DEFAULT") {
return RMW_QOS_LIVELINESS_LEASE_DURATION_DEFAULT;
} else if (policy == "RMW_QOS_LIVELINESS_LEASE_DURATION_BEST_AVAILABLE") {
return RMW_QOS_LIVELINESS_LEASE_DURATION_BEST_AVAILABLE;
} else {
return EnumParser::parseRmwTime(policy, "livelinessLeaseDuration");
}
}
private:
/**
* Parses a value holding a inline rmw_time_t definition into a rmw_time t
*
* @param value inline rmw_time_t definition
* @param policy name of policy to parse rmw_time_t for
* @return parsed rmw_time_t
* @throws std::invalid_argument if value does not hold a inline rmw_time_t definition
*/
static rmw_time_t parseRmwTime(const std::string &value, const std::string &policy) {
const std::regex timeRegex(RMW_TIME_REGEX);
std::smatch match;
if (std::regex_match(value, match, timeRegex)) {
rmw_time_t parsedTime;
parsedTime.sec = std::stoll(match[1].str());
parsedTime.nsec = std::stoull(match[2].str());
return parsedTime;
} else {
throw std::invalid_argument("Not a " + policy + " policy: \"" + value + "\"");
}
}
};
#endif //ENUMPARSER_HPP