-
Notifications
You must be signed in to change notification settings - Fork 20
/
parameter_validators.hpp
305 lines (279 loc) · 12.6 KB
/
parameter_validators.hpp
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#pragma once
#include <rsl/algorithm.hpp>
#include <rsl/export.hpp>
#include <rsl/static_string.hpp>
#include <rsl/static_vector.hpp>
#include <rcl_interfaces/msg/set_parameters_result.hpp>
#include <rclcpp/parameter.hpp>
#include <tl_expected/expected.hpp>
#include <fmt/ranges.h>
namespace rsl {
/** @file */
/**
* @cond DETAIL
*/
namespace detail {
template <typename T, typename Fn>
[[nodiscard]] auto size_compare(rclcpp::Parameter const& parameter, size_t const size,
std::string const& predicate_description, Fn const& predicate)
-> tl::expected<void, std::string> {
static constexpr auto format_string = "Length of parameter '{}' is '{}' but must be {} '{}'";
switch (parameter.get_type()) {
case rclcpp::ParameterType::PARAMETER_STRING:
if (auto value = parameter.get_value<std::string>(); !predicate(value.size(), size))
return tl::unexpected(fmt::format(format_string, parameter.get_name(), value.size(),
predicate_description, size));
break;
default:
if (auto value = parameter.get_value<std::vector<T>>(); !predicate(value.size(), size))
return tl::unexpected(fmt::format(format_string, parameter.get_name(), value.size(),
predicate_description, size));
}
return {};
}
template <typename T, typename Fn>
[[nodiscard]] auto compare(rclcpp::Parameter const& parameter, T const& value,
std::string const& predicate_description, Fn const& predicate)
-> tl::expected<void, std::string> {
if (auto const param_value = parameter.get_value<T>(); !predicate(param_value, value))
return tl::unexpected(fmt::format("Parameter '{}' with the value '{}' must be {} '{}'",
parameter.get_name(), param_value, predicate_description,
value));
return {};
}
} // namespace detail
/**
* @endcond
*/
/**
* @brief Is every element of rclcpp::Parameter unique?
* @pre rclcpp::Parameter must be an array type
* @tparam T Interior type of array; e.g. for parameter type double_array, T = double
* @return Help string if the parameter is invalid, otherwise void
*/
template <typename T>
[[nodiscard]] auto unique(rclcpp::Parameter const& parameter) -> tl::expected<void, std::string> {
if (is_unique(parameter.get_value<std::vector<T>>())) return {};
return tl::unexpected(
fmt::format("Parameter '{}' must only contain unique values", parameter.get_name()));
}
/**
* @brief Are the values in parameter a subset of the valid values?
* @pre rclcpp::Parameter must be an array type
* @tparam T Interior type of array; e.g. for parameter type double_array, T = double
* @return Help string if the parameter is invalid, otherwise void
*/
template <typename T>
[[nodiscard]] auto subset_of(rclcpp::Parameter const& parameter, std::vector<T> const& valid_values)
-> tl::expected<void, std::string> {
auto const& values = parameter.get_value<std::vector<T>>();
for (auto const& value : values)
if (!contains(valid_values, value))
return tl::unexpected(
fmt::format("Entry '{}' in parameter '{}' is not in the set '{{{}}}'", value,
parameter.get_name(), fmt::join(valid_values, ", ")));
return {};
}
/**
* @brief Is the array size of parameter equal to passed in size?
* @pre rclcpp::Parameter must be an array type
* @tparam T Interior type of array; e.g. for parameter type double_array, T = double
* @return Help string if the parameter is invalid, otherwise void
*/
template <typename T>
[[nodiscard]] auto fixed_size(rclcpp::Parameter const& parameter, size_t const size) {
return detail::size_compare<T>(parameter, size, "equal to", std::equal_to<>());
}
/**
* @brief Is the array size of parameter greater than passed in size?
* @pre rclcpp::Parameter must be an array type
* @tparam T Interior type of array; e.g. for parameter type double_array, T = double
* @return Help string if the parameter is invalid, otherwise void
*/
template <typename T>
[[nodiscard]] auto size_gt(rclcpp::Parameter const& parameter, size_t const size) {
return detail::size_compare<T>(parameter, size, "greater than", std::greater<>());
}
/**
* @brief Is the array size of parameter less than passed in size?
* @pre rclcpp::Parameter must be an array type
* @tparam T Interior type of array; e.g. for parameter type double_array, T = double
* @return Help string if the parameter is invalid, otherwise void
*/
template <typename T>
[[nodiscard]] auto size_lt(rclcpp::Parameter const& parameter, size_t const size) {
return detail::size_compare<T>(parameter, size, "less than", std::less<>());
}
/**
* @brief Is the size of the value passed in not zero?
* @pre rclcpp::Parameter must be an array type or a string
* @tparam T Interior type of array or std::string; e.g. for parameter type double_array, T
* = double
* @return Help string if the parameter is invalid, otherwise void
*/
template <typename T>
[[nodiscard]] auto not_empty(rclcpp::Parameter const& parameter)
-> tl::expected<void, std::string> {
switch (parameter.get_type()) {
case rclcpp::ParameterType::PARAMETER_STRING:
if (auto param_value = parameter.get_value<std::string>(); param_value.empty())
return tl::unexpected(
fmt::format("Parameter '{}' cannot be empty", parameter.get_name()));
break;
default:
if (auto param_value = parameter.get_value<std::vector<T>>(); param_value.empty())
return tl::unexpected(
fmt::format("Parameter '{}' cannot be empty", parameter.get_name()));
}
return {};
}
/**
* @brief Are all elements of parameter within the bounds (inclusive)?
* @pre rclcpp::Parameter must be an array type
* @tparam T Interior type of array; e.g. for parameter type double_array, T = double
* @return Help string if the parameter is invalid, otherwise void
*/
template <typename T>
[[nodiscard]] auto element_bounds(rclcpp::Parameter const& parameter, T const& lower,
T const& upper) -> tl::expected<void, std::string> {
auto const& param_value = parameter.get_value<std::vector<T>>();
for (auto val : param_value)
if (val < lower || val > upper)
return tl::unexpected(
fmt::format("Value '{}' in parameter '{}' must be within bounds '[{}, {}]'", val,
parameter.get_name(), lower, upper));
return {};
}
/**
* @brief Are all elements of parameter greater than lower bound?
* @pre rclcpp::Parameter must be an array type
* @tparam T Interior type of array; e.g. for parameter type double_array, T = double
* @return Help string if the parameter is invalid, otherwise void
*/
template <typename T>
[[nodiscard]] auto lower_element_bounds(rclcpp::Parameter const& parameter, T const& lower)
-> tl::expected<void, std::string> {
auto const& param_value = parameter.get_value<std::vector<T>>();
for (auto val : param_value)
if (val < lower)
return tl::unexpected(
fmt::format("Value '{}' in parameter '{}' must be above lower bound of '{}'", val,
parameter.get_name(), lower));
return {};
}
/**
* @brief Are all elements of parameter less than some upper bound?
* @pre rclcpp::Parameter must be an array type
* @tparam T Interior type of array; e.g. for parameter type double_array, T = double
* @return Help string if the parameter is invalid, otherwise void
*/
template <typename T>
[[nodiscard]] auto upper_element_bounds(rclcpp::Parameter const& parameter, T const& upper)
-> tl::expected<void, std::string> {
auto const& param_value = parameter.get_value<std::vector<T>>();
for (auto val : param_value)
if (val > upper)
return tl::unexpected(
fmt::format("Value '{}' in parameter '{}' must be below upper bound of '{}'", val,
parameter.get_name(), upper));
return {};
}
/**
* @brief Is parameter within bounds (inclusive)?
* @pre rclcpp::Parameter must be a non-array type
* @tparam T Interior type of parameter; e.g. for parameter type int, T = int64_t
* @return Help string if the parameter is invalid, otherwise void
*/
template <typename T>
[[nodiscard]] auto bounds(rclcpp::Parameter const& parameter, T const& lower, T const& upper)
-> tl::expected<void, std::string> {
auto const& param_value = parameter.get_value<T>();
if (param_value < lower || param_value > upper)
return tl::unexpected(
fmt::format("Parameter '{}' with the value '{}' must be within bounds '[{}, {}]'",
parameter.get_name(), param_value, lower, upper));
return {};
}
/**
* @brief Is parameter within some lower bound (same as gt_eq)?
* @pre rclcpp::Parameter must be a non-array type
* @tparam T Interior type of parameter; e.g. for parameter type int, T = int64_t
* @return Help string if the parameter is invalid, otherwise void
*/
template <typename T>
[[deprecated("Replace with rsl::gt_eq"), nodiscard]] auto lower_bounds(
rclcpp::Parameter const& parameter, T const& value) {
return detail::compare(parameter, value, "above lower bound of", std::greater_equal<T>());
}
/**
* @brief Is parameter within some upper bound (same as lt_eq)?
* @pre rclcpp::Parameter must be a non-array type
* @tparam T Interior type of parameter; e.g. for parameter type int, T = int64_t
* @return Help string if the parameter is invalid, otherwise void
*/
template <typename T>
[[deprecated("Replace with rsl::lt_eq"), nodiscard]] auto upper_bounds(
rclcpp::Parameter const& parameter, T const& value) {
return detail::compare(parameter, value, "below upper bound of", std::less_equal<T>());
}
/**
* @brief Is parameter less than some value?
* @pre rclcpp::Parameter must be a non-array type
* @tparam T Interior type of parameter; e.g. for parameter type int, T = int64_t
* @return Help string if the parameter is invalid, otherwise void
*/
template <typename T>
[[nodiscard]] auto lt(rclcpp::Parameter const& parameter, T const& value) {
return detail::compare(parameter, value, "less than", std::less<T>());
}
/**
* @brief Is parameter greater than some value?
* @pre rclcpp::Parameter must be a non-array type
* @tparam T Interior type of parameter; e.g. for parameter type int, T = int64_t
* @return Help string if the parameter is invalid, otherwise void
*/
template <typename T>
[[nodiscard]] auto gt(rclcpp::Parameter const& parameter, T const& value) {
return detail::compare(parameter, value, "greater than", std::greater<T>());
}
/**
* @brief Is parameter less than or equal to some value?
* @pre rclcpp::Parameter must be a non-array type
* @tparam T Interior type of parameter; e.g. for parameter type int, T = int64_t
* @return Help string if the parameter is invalid, otherwise void
*/
template <typename T>
[[nodiscard]] auto lt_eq(rclcpp::Parameter const& parameter, T const& value) {
return detail::compare(parameter, value, "less than or equal to", std::less_equal<T>());
}
/**
* @brief Is parameter greater than or equal to some value?
* @pre rclcpp::Parameter must be a non-array type
* @tparam T Interior type of parameter; e.g. for parameter type int, T = int64_t
* @return Help string if the parameter is invalid, otherwise void
*/
template <typename T>
[[nodiscard]] auto gt_eq(rclcpp::Parameter const& parameter, T const& value) {
return detail::compare(parameter, value, "greater than or equal to", std::greater_equal<T>());
}
/**
* @brief Is the parameter value one of a set of values?
* @pre rclcpp::Parameter must be a non-array type
* @tparam T Interior type of parameter; e.g. for parameter type int, T = int64_t
* @return Help string if the parameter is invalid, otherwise void
*/
template <typename T>
[[nodiscard]] auto one_of(rclcpp::Parameter const& parameter, std::vector<T> const& collection)
-> tl::expected<void, std::string> {
auto const& param_value = parameter.get_value<T>();
if (contains(collection, param_value)) return {};
return tl::unexpected(fmt::format(
"Parameter '{}' with the value '{}' is not in the set '{{{}}}'", parameter.get_name(),
param_value, fmt::format("{}", fmt::join(collection, ", "))));
}
/**
* @brief Convert the result of a validator function into a SetParametersResult msg
*/
[[nodiscard]] RSL_EXPORT auto to_parameter_result_msg(tl::expected<void, std::string> const& result)
-> rcl_interfaces::msg::SetParametersResult;
} // namespace rsl