18 #ifndef _SDF_PARAM_HH_
19 #define _SDF_PARAM_HH_
23 #include <boost/lexical_cast.hpp>
24 #include <boost/any.hpp>
25 #include <boost/variant.hpp>
26 #include <boost/version.hpp>
35 #include <ignition/math.hh>
67 public:
Param(
const std::string &_key,
const std::string &_typeName,
68 const std::string &_default,
bool _required,
69 const std::string &_description =
"");
72 public:
virtual ~
Param();
76 public: std::string GetAsString()
const;
80 public: std::string GetDefaultAsString()
const;
84 public:
bool SetFromString(
const std::string &_value);
91 public:
const std::string &GetKey()
const;
96 public:
template<
typename Type>
101 public:
const std::string &GetTypeName()
const;
105 public:
bool GetRequired()
const;
109 public:
bool GetSet()
const;
118 public:
template<
typename T>
119 void SetUpdateFunc(T _updateFunc);
123 public:
void Update();
131 public:
template<
typename T>
132 bool Set(
const T &_value);
137 public:
bool GetAny(boost::any &_anyVal)
const;
143 public:
template<
typename T>
144 bool Get(T &_value)
const;
150 public:
template<
typename T>
151 bool GetDefault(T &_value)
const;
157 public:
Param &operator=(
const Param &_param);
161 public:
void SetDescription(
const std::string &_desc);
165 public: std::string GetDescription()
const;
180 private:
template<
typename T>
181 void Init(
const std::string &_value);
211 public:
typedef boost::variant<bool, char, std::string, int, uint64_t,
213 ignition::math::Vector3d, ignition::math::Vector2i,
214 ignition::math::Vector2d, ignition::math::Quaterniond,
237 this->
SetFromString(boost::lexical_cast<std::string>(_value));
241 sdferr <<
"Unable to set parameter["
242 << this->dataPtr->
key <<
"]."
243 <<
"Type type used must have a stream input and output"
244 <<
"operator, which allow boost::lexical_cast to"
245 <<
"function properly.\n";
257 if (
typeid(T) ==
typeid(
bool) &&
258 this->dataPtr->
typeName ==
"string")
260 std::string strValue =
261 boost::lexical_cast<std::string>(this->dataPtr->
value);
262 std::transform(strValue.begin(), strValue.end(),
263 strValue.begin(), ::tolower);
264 if (strValue ==
"true" || strValue ==
"1")
265 _value = boost::lexical_cast<T>(
"1");
267 _value = boost::lexical_cast<T>(
"0");
269 else if (
typeid(T) == this->dataPtr->
value.type())
271 #if BOOST_VERSION < 105800
272 _value = boost::get<T>(this->dataPtr->
value);
274 _value = boost::relaxed_get<T>(this->dataPtr->
value);
279 _value = boost::lexical_cast<T>(this->dataPtr->
value);
284 sdferr <<
"Unable to convert parameter["
285 << this->dataPtr->
key <<
"] "
287 << this->dataPtr->
typeName <<
"], to "
288 <<
"type[" <<
typeid(T).name() <<
"]\n";
300 _value = boost::lexical_cast<T>(this->dataPtr->
defaultValue);
304 sdferr <<
"Unable to convert parameter["
305 << this->dataPtr->
key <<
"] "
307 << this->dataPtr->
typeName <<
"], to "
308 <<
"type[" <<
typeid(T).name() <<
"]\n";
316 void Param::Init(
const std::string &_value)
320 this->dataPtr->
value = boost::lexical_cast<T>(_value);
324 if (this->dataPtr->
typeName ==
"bool")
326 std::string strValue = _value;
327 std::transform(strValue.begin(), strValue.end(),
328 strValue.begin(), ::tolower);
329 if (strValue ==
"true" || strValue ==
"1")
330 this->dataPtr->
value =
true;
332 this->dataPtr->
value =
false;
336 sdferr <<
"Unable to init parameter value from string["
342 this->dataPtr->
set =
false;
346 template<
typename Type>
349 return this->dataPtr->
value.type() ==
typeid(Type);
A parameter class.
Definition: Param.hh:58
bool Set(const T &_value)
Set the parameter's value.
Definition: Param.hh:233
ParamVariant value
This parameter's value.
Definition: Param.hh:218
ParamVariant defaultValue
This parameter's default value.
Definition: Param.hh:221
friend std::ostream & operator<<(std::ostream &_out, const Param &_p)
Ostream operator.
Definition: Param.hh:171
class SDFORMAT_VISIBLE Param
Definition: Param.hh:43
boost::variant< bool, char, std::string, int, uint64_t, unsigned int, double, float, sdf::Time, sdf::Color, ignition::math::Vector3d, ignition::math::Vector2i, ignition::math::Vector2d, ignition::math::Quaterniond, ignition::math::Pose3d > ParamVariant
Definition: Param.hh:215
void SetUpdateFunc(T _updateFunc)
Set the update function.
Definition: Param.hh:226
bool set
True if the parameter is set.
Definition: Param.hh:198
std::string typeName
Definition: Param.hh:201
std::function< boost::any()> updateFunc
Update function pointer.
Definition: Param.hh:207
bool Get(T &_value) const
Get the value of the parameter.
Definition: Param.hh:253
bool required
True if the parameter is required.
Definition: Param.hh:195
#define SDFORMAT_VISIBLE
Use to represent "symbol visible" if supported.
Definition: system_util.hh:48
#define sdferr
Output an error message.
Definition: Console.hh:54
std::string description
Description of the parameter.
Definition: Param.hh:204
std::shared_ptr< Param > ParamPtr
Definition: Param.hh:47
bool SetFromString(const std::string &_value)
Set the parameter value from a string.
std::string GetAsString() const
Get the value as a string.
std::vector< ParamPtr > Param_V
Definition: Param.hh:51
Defines a color.
Definition: Types.hh:61
bool IsType() const
Return true if the param is a particular type.
Definition: Param.hh:347
A Time class, can be used to hold wall- or sim-time.
Definition: Types.hh:120
std::string key
Key value.
Definition: Param.hh:192
bool GetDefault(T &_value) const
Get the default value of the parameter.
Definition: Param.hh:296