18 #ifndef SDFORMAT_PARAM_HH_ 19 #define SDFORMAT_PARAM_HH_ 34 #include <ignition/math.hh> 45 #pragma warning(disable: 4251) 51 inline namespace SDF_VERSION_NAMESPACE {
76 std::ostream& operator<<(std::ostream &os, ParamStreamer<T> s)
86 std::visit([&os](
auto const &v)
105 public:
Param(
const std::string &_key,
const std::string &_typeName,
106 const std::string &_default,
bool _required,
107 const std::string &_description =
"");
119 public:
Param(
const std::string &_key,
const std::string &_typeName,
120 const std::string &_default,
bool _required,
121 const std::string &_minValue,
const std::string &_maxValue,
122 const std::string &_description =
"");
131 public:
Param(
Param &&_param) noexcept =
default;
137 public:
Param &operator=(
const Param &_param);
142 public:
Param &operator=(
Param &&_param) noexcept =
default;
145 public:
virtual ~
Param();
149 public: std::string GetAsString()
const;
153 public: std::string GetDefaultAsString()
const;
159 public: std::optional<std::string> GetMinValueAsString()
const;
165 public: std::optional<std::string> GetMaxValueAsString()
const;
169 public:
bool SetFromString(
const std::string &_value);
172 public:
void Reset();
176 public:
const std::string &GetKey()
const;
181 public:
template<
typename Type>
186 public:
const std::string &GetTypeName()
const;
190 public:
bool GetRequired()
const;
194 public:
bool GetSet()
const;
198 public: ParamPtr Clone()
const;
203 public:
template<
typename T>
204 void SetUpdateFunc(T _updateFunc);
208 public:
void Update();
215 public:
template<
typename T>
216 bool Set(
const T &_value);
221 public:
bool GetAny(std::any &_anyVal)
const;
227 public:
template<
typename T>
228 bool Get(T &_value)
const;
234 public:
template<
typename T>
235 bool GetDefault(T &_value)
const;
239 public:
void SetDescription(
const std::string &_desc);
243 public: std::string GetDescription()
const;
247 public:
bool ValidateValue()
const;
263 private:
bool ValueFromString(
const std::string &_value);
266 private: std::unique_ptr<ParamPrivate> dataPtr;
295 public:
typedef std::variant<bool, char, std::string, int, std::uint64_t,
296 unsigned int, double, float, sdf::Time,
297 ignition::math::Angle,
298 ignition::math::Color,
299 ignition::math::Vector2i,
300 ignition::math::Vector2d,
301 ignition::math::Vector3d,
302 ignition::math::Quaterniond,
323 const std::string &_typeName,
324 const std::string &_valueStr,
325 ParamVariant &_valueToSet)
const;
329 public:
template<
typename T>
330 std::string TypeToString()
const;
338 if constexpr (std::is_same_v<T, bool>)
340 else if constexpr (std::is_same_v<T, char>)
342 else if constexpr (std::is_same_v<T, std::string>)
344 else if constexpr (std::is_same_v<T, int>)
346 else if constexpr (std::is_same_v<T, std::uint64_t>)
348 else if constexpr (std::is_same_v<T, unsigned int>)
349 return "unsigned int";
350 else if constexpr (std::is_same_v<T, double>)
352 else if constexpr (std::is_same_v<T, float>)
354 else if constexpr (std::is_same_v<T, sdf::Time>)
356 else if constexpr (std::is_same_v<T, ignition::math::Angle>)
358 else if constexpr (std::is_same_v<T, ignition::math::Color>)
360 else if constexpr (std::is_same_v<T, ignition::math::Vector2i>)
362 else if constexpr (std::is_same_v<T, ignition::math::Vector2d>)
364 else if constexpr (std::is_same_v<T, ignition::math::Vector3d>)
366 else if constexpr (std::is_same_v<T, ignition::math::Quaterniond>)
368 else if constexpr (std::is_same_v<T, ignition::math::Pose3d>)
378 this->dataPtr->updateFunc = _updateFunc;
387 std::stringstream ss;
389 return this->SetFromString(ss.str());
393 sdferr <<
"Unable to set parameter[" 394 << this->dataPtr->key <<
"]." 395 <<
"Type used must have a stream input and output operator," 396 <<
"which allows proper functioning of Param.\n";
405 T *value = std::get_if<T>(&this->dataPtr->value);
412 std::string typeStr = this->dataPtr->TypeToString<T>();
415 sdferr <<
"Unknown parameter type[" <<
typeid(T).name() <<
"]\n";
419 std::string valueStr = this->GetAsString();
421 bool success = this->dataPtr->ValueFromStringImpl(typeStr, valueStr, pv);
425 _value = std::get<T>(pv);
427 else if (typeStr ==
"bool" && this->dataPtr->typeName ==
"string")
434 std::stringstream tmp;
435 if (valueStr ==
"true" || valueStr ==
"1")
454 std::stringstream ss;
463 sdferr <<
"Unable to convert parameter[" 464 << this->dataPtr->key <<
"] " 466 << this->dataPtr->typeName <<
"], to " 467 <<
"type[" <<
typeid(T).name() <<
"]\n";
475 template<
typename Type>
478 return std::holds_alternative<Type>(this->dataPtr->value);
void SetUpdateFunc(T _updateFunc)
Set the update function.
Definition: Param.hh:376
std::ostream & operator<<(std::ostream &os, ParamStreamer< T > s)
Definition: Param.hh:76
std::shared_ptr< Param > ParamPtr
Definition: Param.hh:58
ParamVariant value
This parameter's value.
Definition: Param.hh:306
friend std::ostream & operator<<(std::ostream &_out, const Param &_p)
Ostream operator.
Definition: Param.hh:253
std::string SDFORMAT_VISIBLE lowercase(const std::string &_in)
Transforms a string to its lowercase equivalent.
std::vector< ParamPtr > Param_V
Definition: Param.hh:62
bool required
True if the parameter is required.
Definition: Param.hh:277
std::function< std::any()> updateFunc
Update function pointer.
Definition: Param.hh:289
std::string TypeToString() const
Data type to string mapping.
Definition: Param.hh:335
std::optional< ParamVariant > maxValue
This parameter's maximum allowed value.
Definition: Param.hh:315
class SDFORMAT_VISIBLE Param
Definition: Param.hh:54
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:303
std::optional< ParamVariant > minValue
This parameter's minimum allowed value.
Definition: Param.hh:312
bool GetDefault(T &_value) const
Get the default value of the parameter.
Definition: Param.hh:452
std::string key
Key value.
Definition: Param.hh:274
#define SDFORMAT_VISIBLE
Use to represent "symbol visible" if supported.
Definition: system_util.hh:25
#define sdferr
Output an error message.
Definition: Console.hh:57
const T & val
Definition: Param.hh:70
std::string GetAsString() const
Get the value as a string.
ParamVariant defaultValue
This parameter's default value.
Definition: Param.hh:309
std::string typeName
Definition: Param.hh:283
bool IsType() const
Return true if the param is a particular type.
Definition: Param.hh:476
ParamStreamer(T) -> ParamStreamer< T >
namespace for Simulation Description Format parser
Definition: Actor.hh:32
bool Get(T &_value) const
Get the value of the parameter.
Definition: Param.hh:403
std::string description
Description of the parameter.
Definition: Param.hh:286
bool Set(const T &_value)
Set the parameter's value.
Definition: Param.hh:383
A parameter class.
Definition: Param.hh:95