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);
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())
201 defaultUsedMessage = ::
cras::format(
" Assigning default: %s%s.",
205 info.
message += defaultUsedMessage;
211 if (logger !=
nullptr &&
options.printMessages)
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)
281 return {*resultValue, info};
302 template<typename ResultType, typename ParamServerType = typename ::cras::DefaultParamServerType<ResultType>::type,
303 ::cras::check_get_param_types<ResultType, ParamServerType>* =
nullptr>
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,
334 ::cras::check_get_param_types<ResultType, ParamServerType>* =
nullptr>
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,
364 ::cras::check_get_param_types<ResultType, ParamServerType>* =
nullptr>
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>) inline ::std::string to_string(const ::Eigen::Matrix< Scalar, Rows, Cols, Options, MaxRows, MaxCols > &value)
Type trait determining whether type T is cras::optional or not.
typename std::enable_if_t< !::cras::is_optional< ResultType >::value &&!::cras::is_c_string< ResultType >::value &&!::cras::is_c_string< ParamServerType >::value > check_get_param_types
This type is a TrueType if the combination of ResultType and ParamServerType is valid.
GetParamResult< ResultType > getParamVerbose(const ::ros::NodeHandle &node, const ::std::string &name, const ::cras::optional< ResultType > &defaultValue=ResultType(), const ::std::string &unit="", const ::cras::GetParamOptions< ResultType, ParamServerType > &options={})
Get the value of the given ROS parameter, falling back to the specified default value (if not nullopt...
Specializations of getParam() for tf2 types.
A C++11 shim for std::optional. Uses std::optional when used in C++17 mode.
Exception thrown when conversion of a parameter fails during getParam() if option throwIfConvertFails...
Utilities for working with C++ types.
Options for getParam() calls.
#define CRAS_LOG(logger, level, name,...)
Log to a given named logger at a given verbosity level, with printf-style formatting.
inline ::cras::GetParamResult<::std::string > getParamVerbose(const ::cras::GetParamAdapter ¶m, const ::std::string &name, const char *defaultValue, const ::std::string &unit="", const ::cras::GetParamOptions<::std::string > &options={}, const ::cras::LogHelper *const logger=nullptr)
Get the value of the given ROS parameter, falling back to the specified default value, and print out a ROS log message with the loaded values (if specified).
Specializations of getParam() for Eigen vectors and matrices.
bool defaultUsed
Whether the default value has been used.
Type traits for XmlRpcValue.
::std::string prependIfNonEmpty(const ::std::string &str, const ::std::string &prefix)
If str is nonempty, returns prefix + str, otherwise empty string.
Type const & getType() const
Specializations of getParam() for ROS basic types.
bool convertFailed
Whether a value conversion failed.
Utilities for working with XmlRpcValues.
::std::string message
The log message (returned even if option printMessages is false).
ResultType getParam(const ::ros::NodeHandle &node, const ::std::string &name, const ::cras::optional< ResultType > &defaultValue=ResultType(), const ::std::string &unit="", const ::cras::GetParamOptions< ResultType, ParamServerType > &options={})
Get the value of the given ROS parameter, falling back to the specified default value (if not nullopt...
::ros::console::Level messageLevel
Severity of the log message.
bool requiredMissing
Whether a required parameter was found missing or could not be read.
inline ::std::string format(const char *format, ::va_list args)
Detailed information about the executed getParam() call.
Wrapper of getParam() call results.
Type trait for dynamic-sized and constant-sized C strings.
bool contains(const ::std::string &str, char c)
Check whether str contains character c.
::cras::GetParamResultInfo info
Details about getParam() execution.
::std::vector<::std::string > split(const ::std::string &str, const ::std::string &delimiter, int maxSplits=-1)
Split the given string by the given delimiter.
An adapter that allows getting ROS parameters from various sources.
Utils for working with strings.
#define ROSCONSOLE_DEFAULT_NAME
constexpr const char * to_cstring(const ::XmlRpc::XmlRpcValue::Type &value)
Return a string representation of the XmlRpcValue type.
GetParamException(const ::cras::GetParamResultInfo &info)
Construct the exception.
Specializations of getParam() for geometry_msgs.