18 #ifndef SDFORMAT_PARAM_HH_
19 #define SDFORMAT_PARAM_HH_
36 #include <ignition/math.hh>
39 #include "sdf/sdf_config.h"
47 #pragma warning(disable: 4251)
53 inline namespace SDF_VERSION_NAMESPACE {
87 os << std::setprecision(std::numeric_limits<double>::max_digits10) << s.
val;
94 os << std::setprecision(std::numeric_limits<float>::max_digits10) << s.
val;
102 std::visit([&os](
auto const &v)
121 public:
Param(
const std::string &_key,
const std::string &_typeName,
122 const std::string &_default,
bool _required,
123 const std::string &_description =
"");
135 public:
Param(
const std::string &_key,
const std::string &_typeName,
136 const std::string &_default,
bool _required,
137 const std::string &_minValue,
const std::string &_maxValue,
138 const std::string &_description =
"");
147 public:
Param(
Param &&_param) noexcept =
default;
153 public:
Param &operator=(
const Param &_param);
158 public:
Param &operator=(
Param &&_param) noexcept =
default;
161 public:
virtual ~
Param();
165 public: std::string GetAsString()
const;
169 public: std::string GetDefaultAsString()
const;
175 public: std::optional<std::string> GetMinValueAsString()
const;
181 public: std::optional<std::string> GetMaxValueAsString()
const;
185 public:
bool SetFromString(
const std::string &_value);
188 public:
void Reset();
192 public:
const std::string &GetKey()
const;
197 public:
template<
typename Type>
202 public:
const std::string &GetTypeName()
const;
206 public:
bool GetRequired()
const;
210 public:
bool GetSet()
const;
219 public:
template<
typename T>
220 void SetUpdateFunc(T _updateFunc);
224 public:
void Update();
231 public:
template<
typename T>
232 bool Set(
const T &_value);
237 public:
bool GetAny(std::any &_anyVal)
const;
243 public:
template<
typename T>
244 bool Get(T &_value)
const;
250 public:
template<
typename T>
251 bool GetDefault(T &_value)
const;
255 public:
void SetDescription(
const std::string &_desc);
259 public: std::string GetDescription()
const;
263 public:
bool ValidateValue()
const;
278 private:
bool ValueFromString(
const std::string &_value);
281 private: std::unique_ptr<ParamPrivate> dataPtr;
308 public:
typedef std::variant<bool, char, std::string, int, std::uint64_t,
310 ignition::math::Angle,
311 ignition::math::Color,
312 ignition::math::Vector2i,
313 ignition::math::Vector2d,
314 ignition::math::Vector3d,
315 ignition::math::Quaterniond,
333 void Param::SetUpdateFunc(T _updateFunc)
335 this->dataPtr->updateFunc = _updateFunc;
340 bool Param::Set(
const T &_value)
344 std::stringstream ss;
346 return this->SetFromString(ss.str());
350 sdferr <<
"Unable to set parameter["
351 << this->dataPtr->key <<
"]."
352 <<
"Type used must have a stream input and output operator,"
353 <<
"which allows proper functioning of Param.\n";
360 bool Param::Get(T &_value)
const
364 if (
typeid(T) ==
typeid(
bool) && this->dataPtr->typeName ==
"string")
366 std::string strValue = std::get<std::string>(this->dataPtr->value);
367 std::transform(strValue.begin(), strValue.end(), strValue.begin(),
370 return static_cast<unsigned char>(std::tolower(c));
373 std::stringstream tmp;
374 if (strValue ==
"true" || strValue ==
"1")
386 T *value = std::get_if<T>(&this->dataPtr->value);
391 std::stringstream ss;
399 sdferr <<
"Unable to convert parameter["
400 << this->dataPtr->key <<
"] "
402 << this->dataPtr->typeName <<
"], to "
403 <<
"type[" <<
typeid(T).name() <<
"]\n";
411 bool Param::GetDefault(T &_value)
const
413 std::stringstream ss;
422 sdferr <<
"Unable to convert parameter["
423 << this->dataPtr->key <<
"] "
425 << this->dataPtr->typeName <<
"], to "
426 <<
"type[" <<
typeid(T).name() <<
"]\n";
434 template<
typename Type>
435 bool Param::IsType()
const
437 return std::holds_alternative<Type>(this->dataPtr->value);