18 #ifndef SDFORMAT_PARAM_HH_
19 #define SDFORMAT_PARAM_HH_
34 #include <ignition/math.hh>
37 #include "sdf/sdf_config.h"
45 #pragma warning(disable: 4251)
51 inline namespace SDF_VERSION_NAMESPACE {
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;
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;
262 private:
bool ValueFromString(
const std::string &_value);
265 private: std::unique_ptr<ParamPrivate> dataPtr;
292 public:
typedef std::variant<bool, char, std::string, int, std::uint64_t,
294 ignition::math::Angle,
295 ignition::math::Color,
296 ignition::math::Vector2i,
297 ignition::math::Vector2d,
298 ignition::math::Vector3d,
299 ignition::math::Quaterniond,
317 void Param::SetUpdateFunc(T _updateFunc)
319 this->dataPtr->updateFunc = _updateFunc;
324 bool Param::Set(
const T &_value)
328 std::stringstream ss;
330 return this->SetFromString(ss.str());
334 sdferr <<
"Unable to set parameter["
335 << this->dataPtr->key <<
"]."
336 <<
"Type used must have a stream input and output operator,"
337 <<
"which allows proper functioning of Param.\n";
344 bool Param::Get(T &_value)
const
348 if (
typeid(T) ==
typeid(
bool) && this->dataPtr->typeName ==
"string")
350 std::string strValue = std::get<std::string>(this->dataPtr->value);
351 std::transform(strValue.begin(), strValue.end(), strValue.begin(),
354 return static_cast<unsigned char>(std::tolower(c));
357 std::stringstream tmp;
358 if (strValue ==
"true" || strValue ==
"1")
370 T *value = std::get_if<T>(&this->dataPtr->value);
375 std::stringstream ss;
383 sdferr <<
"Unable to convert parameter["
384 << this->dataPtr->key <<
"] "
386 << this->dataPtr->typeName <<
"], to "
387 <<
"type[" <<
typeid(T).name() <<
"]\n";
395 bool Param::GetDefault(T &_value)
const
397 std::stringstream ss;
406 sdferr <<
"Unable to convert parameter["
407 << this->dataPtr->key <<
"] "
409 << this->dataPtr->typeName <<
"], to "
410 <<
"type[" <<
typeid(T).name() <<
"]\n";
418 template<
typename Type>
419 bool Param::IsType()
const
421 return std::holds_alternative<Type>(this->dataPtr->value);