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 =
"");
109 public:
virtual ~
Param();
113 public: std::string GetAsString()
const;
117 public: std::string GetDefaultAsString()
const;
121 public:
bool SetFromString(
const std::string &_value);
124 public:
void Reset();
128 public:
const std::string &GetKey()
const;
133 public:
template<
typename Type>
138 public:
const std::string &GetTypeName()
const;
142 public:
bool GetRequired()
const;
146 public:
bool GetSet()
const;
155 public:
template<
typename T>
156 void SetUpdateFunc(T _updateFunc);
160 public:
void Update();
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;
193 public:
Param &operator=(
const Param &_param);
197 public:
void SetDescription(
const std::string &_desc);
201 public: std::string GetDescription()
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
285 if constexpr (std::is_same_v<T, bool>)
287 else if constexpr (std::is_same_v<T, char>)
289 else if constexpr (std::is_same_v<T, std::string>)
291 else if constexpr (std::is_same_v<T, int>)
293 else if constexpr (std::is_same_v<T, std::uint64_t>)
295 else if constexpr (std::is_same_v<T, unsigned int>)
296 return "unsigned int";
297 else if constexpr (std::is_same_v<T, double>)
299 else if constexpr (std::is_same_v<T, float>)
301 else if constexpr (std::is_same_v<T, sdf::Time>)
303 else if constexpr (std::is_same_v<T, ignition::math::Angle>)
305 else if constexpr (std::is_same_v<T, ignition::math::Color>)
307 else if constexpr (std::is_same_v<T, ignition::math::Vector2i>)
309 else if constexpr (std::is_same_v<T, ignition::math::Vector2d>)
311 else if constexpr (std::is_same_v<T, ignition::math::Vector3d>)
313 else if constexpr (std::is_same_v<T, ignition::math::Quaterniond>)
315 else if constexpr (std::is_same_v<T, ignition::math::Pose3d>)
323 void Param::SetUpdateFunc(T _updateFunc)
325 this->dataPtr->updateFunc = _updateFunc;
330 bool Param::Set(
const T &_value)
334 std::stringstream ss;
336 return this->SetFromString(ss.str());
340 sdferr <<
"Unable to set parameter["
341 << this->dataPtr->key <<
"]."
342 <<
"Type used must have a stream input and output operator,"
343 <<
"which allows proper functioning of Param.\n";
350 bool Param::Get(T &_value)
const
352 T *value = std::get_if<T>(&this->dataPtr->value);
359 std::string typeStr = this->dataPtr->TypeToString<T>();
362 sdferr <<
"Unknown parameter type[" <<
typeid(T).name() <<
"]\n";
366 std::string valueStr = this->GetAsString();
368 bool success = this->dataPtr->ValueFromStringImpl(typeStr, valueStr, pv);
372 _value = std::get<T>(pv);
374 else if (typeStr ==
"bool" && this->dataPtr->typeName ==
"string")
381 std::stringstream tmp;
382 if (valueStr ==
"true" || valueStr ==
"1")
399 bool Param::GetDefault(T &_value)
const
401 std::stringstream ss;
410 sdferr <<
"Unable to convert parameter["
411 << this->dataPtr->key <<
"] "
413 << this->dataPtr->typeName <<
"], to "
414 <<
"type[" <<
typeid(T).name() <<
"]\n";
422 template<
typename Type>
423 bool Param::IsType()
const
425 return std::holds_alternative<Type>(this->dataPtr->value);