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;
113 public: ParamPtr Clone()
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,
228 this->dataPtr->updateFunc = _updateFunc;
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 if (strValue ==
"true" || strValue ==
"1")
263 _value = boost::lexical_cast<T>(
"1");
265 _value = boost::lexical_cast<T>(
"0");
267 else if (
typeid(T) == this->dataPtr->value.type())
269 #if BOOST_VERSION < 105800 270 _value = boost::get<T>(this->dataPtr->value);
272 _value = boost::relaxed_get<T>(this->dataPtr->value);
277 _value = boost::lexical_cast<T>(this->dataPtr->value);
282 sdferr <<
"Unable to convert parameter[" 283 << this->dataPtr->key <<
"] " 285 << this->dataPtr->typeName <<
"], to " 286 <<
"type[" <<
typeid(T).name() <<
"]\n";
298 _value = boost::lexical_cast<T>(this->dataPtr->defaultValue);
302 sdferr <<
"Unable to convert parameter[" 303 << this->dataPtr->key <<
"] " 305 << this->dataPtr->typeName <<
"], to " 306 <<
"type[" <<
typeid(T).name() <<
"]\n";
314 void Param::Init(
const std::string &_value)
318 this->dataPtr->value = boost::lexical_cast<T>(_value);
322 if (this->dataPtr->typeName ==
"bool")
324 std::string strValue = _value;
325 std::transform(strValue.begin(), strValue.end(),
326 strValue.begin(), ::tolower);
327 if (strValue ==
"true" || strValue ==
"1")
328 this->dataPtr->value =
true;
330 this->dataPtr->value =
false;
334 sdferr <<
"Unable to init parameter value from string[" 339 this->dataPtr->defaultValue = this->dataPtr->value;
340 this->dataPtr->set =
false;
344 template<
typename Type>
347 return this->dataPtr->value.type() ==
typeid(Type);
std::string GetAsString() const
Get the value as a string.
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
bool Get(T &_value) const
Get the value of the parameter.
Definition: Param.hh:253
bool IsType() const
Return true if the param is a particular type.
Definition: Param.hh:345
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
std::string typeName
Definition: Param.hh:201
std::function< boost::any()> updateFunc
Update function pointer.
Definition: Param.hh:207
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
std::vector< ParamPtr > Param_V
Definition: Param.hh:51
Defines a color.
Definition: Types.hh:61
bool GetDefault(T &_value) const
Get the default value of the parameter.
Definition: Param.hh:294
namespace for Simulation Description Format parser
Definition: Console.hh:36
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