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 {
78 std::ostream& operator<<(std::ostream &os, ParamStreamer<T> s)
85 inline std::ostream& operator<<(std::ostream &os, ParamStreamer<double> s)
87 os << std::setprecision(std::numeric_limits<double>::max_digits10) << s.val;
92 inline std::ostream& operator<<(std::ostream &os, ParamStreamer<float> s)
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;
214 public: ParamPtr Clone()
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,
309 unsigned int, double, float, sdf::Time,
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);
class SDFORMAT_VISIBLE Param
Definition: Param.hh:56
ParamVariant defaultValue
This parameter's default value.
Definition: Param.hh:322
std::string key
Key value.
Definition: Param.hh:289
ParamStreamer(T) -> ParamStreamer< T >
std::string GetAsString() const
Get the value as a string.
const T & val
Definition: Param.hh:72
ParamVariant value
This parameter's value.
Definition: Param.hh:319
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:316
std::string description
Description of the parameter.
Definition: Param.hh:301
std::function< std::any()> updateFunc
Update function pointer.
Definition: Param.hh:304
#define SDFORMAT_VISIBLE
Use to represent "symbol visible" if supported.
Definition: system_util.hh:41
#define sdferr
Output an error message.
Definition: Console.hh:57
friend std::ostream & operator<<(std::ostream &_out, const Param &_p)
Ostream operator.
Definition: Param.hh:269
namespace for Simulation Description Format parser
Definition: Actor.hh:33
std::ostream & operator<<(std::ostream &os, ParamStreamer< std::variant< Ts... >> sv)
Definition: Param.hh:99
bool required
True if the parameter is required.
Definition: Param.hh:292
std::shared_ptr< Param > ParamPtr
Definition: Param.hh:60
std::vector< ParamPtr > Param_V
Definition: Param.hh:64
std::optional< ParamVariant > maxValue
This parameter's maximum allowed value.
Definition: Param.hh:328
std::optional< ParamVariant > minValue
This parameter's minimum allowed value.
Definition: Param.hh:325
std::string typeName
Definition: Param.hh:298
A parameter class.
Definition: Param.hh:111