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;
216 private:
bool ValueFromString(
const std::string &_value);
219 private: std::unique_ptr<ParamPrivate> dataPtr;
246 public:
typedef std::variant<bool, char, std::string, int, std::uint64_t,
248 ignition::math::Angle,
249 ignition::math::Color,
250 ignition::math::Vector2i,
251 ignition::math::Vector2d,
252 ignition::math::Vector3d,
253 ignition::math::Quaterniond,
265 void Param::SetUpdateFunc(T _updateFunc)
267 this->dataPtr->updateFunc = _updateFunc;
272 bool Param::Set(
const T &_value)
276 std::stringstream ss;
278 return this->SetFromString(ss.str());
282 sdferr <<
"Unable to set parameter["
283 << this->dataPtr->key <<
"]."
284 <<
"Type used must have a stream input and output operator,"
285 <<
"which allows proper functioning of Param.\n";
292 bool Param::Get(T &_value)
const
296 if (
typeid(T) ==
typeid(
bool) && this->dataPtr->typeName ==
"string")
298 std::string strValue = std::get<std::string>(this->dataPtr->value);
299 std::transform(strValue.begin(), strValue.end(), strValue.begin(),
302 return static_cast<unsigned char>(std::tolower(c));
305 std::stringstream tmp;
306 if (strValue ==
"true" || strValue ==
"1")
318 T *value = std::get_if<T>(&this->dataPtr->value);
323 std::stringstream ss;
331 sdferr <<
"Unable to convert parameter["
332 << this->dataPtr->key <<
"] "
334 << this->dataPtr->typeName <<
"], to "
335 <<
"type[" <<
typeid(T).name() <<
"]\n";
343 bool Param::GetDefault(T &_value)
const
345 std::stringstream ss;
354 sdferr <<
"Unable to convert parameter["
355 << this->dataPtr->key <<
"] "
357 << this->dataPtr->typeName <<
"], to "
358 <<
"type[" <<
typeid(T).name() <<
"]\n";
366 template<
typename Type>
367 bool Param::IsType()
const
369 return std::holds_alternative<Type>(this->dataPtr->value);