18 #ifndef SDFORMAT_PARAM_HH_
19 #define SDFORMAT_PARAM_HH_
33 #include <ignition/math.hh>
36 #include "sdf/sdf_config.h"
44 #pragma warning(disable: 4251)
50 inline namespace SDF_VERSION_NAMESPACE {
85 std::visit([&os](
auto const &v)
104 public:
Param(
const std::string &_key,
const std::string &_typeName,
105 const std::string &_default,
bool _required,
106 const std::string &_description =
"");
128 public:
const std::string &
GetKey()
const;
133 public:
template<
typename Type>
155 public:
template<
typename T>
156 void SetUpdateFunc(T _updateFunc);
167 public:
template<
typename T>
168 bool Set(
const T &_value);
173 public:
bool GetAny(std::any &_anyVal)
const;
179 public:
template<
typename T>
180 bool Get(T &_value)
const;
186 public:
template<
typename T>
187 bool GetDefault(T &_value)
const;
217 private:
bool ValueFromString(
const std::string &_value);
220 private: std::unique_ptr<ParamPrivate> dataPtr;
249 public:
typedef std::variant<bool, char, std::string, int, std::uint64_t,
251 ignition::math::Angle,
252 ignition::math::Color,
253 ignition::math::Vector2i,
254 ignition::math::Vector2d,
255 ignition::math::Vector3d,
256 ignition::math::Quaterniond,
271 const std::string &_typeName,
272 const std::string &_valueStr,
277 public:
template<
typename T>
278 std::string TypeToString()
const;
283 std::string ParamPrivate::TypeToString()
const
286 if constexpr (std::is_same_v<T, bool>)
288 else if constexpr (std::is_same_v<T, char>)
290 else if constexpr (std::is_same_v<T, std::string>)
292 else if constexpr (std::is_same_v<T, int>)
294 else if constexpr (std::is_same_v<T, std::uint64_t>)
296 else if constexpr (std::is_same_v<T, unsigned int>)
297 return "unsigned int";
298 else if constexpr (std::is_same_v<T, double>)
300 else if constexpr (std::is_same_v<T, float>)
302 else if constexpr (std::is_same_v<T, sdf::Time>)
304 else if constexpr (std::is_same_v<T, ignition::math::Angle>)
306 else if constexpr (std::is_same_v<T, ignition::math::Color>)
308 else if constexpr (std::is_same_v<T, ignition::math::Vector2i>)
310 else if constexpr (std::is_same_v<T, ignition::math::Vector2d>)
312 else if constexpr (std::is_same_v<T, ignition::math::Vector3d>)
314 else if constexpr (std::is_same_v<T, ignition::math::Quaterniond>)
316 else if constexpr (std::is_same_v<T, ignition::math::Pose3d>)
324 void Param::SetUpdateFunc(T _updateFunc)
326 this->dataPtr->updateFunc = _updateFunc;
331 bool Param::Set(
const T &_value)
335 std::stringstream ss;
337 return this->SetFromString(ss.str());
341 sdferr <<
"Unable to set parameter["
342 << this->dataPtr->key <<
"]."
343 <<
"Type used must have a stream input and output operator,"
344 <<
"which allows proper functioning of Param.\n";
351 bool Param::Get(T &_value)
const
353 T *value = std::get_if<T>(&this->dataPtr->value);
360 std::string typeStr = this->dataPtr->TypeToString<T>();
363 sdferr <<
"Unknown parameter type[" <<
typeid(T).name() <<
"]\n";
367 std::string valueStr = this->GetAsString();
369 bool success = this->dataPtr->ValueFromStringImpl(typeStr, valueStr, pv);
373 _value = std::get<T>(pv);
375 else if (typeStr ==
"bool" && this->dataPtr->typeName ==
"string")
382 std::stringstream tmp;
383 if (valueStr ==
"true" || valueStr ==
"1")
400 bool Param::GetDefault(T &_value)
const
402 std::stringstream ss;
411 sdferr <<
"Unable to convert parameter["
412 << this->dataPtr->key <<
"] "
414 << this->dataPtr->typeName <<
"], to "
415 <<
"type[" <<
typeid(T).name() <<
"]\n";
423 template<
typename Type>
424 bool Param::IsType()
const
426 return std::holds_alternative<Type>(this->dataPtr->value);
ParamVariant defaultValue
This parameter's default value.
Definition: Param.hh:263
std::function< std::any()> updateFunc
Update function pointer.
Definition: Param.hh:243
bool set
True if the parameter is set.
Definition: Param.hh:234
bool required
True if the parameter is required.
Definition: Param.hh:231
std::string description
Description of the parameter.
Definition: Param.hh:240
std::string typeName
Definition: Param.hh:237
ParamVariant value
This parameter's value.
Definition: Param.hh:260
std::string key
Key value.
Definition: Param.hh:228
bool SDFORMAT_VISIBLE ValueFromStringImpl(const std::string &_typeName, const std::string &_valueStr, ParamVariant &_valueToSet) const
Method used to set the Param from a passed-in string.
std::variant< bool, char, std::string, int, std::uint64_t, unsigned int, double, float, sdf::Time, ignition::math::Angle, ignition::math::Color, ignition::math::Vector2i, ignition::math::Vector2d, ignition::math::Vector3d, ignition::math::Quaterniond, ignition::math::Pose3d > ParamVariant
Definition: Param.hh:257
A parameter class.
Definition: Param.hh:95
std::string GetAsString() const
Get the value as a string.
const std::string & GetTypeName() const
Get the type name value.
Param(const std::string &_key, const std::string &_typeName, const std::string &_default, bool _required, const std::string &_description="")
Constructor.
std::string GetDescription() const
Get the description of the parameter.
bool SetFromString(const std::string &_value)
Set the parameter value from a string.
ParamPtr Clone() const
Clone the parameter.
void Update()
Set the parameter's value using the updateFunc.
bool GetRequired() const
Return whether the parameter is required.
bool GetSet() const
Return true if the parameter has been set.
friend std::ostream & operator<<(std::ostream &_out, const Param &_p)
Ostream operator.
Definition: Param.hh:207
bool GetAny(std::any &_anyVal) const
Get the value of the parameter as a std::any.
void SetDescription(const std::string &_desc)
Set the description of the parameter.
virtual ~Param()
Destructor.
const std::string & GetKey() const
Get the key value.
Param & operator=(const Param &_param)
Equal operator.
std::string GetDefaultAsString() const
Get the default value as a string.
void Reset()
Reset the parameter to the default value.
A Time class, can be used to hold wall- or sim-time.
Definition: Types.hh:154
#define sdferr
Output an error message.
Definition: Console.hh:57
std::ostream & operator<<(std::ostream &os, ParamStreamer< T > s)
Definition: Param.hh:75
ParamStreamer(T) -> ParamStreamer< T >
std::shared_ptr< Param > ParamPtr
Definition: Param.hh:57
std::vector< ParamPtr > Param_V
Definition: Param.hh:61
std::string SDFORMAT_VISIBLE lowercase(const std::string &_in)
Transforms a string to its lowercase equivalent.
namespace for Simulation Description Format parser
Definition: Actor.hh:33
const T & val
Definition: Param.hh:69
#define SDFORMAT_VISIBLE
Use to represent "symbol visible" if supported.
Definition: system_util.hh:48