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 }
inline ::std::string to_string(const ::Eigen::Matrix< Scalar, Rows, Cols, Options, MaxRows, MaxCols > &value)
Default function for converting values to string in getParam(Verbose) functions. Uses cras::to_string...
::cras::ToStringFn< ParamServerType > paramToStr
A function that converts ParamServerType values to string for use in log messages.
GetParamOptions & operator=(const ::cras::GetParamOptions< R1, P1 > &other)
Assign from options of a different type. Only the non-function members are copied! ...
::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. ...
Default function for converting param server types to result types.
::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. ...
bool printMessages
Whether to print error messages to log.
Utilities for working with XmlRpcValues.
::std::string to_string(const T &value)
Convert the given value to a string representation.
::cras::ToStringFn< ResultType > resultToStr
A function that converts ResultType values to string for use in log messages.
options
GetParamOptions & operator=(const ::cras::GetParamOptions< ResultType, ParamServerType > &other)
Assign from options of the same type. Non-function members are also copied.
Default ParamServerType for the given ResultType.
::std::function< ResultType(const ParamServerType &value)> ToResultFn
Function converting ParamServerType values to ResultType.
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. ...
bool convert(const ::XmlRpc::XmlRpcValue &x, ::XmlRpc::XmlRpcValue &v, bool=false, ::std::list<::std::string > *errors=nullptr)
Convert XmlRpcValue x to value v.
static ResultType toResult(const ParamServerType &value)
Function converting ParamServerType values to ResultType.
Default function for converting XmlRpcValue to an intermediate value of type ParamServerType.
ResultType type
The ParamServerType to be used as intermediate type when reading values of type ResultType.
Definition: any.hpp:15
Options specifying behavior of getParam() calls.
Utils for working with strings.
::std::function<::std::string(const T &)> ToStringFn
Type of function that converts anything to a string.


cras_cpp_common
Author(s): Martin Pecka
autogenerated on Sat Jun 17 2023 02:32:53