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 {
76 std::ostream& operator<<(std::ostream &os, ParamStreamer<T> s)
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;
198 public: ParamPtr Clone()
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,
293 unsigned int, double, float, sdf::Time,
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);
std::optional< ParamVariant > maxValue
This parameter's maximum allowed value.
Definition: Param.hh:312
std::function< std::any()> updateFunc
Update function pointer.
Definition: Param.hh:288
std::variant< bool, char, std::string, int, std::uint64_t, unsigned int, double, float, sdf::Time, ignition::math::Angle, ignition::math::Color, ignition::math::Vector2i, ignition::math::Vector2d, ignition::math::Vector3d, ignition::math::Quaterniond, ignition::math::Pose3d > ParamVariant
Definition: Param.hh:300
ParamStreamer(T) -> ParamStreamer< T >
std::vector< ParamPtr > Param_V
Definition: Param.hh:62
A parameter class.
Definition: Param.hh:95
friend std::ostream & operator<<(std::ostream &_out, const Param &_p)
Ostream operator.
Definition: Param.hh:253
class SDFORMAT_VISIBLE Param
Definition: Param.hh:54
#define SDFORMAT_VISIBLE
Use to represent "symbol visible" if supported.
Definition: system_util.hh:48
#define sdferr
Output an error message.
Definition: Console.hh:57
std::optional< ParamVariant > minValue
This parameter's minimum allowed value.
Definition: Param.hh:309
std::shared_ptr< Param > ParamPtr
Definition: Param.hh:58
std::string GetAsString() const
Get the value as a string.
const T & val
Definition: Param.hh:70
std::string typeName
Definition: Param.hh:282
bool required
True if the parameter is required.
Definition: Param.hh:276
ParamVariant value
This parameter's value.
Definition: Param.hh:303
std::string description
Description of the parameter.
Definition: Param.hh:285
namespace for Simulation Description Format parser
Definition: Actor.hh:32
ParamVariant defaultValue
This parameter's default value.
Definition: Param.hh:306
std::ostream & operator<<(std::ostream &os, ParamStreamer< std::variant< Ts... >> sv)
Definition: Param.hh:83
std::string key
Key value.
Definition: Param.hh:273