18 #ifndef SDFORMAT_PARAM_HH_
19 #define SDFORMAT_PARAM_HH_
23 #include <boost/any.hpp>
24 #include <boost/variant.hpp>
36 #include <ignition/math.hh>
46 #pragma warning(disable: 4251)
76 public:
Param(
const std::string &_key,
const std::string &_typeName,
77 const std::string &_default,
bool _required,
78 const std::string &_description =
"");
81 public:
virtual ~
Param();
85 public: std::string GetAsString()
const;
89 public: std::string GetDefaultAsString()
const;
93 public:
bool SetFromString(
const std::string &_value);
100 public:
const std::string &GetKey()
const;
105 public:
template<
typename Type>
110 public:
const std::string &GetTypeName()
const;
114 public:
bool GetRequired()
const;
118 public:
bool GetSet()
const;
127 public:
template<
typename T>
128 void SetUpdateFunc(T _updateFunc);
132 public:
void Update();
139 public:
template<
typename T>
140 bool Set(
const T &_value);
145 public:
bool GetAny(boost::any &_anyVal)
const;
151 public:
template<
typename T>
152 bool Get(T &_value)
const;
158 public:
template<
typename T>
159 bool GetDefault(T &_value)
const;
165 public:
Param &operator=(
const Param &_param);
169 public:
void SetDescription(
const std::string &_desc);
173 public: std::string GetDescription()
const;
188 private:
bool ValueFromString(
const std::string &_value);
191 private: std::unique_ptr<ParamPrivate> dataPtr;
218 public:
typedef boost::variant<bool, char, std::string, int, std::uint64_t,
220 ignition::math::Color,
221 ignition::math::Vector2i,
222 ignition::math::Vector2d,
223 ignition::math::Vector3d,
224 ignition::math::Quaterniond,
238 this->dataPtr->updateFunc = _updateFunc;
247 std::stringstream ss;
253 sdferr <<
"Unable to set parameter["
254 << this->dataPtr->key <<
"]."
255 <<
"Type used must have a stream input and output operator,"
256 <<
"which allows proper functioning of Param.\n";
267 if (
typeid(T) ==
typeid(
bool) && this->dataPtr->typeName ==
"string")
269 std::stringstream ss;
270 ss << this->dataPtr->value;
272 std::string strValue;
275 std::transform(strValue.begin(), strValue.end(),
276 strValue.begin(), ::tolower);
278 std::stringstream tmp;
279 if (strValue ==
"true" || strValue ==
"1")
289 else if (
typeid(T) == this->dataPtr->value.type())
291 #if BOOST_VERSION < 105800
292 _value = boost::get<T>(this->dataPtr->value);
294 _value = boost::relaxed_get<T>(this->dataPtr->value);
299 std::stringstream ss;
300 ss << this->dataPtr->value;
306 sdferr <<
"Unable to convert parameter["
307 << this->dataPtr->key <<
"] "
309 << this->dataPtr->typeName <<
"], to "
310 <<
"type[" <<
typeid(T).name() <<
"]\n";
320 std::stringstream ss;
324 ss << this->dataPtr->defaultValue;
329 sdferr <<
"Unable to convert parameter["
330 << this->dataPtr->key <<
"] "
332 << this->dataPtr->typeName <<
"], to "
333 <<
"type[" <<
typeid(T).name() <<
"]\n";
341 template<
typename Type>
344 return this->dataPtr->value.type() ==
typeid(Type);
boost::variant< bool, char, std::string, int, std::uint64_t, unsigned int, double, float, sdf::Time, ignition::math::Color, ignition::math::Vector2i, ignition::math::Vector2d, ignition::math::Vector3d, ignition::math::Quaterniond, ignition::math::Pose3d > ParamVariant
Definition: Param.hh:225
A parameter class.
Definition: Param.hh:66
bool Set(const T &_value)
Set the parameter's value.
Definition: Param.hh:243
ParamVariant value
This parameter's value.
Definition: Param.hh:228
ParamVariant defaultValue
This parameter's default value.
Definition: Param.hh:231
friend std::ostream & operator<<(std::ostream &_out, const Param &_p)
Ostream operator.
Definition: Param.hh:179
class SDFORMAT_VISIBLE Param
Definition: Param.hh:51
void SetUpdateFunc(T _updateFunc)
Set the update function.
Definition: Param.hh:236
bool set
True if the parameter is set.
Definition: Param.hh:205
std::string typeName
Definition: Param.hh:208
std::function< boost::any()> updateFunc
Update function pointer.
Definition: Param.hh:214
bool Get(T &_value) const
Get the value of the parameter.
Definition: Param.hh:263
bool required
True if the parameter is required.
Definition: Param.hh:202
#define SDFORMAT_VISIBLE
Use to represent "symbol visible" if supported.
Definition: system_util.hh:48
#define sdferr
Output an error message.
Definition: Console.hh:52
std::string description
Description of the parameter.
Definition: Param.hh:211
std::shared_ptr< Param > ParamPtr
Definition: Param.hh:55
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:59
bool IsType() const
Return true if the param is a particular type.
Definition: Param.hh:342
A Time class, can be used to hold wall- or sim-time.
Definition: Types.hh:133
std::string key
Key value.
Definition: Param.hh:199
bool GetDefault(T &_value) const
Get the default value of the parameter.
Definition: Param.hh:318