get_param_options.hpp
Go to the documentation of this file.
1 #pragma once
2 
11 #include <functional>
12 #include <list>
13 #include <string>
14 
15 #include <XmlRpcValue.h>
16 
19 
20 namespace cras
21 {
22 
29 template<typename ResultType, typename ParamServerType>
31 {
38  static ResultType toResult(const ParamServerType& value)
39  {
40  return static_cast<ResultType>(value);
41  };
42 };
43 
51 template<typename ParamServerType>
53 {
66  static bool toParam(const ::XmlRpc::XmlRpcValue& x, ParamServerType& v, bool skipNonConvertible = false,
67  ::std::list<::std::string>* errors = nullptr)
68  {
69  return ::cras::convert(x, v, skipNonConvertible, errors);
70  };
71 };
72 
80 template<typename T>
82 {
89  static ::std::string to_string(const T& value)
90  {
92  };
93 };
94 
101 template<typename ResultType>
103 {
105  typedef ResultType type;
106 };
107 
114 template<typename ResultType, typename ParamServerType = typename ::cras::DefaultParamServerType<ResultType>::type>
116 {
123  typedef ::std::function<ResultType(const ParamServerType& value)> ToResultFn;
124 
137  typedef ::std::function<bool(
138  const ::XmlRpc::XmlRpcValue& xmlValue,
139  ParamServerType& value,
140  const bool skipNonConvertible,
141  ::std::list<::std::string>* errors)> ToParamFn;
142 
144  bool printMessages {true};
145 
147  bool printDefaultAsWarn {false};
148 
151  bool throwIfConvertFails {false};
152 
154  bool allowNestedParams {true};
155 
157  ::std::string origNamespace {};
158 
160  ::std::string origParamName {};
161 
164 
167 
170 
173 
181  template<typename R1, typename P1>
182  GetParamOptions& operator=(const ::cras::GetParamOptions<R1, P1>& other)
183  {
184  this->printMessages = other.printMessages;
185  this->printDefaultAsWarn = other.printDefaultAsWarn;
186  this->throwIfConvertFails = other.throwIfConvertFails;
187  this->allowNestedParams = other.allowNestedParams;
188  this->origNamespace = other.origNamespace;
189  this->origParamName = other.origParamName;
190  return *this;
191  }
192 
198  GetParamOptions& operator=(const ::cras::GetParamOptions<ResultType, ParamServerType>& other)
199  {
200  this->printMessages = other.printMessages;
201  this->printDefaultAsWarn = other.printDefaultAsWarn;
202  this->throwIfConvertFails = other.throwIfConvertFails;
203  this->allowNestedParams = other.allowNestedParams;
204  this->origNamespace = other.origNamespace;
205  this->origParamName = other.origParamName;
206  this->paramToStr = other.paramToStr;
207  this->resultToStr = other.resultToStr;
208  this->toResult = other.toResult;
209  this->toParam = other.toParam;
210  return *this;
211  }
212 
221  template<typename NewParamServerType>
223  typename ::cras::GetParamOptions<ResultType, NewParamServerType>::ToResultFn newToResult =
225  ::cras::ToStringFn<NewParamServerType> newParamToStr =
226  [](const NewParamServerType& s){ return ::cras::to_string(s); },
227  typename ::cras::GetParamOptions<ResultType, NewParamServerType>::ToParamFn newToParam =
228  [](const ::XmlRpc::XmlRpcValue& x, NewParamServerType& v, bool skipNonConvertible = false,
229  ::std::list<::std::string>* errors = nullptr) -> bool {
230  return ::cras::convert(x, v, skipNonConvertible, errors);
231  }) const
232  {
233  // We can't initialize the object with the default values of toParam and toResult because they are ill-formed
235  {{}, {}, {}, {}, {}, {}, {}, {}, newToResult, newToParam};
236  options = *this;
237  options.resultToStr = this->resultToStr;
238  options.paramToStr = newParamToStr;
239  return options;
240  }
241 };
242 
243 }
cras::GetParamOptions::allowNestedParams
bool allowNestedParams
Allow parameters of form a/b/c (leads to recursive parsing of the sub-namespaces).
Definition: get_param_options.hpp:154
cras::DefaultToParamFn
Default function for converting XmlRpcValue to an intermediate value of type ParamServerType.
Definition: get_param_options.hpp:52
cras::GetParamOptions::resultToStr
::cras::ToStringFn< ResultType > resultToStr
A function that converts ResultType values to string for use in log messages.
Definition: get_param_options.hpp:166
cras
Definition: any.hpp:15
cras::GetParamOptions::operator=
GetParamOptions & operator=(const ::cras::GetParamOptions< R1, P1 > &other)
Assign from options of a different type. Only the non-function members are copied!
Definition: get_param_options.hpp:182
cras::DefaultToResultFn
Default function for converting param server types to result types.
Definition: get_param_options.hpp:30
s
XmlRpcServer s
cras::DefaultParamServerType
Default ParamServerType for the given ResultType.
Definition: get_param_options.hpp:102
cras::ToStringFn
::std::function<::std::string(const T &)> ToStringFn
Type of function that converts anything to a string.
Definition: string_utils.hpp:339
string_utils.hpp
Utils for working with strings.
cras::GetParamOptions::ToResultFn
::std::function< ResultType(const ParamServerType &value)> ToResultFn
Function converting ParamServerType values to ResultType.
Definition: get_param_options.hpp:123
cras::DefaultToParamFn::toParam
static bool toParam(const ::XmlRpc::XmlRpcValue &x, ParamServerType &v, bool skipNonConvertible=false, ::std::list<::std::string > *errors=nullptr)
Function converting XmlRpcValue to an intermediate value of type ParamServerType.
Definition: get_param_options.hpp:66
cras::GetParamOptions::toResult
ToResultFn toResult
A function converting ParamServerType values to ResultType.
Definition: get_param_options.hpp:169
cras::GetParamOptions::printDefaultAsWarn
bool printDefaultAsWarn
Whether defaulted parameters are reported as warning or info level messages.
Definition: get_param_options.hpp:147
cras::DefaultParamServerType::type
ResultType type
The ParamServerType to be used as intermediate type when reading values of type ResultType.
Definition: get_param_options.hpp:105
XmlRpcValue.h
cras::ParamToStringFn::to_string
::std::string to_string(const T &value)
Convert the given value to a string representation.
Definition: get_param_options.hpp:89
cras::GetParamOptions::origParamName
::std::string origParamName
The human-friendly parameter name to be reported in log messages.
Definition: get_param_options.hpp:160
cras::GetParamOptions::throwIfConvertFails
bool throwIfConvertFails
Throw GetParamException if any conversion fails. If false, the default value is used instead of a val...
Definition: get_param_options.hpp:151
cras::GetParamOptions::origNamespace
::std::string origNamespace
The human-friendly namespace to be reported in log messages.
Definition: get_param_options.hpp:157
cras::GetParamOptions::asType
::cras::GetParamOptions< ResultType, NewParamServerType > asType(typename ::cras::GetParamOptions< ResultType, NewParamServerType >::ToResultFn newToResult=&::cras::DefaultToResultFn< ResultType, NewParamServerType >::toResult, ::cras::ToStringFn< NewParamServerType > newParamToStr=[](const NewParamServerType &s){ return ::cras::to_string(s);}, typename ::cras::GetParamOptions< ResultType, NewParamServerType >::ToParamFn newToParam=[](const ::XmlRpc::XmlRpcValue &x, NewParamServerType &v, bool skipNonConvertible=false, ::std::list<::std::string > *errors=nullptr) -> bool { return ::cras::convert(x, v, skipNonConvertible, errors);}) const
Convert this options object to a similar object with a different ParamServerType.
Definition: get_param_options.hpp:222
cras::GetParamOptions::ToParamFn
::std::function< bool(const ::XmlRpc::XmlRpcValue &xmlValue, ParamServerType &value, const bool skipNonConvertible, ::std::list<::std::string > *errors)> ToParamFn
Function converting XmlRpcValue to an intermediate value of type ParamServerType.
Definition: get_param_options.hpp:141
cras::DefaultToResultFn::toResult
static ResultType toResult(const ParamServerType &value)
Function converting ParamServerType values to ResultType.
Definition: get_param_options.hpp:38
cras::to_string
inline ::std::string to_string(const ::Eigen::Matrix< Scalar, Rows, Cols, Options, MaxRows, MaxCols > &value)
Definition: string_utils/eigen.hpp:21
cras::ParamToStringFn
Default function for converting values to string in getParam(Verbose) functions. Uses cras::to_string...
Definition: get_param_options.hpp:81
cras::GetParamOptions::printMessages
bool printMessages
Whether to print error messages to log.
Definition: get_param_options.hpp:144
cras::GetParamOptions::toParam
ToParamFn toParam
A function converting XmlRpcValue to an intermediate value of type ParamServerType.
Definition: get_param_options.hpp:172
cras::GetParamOptions::paramToStr
::cras::ToStringFn< ParamServerType > paramToStr
A function that converts ParamServerType values to string for use in log messages.
Definition: get_param_options.hpp:163
cras::GetParamOptions::operator=
GetParamOptions & operator=(const ::cras::GetParamOptions< ResultType, ParamServerType > &other)
Assign from options of the same type. Non-function members are also copied.
Definition: get_param_options.hpp:198
xmlrpc_value_utils.hpp
Utilities for working with XmlRpcValues.
cras::convert
bool convert(const ::XmlRpc::XmlRpcValue &x, ::XmlRpc::XmlRpcValue &v, bool=false, ::std::list<::std::string > *errors=nullptr)
Convert XmlRpcValue x to value v.
Definition: xmlrpc_value_utils.hpp:53
cras::GetParamOptions
Options specifying behavior of getParam() calls.
Definition: get_param_options.hpp:115


cras_cpp_common
Author(s): Martin Pecka
autogenerated on Mon Jun 17 2024 02:48:56