18 #ifndef SDFORMAT_PARAM_HH_ 19 #define SDFORMAT_PARAM_HH_ 21 #include <boost/any.hpp> 23 #pragma clang diagnostic push 24 #pragma clang diagnostic ignored "-Wc++98-c++11-compat" 26 #include <boost/variant.hpp> 28 #pragma clang diagnostic pop 41 #include <ignition/math.hh> 51 #pragma warning(disable: 4251) 81 public:
Param(
const std::string &_key,
const std::string &_typeName,
82 const std::string &_default,
bool _required,
83 const std::string &_description =
"");
86 public:
virtual ~
Param();
90 public: std::string GetAsString()
const;
94 public: std::string GetDefaultAsString()
const;
98 public:
bool SetFromString(
const std::string &_value);
101 public:
void Reset();
105 public:
const std::string &GetKey()
const;
110 public:
template<
typename Type>
115 public:
const std::string &GetTypeName()
const;
119 public:
bool GetRequired()
const;
123 public:
bool GetSet()
const;
127 public: ParamPtr Clone()
const;
132 public:
template<
typename T>
133 void SetUpdateFunc(T _updateFunc);
137 public:
void Update();
144 public:
template<
typename T>
145 bool Set(
const T &_value);
150 public:
bool GetAny(boost::any &_anyVal)
const;
156 public:
template<
typename T>
157 bool Get(T &_value)
const;
163 public:
template<
typename T>
164 bool GetDefault(T &_value)
const;
170 public:
Param &operator=(
const Param &_param);
174 public:
void SetDescription(
const std::string &_desc);
178 public: std::string GetDescription()
const;
193 private:
bool ValueFromString(
const std::string &_value);
196 private: std::unique_ptr<ParamPrivate> dataPtr;
223 public:
typedef boost::variant<bool, char, std::string, int, std::uint64_t,
225 ignition::math::Color,
226 ignition::math::Vector2i,
227 ignition::math::Vector2d,
228 ignition::math::Vector3d,
229 ignition::math::Quaterniond,
243 this->dataPtr->updateFunc = _updateFunc;
252 std::stringstream ss;
254 return this->SetFromString(ss.str());
258 sdferr <<
"Unable to set parameter[" 259 << this->dataPtr->key <<
"]." 260 <<
"Type used must have a stream input and output operator," 261 <<
"which allows proper functioning of Param.\n";
272 if (
typeid(T) ==
typeid(
bool) && this->dataPtr->typeName ==
"string")
274 std::stringstream ss;
275 ss << this->dataPtr->value;
277 std::string strValue;
280 std::transform(strValue.begin(), strValue.end(), strValue.begin(),
283 return static_cast<unsigned char>(std::tolower(c));
286 std::stringstream tmp;
287 if (strValue ==
"true" || strValue ==
"1")
297 else if (
typeid(T) == this->dataPtr->value.type())
299 #if BOOST_VERSION < 105800 300 _value = boost::get<T>(this->dataPtr->value);
302 _value = boost::relaxed_get<T>(this->dataPtr->value);
307 std::stringstream ss;
308 ss << this->dataPtr->value;
314 sdferr <<
"Unable to convert parameter[" 315 << this->dataPtr->key <<
"] " 317 << this->dataPtr->typeName <<
"], to " 318 <<
"type[" <<
typeid(T).name() <<
"]\n";
328 std::stringstream ss;
332 ss << this->dataPtr->defaultValue;
337 sdferr <<
"Unable to convert parameter[" 338 << this->dataPtr->key <<
"] " 340 << this->dataPtr->typeName <<
"], to " 341 <<
"type[" <<
typeid(T).name() <<
"]\n";
349 template<
typename Type>
352 return this->dataPtr->value.type() ==
typeid(Type);
std::string GetAsString() const
Get the value as a string.
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:230
A parameter class.
Definition: Param.hh:71
bool Set(const T &_value)
Set the parameter's value.
Definition: Param.hh:248
ParamVariant value
This parameter's value.
Definition: Param.hh:233
bool Get(T &_value) const
Get the value of the parameter.
Definition: Param.hh:268
bool IsType() const
Return true if the param is a particular type.
Definition: Param.hh:350
ParamVariant defaultValue
This parameter's default value.
Definition: Param.hh:236
friend std::ostream & operator<<(std::ostream &_out, const Param &_p)
Ostream operator.
Definition: Param.hh:184
class SDFORMAT_VISIBLE Param
Definition: Param.hh:56
void SetUpdateFunc(T _updateFunc)
Set the update function.
Definition: Param.hh:241
std::string typeName
Definition: Param.hh:213
std::function< boost::any()> updateFunc
Update function pointer.
Definition: Param.hh:219
bool required
True if the parameter is required.
Definition: Param.hh:207
#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:216
std::shared_ptr< Param > ParamPtr
Definition: Param.hh:60
std::vector< ParamPtr > Param_V
Definition: Param.hh:64
bool GetDefault(T &_value) const
Get the default value of the parameter.
Definition: Param.hh:326
namespace for Simulation Description Format parser
Definition: Console.hh:35
A Time class, can be used to hold wall- or sim-time.
Definition: Types.hh:133
std::string key
Key value.
Definition: Param.hh:204