17 #include <unordered_map>
18 #include <unordered_set>
21 #include <dynamic_reconfigure/BoolParameter.h>
22 #include <dynamic_reconfigure/Config.h>
23 #include <dynamic_reconfigure/DoubleParameter.h>
24 #include <dynamic_reconfigure/IntParameter.h>
25 #include <dynamic_reconfigure/StrParameter.h>
31 #if !XMLRPCPP_HAS_PRINTTO
36 inline void PrintTo(
const XmlRpcValue& value, ::std::ostream* os)
56 ::std::list<::std::string>* errors =
nullptr)
63 inline bool convert(const ::XmlRpc::XmlRpcValue& x,
bool& v,
bool =
false,
64 ::std::list<::std::string>* errors =
nullptr)
74 const auto i =
static_cast<int>(x);
77 v =
static_cast<bool>(i);
80 else if (errors !=
nullptr)
81 errors->push_back(::
cras::format(
"Cannot convert int value %i to boolean.", i));
84 if (errors !=
nullptr)
89 inline bool convert(const ::XmlRpc::XmlRpcValue& x,
int& v,
bool =
false,
90 ::std::list<::std::string>* errors =
nullptr)
98 if (errors !=
nullptr)
104 #define DEFINE_INTEGRAL_CONVERT(resultType, xmlType, minBound, maxBound) \
106 inline bool convert(const ::XmlRpc::XmlRpcValue& x, resultType& v, bool skipNonConvertible = false, \
107 ::std::list<::std::string>* errors = nullptr) \
110 if (!convert(x, i, skipNonConvertible, errors)) \
112 if (i < (minBound) || i > (maxBound)) { \
113 if (errors != nullptr) \
114 errors->push_back(::cras::format("Value %s is out of bounds <%s, %s>.", \
115 ::cras::to_string(i).c_str(), ::cras::to_string(minBound).c_str(), ::cras::to_string(maxBound).c_str())); \
118 v = static_cast<resultType>(i); \
135 inline
bool convert(const ::
XmlRpc::XmlRpcValue& x,
double& v,
bool = false,
136 ::
std::list<::
std::
string>* errors =
nullptr)
146 v =
static_cast<double>(
static_cast<int>(x));
150 if (errors !=
nullptr)
156 #define DEFINE_DOUBLE_CONVERT(resultType, xmlType, minBound, maxBound) \
158 inline bool convert(const ::XmlRpc::XmlRpcValue& x, resultType& v, bool skipNonConvertible = false, \
159 ::std::list<::std::string>* errors = nullptr) \
162 if (!convert(x, i, skipNonConvertible, errors)) \
164 if (::std::isnan(i)) { v = ::std::numeric_limits<resultType>::quiet_NaN(); return true; } \
165 if (::std::isinf(i) && i > 0) { v = ::std::numeric_limits<resultType>::infinity(); return true; } \
166 if (::std::isinf(i) && i < 0) { v = -::std::numeric_limits<resultType>::infinity(); return true; } \
167 if (i < (minBound) || i > (maxBound)) { \
168 if (errors != nullptr) \
169 errors->push_back(::cras::format("Value %s is out of bounds <%s, %s>.", \
170 ::cras::to_string(i).c_str(), ::cras::to_string(minBound).c_str(), ::cras::to_string(maxBound).c_str())); \
173 v = static_cast<resultType>(i); \
180 inline bool convert(const ::XmlRpc::XmlRpcValue& x, std::string& v,
bool =
false,
182 ::std::list<::std::string>* errors =
nullptr)
186 v =
static_cast<::std::string
>(x);
190 if (errors !=
nullptr)
199 inline bool convert(const ::XmlRpc::XmlRpcValue& x, ::std::map<::std::string, T>& v,
200 bool skipNonConvertible =
false, ::std::list<::std::string>* errors =
nullptr);
204 inline bool convert(const ::XmlRpc::XmlRpcValue& x, ::std::unordered_map<::std::string, T>& v,
205 bool skipNonConvertible =
false, ::std::list<::std::string>* errors =
nullptr);
209 inline bool convert(const ::XmlRpc::XmlRpcValue& x, ::std::vector<T>& v,
210 bool skipNonConvertible =
false, ::std::list<::std::string>* errors =
nullptr);
214 inline bool convert(const ::XmlRpc::XmlRpcValue& x, ::std::list<T>& v,
215 bool skipNonConvertible =
false, ::std::list<::std::string>* errors =
nullptr);
219 inline bool convert(const ::XmlRpc::XmlRpcValue& x, ::std::set<T>& v,
220 bool skipNonConvertible =
false, ::std::list<::std::string>* errors =
nullptr);
224 inline bool convert(const ::XmlRpc::XmlRpcValue& x, ::std::unordered_set<T>& v,
225 bool skipNonConvertible =
false, ::std::list<::std::string>* errors =
nullptr);
228 template<
typename T,
size_t N>
229 inline bool convert(const ::XmlRpc::XmlRpcValue& x, ::std::array<T, N>& v,
230 bool skipNonConvertible =
false, ::std::list<::std::string>* errors =
nullptr);
232 #define DEFINE_ARRAY_CONVERT(arrayType, insertFn) \
233 template<typename T> \
234 inline bool convert(const ::XmlRpc::XmlRpcValue& x, arrayType<T>& v, bool skipNonConvertible, \
235 ::std::list<::std::string>* errors) \
237 if (x.getType() != ::XmlRpc::XmlRpcValue::TypeArray) { \
238 if (errors != nullptr) \
239 errors->push_back(::cras::format("Cannot convert type %s to array.", ::cras::to_cstring(x.getType())));\
243 for (size_t i = 0; i < x.size(); ++i) \
246 if (convert(x[i], t, skipNonConvertible, errors)) \
248 else if (!skipNonConvertible) \
251 return v.size() > 0 || x.size() == 0; \
259 template<
typename T,
size_t N>
260 inline bool convert(const ::XmlRpc::XmlRpcValue& x, ::std::array<T, N>& v,
bool skipNonConvertible,
261 ::std::list<::std::string>* errors)
265 if (errors !=
nullptr)
271 if (errors !=
nullptr)
272 errors->push_back(::
cras::format(
"The array is expected to have %zu items, but %i was given.", N, x.size()));
275 for (
size_t i = 0; i < x.size(); ++i)
278 if (
convert(x[i],
t, skipNonConvertible, errors))
286 #define DEFINE_STRUCT_CONVERT(mapType) \
287 template<typename T> \
288 inline bool convert(const ::XmlRpc::XmlRpcValue& x, mapType<::std::string, T>& v, bool skipNonConvertible, \
289 ::std::list<::std::string>* errors) \
291 if (x.getType() != ::XmlRpc::XmlRpcValue::TypeStruct) { \
292 if (errors != nullptr) \
293 errors->push_back(::cras::format("Cannot convert type %s to struct.", ::cras::to_cstring(x.getType())));\
297 for (auto it = x.begin(); it != x.end(); ++it) \
300 if (convert(it->second, t, skipNonConvertible, errors)) \
302 else if (!skipNonConvertible) \
305 return v.size() > 0 || x.size() == 0; \
311 inline
bool convert(const ::
XmlRpc::XmlRpcValue& x, ::dynamic_reconfigure::Config& v,
bool skipNonConvertible,
312 ::
std::list<::
std::
string>* errors)
316 if (errors !=
nullptr)
318 "Cannot convert type %s to dynamic_reconfigure/Config.", ::
cras::to_cstring(x.getType())));
321 for (
auto it = x.begin(); it != x.end(); ++it)
323 const auto& name = it->first;
324 const auto& val = it->second;
325 switch (val.getType())
329 dynamic_reconfigure::BoolParameter p;
331 p.value =
static_cast<bool>(val);
332 v.bools.push_back(p);
337 dynamic_reconfigure::IntParameter p;
339 p.value =
static_cast<int>(val);
345 dynamic_reconfigure::DoubleParameter p;
347 p.value =
static_cast<double>(val);
348 v.doubles.push_back(p);
353 dynamic_reconfigure::StrParameter p;
355 p.value =
static_cast<std::string
>(val);
361 if (errors !=
nullptr)
362 errors->push_back(::
cras::format(
"Field %s of type %s cannot be stored in dynamic_reconfigure/Config.",
364 if (!skipNonConvertible)