Go to the documentation of this file.
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 =
"");
128 public:
const std::string &
GetKey()
const;
133 public:
template<
typename Type>
155 public:
template<
typename T>
156 void SetUpdateFunc(T _updateFunc);
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;
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);
bool GetSet() const
Return true if the parameter has been set.
void Update()
Set the parameter's value using the updateFunc.
std::string GetAsString() const
Get the value as a string.
const T & val
Definition: Param.hh:69
const std::string & GetTypeName() const
Get the type name value.
ParamStreamer(T) -> ParamStreamer< T >
virtual ~Param()
Destructor.
namespace for Simulation Description Format parser
Definition: Actor.hh:33
Param & operator=(const Param &_param)
Equal operator.
std::ostream & operator<<(std::ostream &os, ParamStreamer< T > s)
Definition: Param.hh:75
ParamVariant defaultValue
This parameter's default value.
Definition: Param.hh:260
A parameter class.
Definition: Param.hh:95
std::string GetDefaultAsString() const
Get the default value as a string.
void SetDescription(const std::string &_desc)
Set the description of the parameter.
bool required
True if the parameter is required.
Definition: Param.hh:230
std::shared_ptr< Param > ParamPtr
Definition: Param.hh:57
std::string typeName
Definition: Param.hh:236
#define SDFORMAT_VISIBLE
Use to represent "symbol visible" if supported.
Definition: system_util.hh:48
std::string description
Description of the parameter.
Definition: Param.hh:239
std::vector< ParamPtr > Param_V
Definition: Param.hh:61
bool set
True if the parameter is set.
Definition: Param.hh:233
Param(const std::string &_key, const std::string &_typeName, const std::string &_default, bool _required, const std::string &_description="")
Constructor.
bool GetRequired() const
Return whether the parameter is required.
ParamPtr Clone() const
Clone the parameter.
std::string GetDescription() const
Get the description of the parameter.
const std::string & GetKey() const
Get the key value.
std::function< std::any()> updateFunc
Update function pointer.
Definition: Param.hh:242
bool GetAny(std::any &_anyVal) const
Get the value of the parameter as a std::any.
bool SetFromString(const std::string &_value)
Set the parameter value from a string.
A Time class, can be used to hold wall- or sim-time.
Definition: Types.hh:154
std::string key
Key value.
Definition: Param.hh:227
#define sdferr
Output an error message.
Definition: Console.hh:57
void Reset()
Reset the parameter to the default value.
ParamVariant value
This parameter's value.
Definition: Param.hh:257
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