18 #ifndef SDFORMAT_PARAM_HH_
19 #define SDFORMAT_PARAM_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,
256 gz::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, gz::math::Angle>)
306 else if constexpr (std::is_same_v<T, gz::math::Color>)
308 else if constexpr (std::is_same_v<T, gz::math::Vector2i>)
310 else if constexpr (std::is_same_v<T, gz::math::Vector2d>)
312 else if constexpr (std::is_same_v<T, gz::math::Vector3d>)
314 else if constexpr (std::is_same_v<T, gz::math::Quaterniond>)
316 else if constexpr (std::is_same_v<T, gz::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);