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 {
75 std::ostream& operator<<(std::ostream &os, ParamStreamer<T> s)
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;
150 public: ParamPtr Clone()
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,
247 unsigned int, double, float, sdf::Time,
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);
A parameter class.
Definition: Param.hh:94
std::function< std::any()> updateFunc
Update function pointer.
Definition: Param.hh:242
const T & val
Definition: Param.hh:69
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:254
friend std::ostream & operator<<(std::ostream &_out, const Param &_p)
Ostream operator.
Definition: Param.hh:207
ParamStreamer(T) -> ParamStreamer< T >
std::ostream & operator<<(std::ostream &os, ParamStreamer< std::variant< Ts... >> sv)
Definition: Param.hh:82
std::string typeName
Definition: Param.hh:236
ParamVariant value
This parameter's value.
Definition: Param.hh:257
std::string GetAsString() const
Get the value as a string.
class SDFORMAT_VISIBLE Param
Definition: Param.hh:53
#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::string key
Key value.
Definition: Param.hh:227
std::string description
Description of the parameter.
Definition: Param.hh:239
std::vector< ParamPtr > Param_V
Definition: Param.hh:61
namespace for Simulation Description Format parser
Definition: AirPressure.hh:25
ParamVariant defaultValue
This parameter's default value.
Definition: Param.hh:260
bool required
True if the parameter is required.
Definition: Param.hh:230
std::shared_ptr< Param > ParamPtr
Definition: Param.hh:57