18 #include <unordered_set>
19 #include <unordered_map>
60 template <
typename ResultType,
typename ParamServerType>
93 template<typename ResultType, typename ParamServerType = typename ::cras::DefaultParamServerType<ResultType>::type,
96 const ::cras::GetParamAdapter& param, const ::std::string& name,
97 const ::cras::optional<ResultType>& defaultValue = ResultType(),
98 const ::std::string& unit =
"",
99 const ::cras::GetParamOptions<ResultType, ParamServerType>& options = {},
100 const ::cras::LogHelper*
const logger =
nullptr)
103 ParamServerType value;
104 bool shouldThrow {
false};
105 bool useDefault {
false};
106 const bool isRequired = !defaultValue.has_value();
107 ::std::list<::std::string> errors;
111 const auto origNs = options.origNamespace.empty() ?
param.getNamespace() : options.origNamespace;
112 const auto origParamName = options.origParamName.empty() ? name : options.origParamName;
115 if (
param.getParam(name, xmlValue))
117 if (!options.toParam(xmlValue, value, !options.throwIfConvertFails, &errors))
123 "%s: Parameter %s found, but it has wrong XmlRpc type. Expected type %s, got type %s with value %s.",
124 origNs.c_str(), origParamName.c_str(),
131 ::std::list<::std::string> uniqueErrors;
132 for (
const auto& error : errors)
134 if (std::find(uniqueErrors.begin(), uniqueErrors.end(), error) == uniqueErrors.end())
135 uniqueErrors.push_back(error);
138 "%s: Parameter %s found with correct XmlRpc type %s and value %s, "
139 "but its conversion to type %s has failed due to the following errors: %s.",
140 origNs.c_str(), origParamName.c_str(),
143 ::cras::getTypeName<ParamServerType>().c_str(),
148 if (isRequired || options.throwIfConvertFails)
165 if (parts.size() == 2 && !parts[0].empty() && !parts[1].empty())
167 const auto& head = parts[0];
168 const auto& tail = parts[1];
171 auto nsParams =
param.getNamespaced(head);
172 auto nestedOptions = options;
173 nestedOptions.origNamespace = origNs;
174 nestedOptions.origParamName = origParamName;
177 catch (const ::std::runtime_error& e)
185 origNs.c_str(), origParamName.c_str());
188 info.
messageLevel = options.printDefaultAsWarn ? ::ros::console::Level::Warn : ::ros::console::Level::Info;
199 ::std::string defaultUsedMessage {};
200 if (defaultValue.has_value())
205 info.
message += defaultUsedMessage;
211 if (logger !=
nullptr && options.printMessages)
213 throw GetParamException(info);
217 ::std::unique_ptr<ResultType> resultValue;
220 resultValue = ::std::make_unique<ResultType>(defaultValue.value());
226 resultValue = ::std::make_unique<ResultType>(options.toResult(value));
229 origNs.c_str(), origParamName.c_str(), options.resultToStr(*resultValue).c_str(),
237 ::std::list<::std::string> uniqueErrors;
238 for (
const auto& error : errors)
240 if (std::find(uniqueErrors.begin(), uniqueErrors.end(), error) == uniqueErrors.end())
241 uniqueErrors.push_back(error);
243 info.
message +=
" Some parts of the value were skipped because of the following conversion errors: " +
248 catch (
const std::runtime_error& e)
251 "%s: Cannot convert value '%s' of parameter %s to requested type %s (error: %s).",
252 origNs.c_str(), options.paramToStr(value).c_str(),
253 origParamName.c_str(),
254 ::cras::getTypeName<ResultType>().c_str(),
258 if (isRequired || options.throwIfConvertFails)
264 info.
message += defaultUsedMessage;
265 resultValue = ::std::make_unique<ResultType>(defaultValue.value());
275 if (logger !=
nullptr && options.printMessages)
279 throw GetParamException(info);
281 return {*resultValue, info};
302 template<typename ResultType, typename ParamServerType = typename ::cras::DefaultParamServerType<ResultType>::type,
305 const ::cras::GetParamAdapter& param, const ::std::string &name,
306 const ResultType& defaultValue = ResultType(),
307 const ::std::string& unit =
"",
308 const ::cras::GetParamOptions<ResultType, ParamServerType>& options = {},
309 const ::cras::LogHelper*
const logger =
nullptr)
333 template<typename ResultType, typename ParamServerType = typename ::cras::DefaultParamServerType<ResultType>::type,
336 const ::cras::GetParamAdapter& param, const ::std::string &name,
337 const ::cras::optional<ResultType>& defaultValue = ResultType(),
338 const ::std::string& unit =
"",
339 const ::cras::GetParamOptions<ResultType, ParamServerType>& options = {},
340 const ::cras::LogHelper*
const logger =
nullptr)
363 template<typename ResultType, typename ParamServerType = typename ::cras::DefaultParamServerType<ResultType>::type,
366 const ::cras::GetParamAdapter& param, const ::std::string &name,
367 const ResultType& defaultValue = ResultType(),
368 const ::std::string& unit =
"",
369 const ::cras::GetParamOptions<ResultType, ParamServerType>& options = {},
370 const ::cras::LogHelper*
const logger =
nullptr)
393 const ::cras::GetParamAdapter& param, const ::std::string &name,
394 const ::cras::optional<const char *>& defaultValue, const ::std::string &unit =
"",
395 const ::cras::GetParamOptions<::std::string>& options = {}, const ::cras::LogHelper*
const logger =
nullptr)
397 ::cras::optional<::std::string> defaultStr;
398 if (defaultValue.has_value())
399 defaultStr = defaultValue.value();
418 const ::cras::GetParamAdapter& param, const ::std::string &name,
419 const char* defaultValue, const ::std::string &unit =
"",
420 const ::cras::GetParamOptions<::std::string>& options = {}, const ::cras::LogHelper*
const logger =
nullptr)
422 ::cras::optional<::std::string> defaultStr(defaultValue);
442 const ::cras::GetParamAdapter& param, const ::std::string &name,
443 const ::cras::optional<const char *>& defaultValue, const ::std::string &unit =
"",
444 const ::cras::GetParamOptions<::std::string>& options = {}, const ::cras::LogHelper*
const logger =
nullptr)
464 const ::cras::GetParamAdapter& param, const ::std::string &name,
465 const char* defaultValue, const ::std::string &unit =
"",
466 const ::cras::GetParamOptions<::std::string>& options = {}, const ::cras::LogHelper*
const logger =
nullptr)
484 #define DEFINE_CONVERTING_GET_PARAM(resultType, paramServerType, defaultUnit, convertToResultFn) \
486 struct ParamToStringFn<resultType>\
488 static ::std::string to_string(const resultType& v){ return ::cras::to_string(v); }\
492 struct DefaultToResultFn<resultType, paramServerType>\
494 static resultType toResult(const paramServerType& v){ return convertToResultFn(v); }\
498 struct DefaultParamServerType<resultType>\
500 typedef paramServerType type;\
512 #define DEFINE_CONVERTING_GET_PARAM_WITH_CONSTRUCTOR(resultType, paramServerType, defaultUnit) \
513 DEFINE_CONVERTING_GET_PARAM(resultType, paramServerType, defaultUnit, \
514 ([](const paramServerType& v) { return resultType(v); }))
525 #define DEFINE_CONVERTING_GET_PARAM_WITH_CAST(resultType, paramServerType, defaultUnit) \
526 DEFINE_CONVERTING_GET_PARAM(resultType, paramServerType, defaultUnit, \
527 ([](const paramServerType& v) { return static_cast<resultType>(v); }))
530 #if __has_include(<ros/duration.h>)
534 #if __has_include(<geometry_msgs/Vector3.h>)
538 #if __has_include(<tf2/LinearMath/Vector3.h>)
542 #if __has_include(<Eigen/Core>)