Loading...
Searching...
No Matches
Param.hh
Go to the documentation of this file.
1/*
2 * Copyright 2012 Open Source Robotics Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16*/
17
18#ifndef SDFORMAT_PARAM_HH_
19#define SDFORMAT_PARAM_HH_
20
21#include <any>
22#include <algorithm>
23#include <cctype>
24#include <cstdint>
25#include <functional>
26#include <iomanip>
27#include <limits>
28#include <memory>
29#include <optional>
30#include <sstream>
31#include <string>
32#include <typeinfo>
33#include <variant>
34#include <vector>
35
36#include <gz/math/Angle.hh>
37#include <gz/math/Color.hh>
38#include <gz/math/Pose3.hh>
39#include <gz/math/Quaternion.hh>
40#include <gz/math/Vector2.hh>
41#include <gz/math/Vector3.hh>
42
43#include "sdf/Console.hh"
44#include "sdf/PrintConfig.hh"
45#include "sdf/config.hh"
46#include "sdf/system_util.hh"
47#include "sdf/Types.hh"
48
49#ifdef _WIN32
50// Disable warning C4251 which is triggered by
51// std::unique_ptr
52#pragma warning(push)
53#pragma warning(disable: 4251)
54#endif
55
56namespace sdf
57{
58 // Inline bracket to help doxygen filtering.
59 inline namespace SDF_VERSION_NAMESPACE {
60 //
61
63 using ElementPtr = std::shared_ptr<Element>;
64 using ElementWeakPtr = std::weak_ptr<Element>;
65
67
70 typedef std::shared_ptr<Param> ParamPtr;
71
74 typedef std::vector<ParamPtr> Param_V;
75
77 class ParamPrivate;
78
79 template<class T>
81 {
82 const T &val;
83 const int precision; // Used to set std::ostream's std::setprecision
84 explicit ParamStreamer(const T &_val, int _precision = 0)
85 : val(_val), precision(_precision) {}
86 };
87
88 // Template deduction guide for ParamVariant
89 template<typename ParamVariant>
90 ParamStreamer(const ParamVariant &_val, int _precision)
92
93 template<class T>
94 std::ostream& operator<<(std::ostream &os, ParamStreamer<T> s)
95 {
96 if (s.precision == std::numeric_limits<int>::max())
97 {
98 if constexpr (std::is_same_v<T, double>
99 || std::is_same_v<T, gz::math::Angle>
100 || std::is_same_v<T, gz::math::Vector2d>
101 || std::is_same_v<T, gz::math::Vector3d>
102 || std::is_same_v<T, gz::math::Quaterniond>
103 || std::is_same_v<T, gz::math::Pose3d>)
104 {
105 os << std::setprecision(std::numeric_limits<double>::max_digits10);
106 }
107 else if constexpr (std::is_same_v<T, float>
108 || std::is_same_v<T, gz::math::Color>)
109 {
110 os << std::setprecision(std::numeric_limits<float>::max_digits10);
111 }
112 }
113 else
114 {
115 os << std::setprecision(s.precision);
116 }
117
118 os << s.val;
119 return os;
120 }
121
122 template<class... Ts>
123 std::ostream& operator<<(std::ostream& os,
124 ParamStreamer<std::variant<Ts...>> sv)
125 {
126 std::visit([&os, &sv](auto const &v)
127 {
128 os << ParamStreamer{v, sv.precision};
129 }, sv.val);
130 return os;
131 }
132
136 {
145 public: Param(const std::string &_key, const std::string &_typeName,
146 const std::string &_default, bool _required,
147 const std::string &_description = "");
148
157 public: Param(const std::string &_key, const std::string &_typeName,
158 const std::string &_default, bool _required,
159 sdf::Errors &_errors,
160 const std::string &_description = "");
161
172 public: Param(const std::string &_key, const std::string &_typeName,
173 const std::string &_default, bool _required,
174 const std::string &_minValue, const std::string &_maxValue,
175 const std::string &_description = "");
176
187 public: Param(const std::string &_key, const std::string &_typeName,
188 const std::string &_default, bool _required,
189 const std::string &_minValue, const std::string &_maxValue,
190 sdf::Errors &_errors,
191 const std::string &_description = "");
192
196 public: Param(const Param &_param);
197
200 public: Param(Param &&_param) noexcept = default;
201
206 public: Param &operator=(const Param &_param);
207
211 public: Param &operator=(Param &&_param) noexcept = default;
212
214 public: virtual ~Param();
215
219 public: std::string GetAsString(
220 const PrintConfig &_config = PrintConfig()) const;
221
226 public: std::string GetAsString(
227 sdf::Errors &_errors,
228 const PrintConfig &_config = PrintConfig()) const;
229
233 public: std::string GetDefaultAsString(
234 const PrintConfig &_config = PrintConfig()) const;
235
240 public: std::string GetDefaultAsString(
241 sdf::Errors &_errors,
242 const PrintConfig &_config = PrintConfig()) const;
243
249 public: std::optional<std::string> GetMinValueAsString(
250 const PrintConfig &_config = PrintConfig()) const;
251
258 public: std::optional<std::string> GetMinValueAsString(
259 sdf::Errors &_errors,
260 const PrintConfig &_config = PrintConfig()) const;
261
267 public: std::optional<std::string> GetMaxValueAsString(
268 const PrintConfig &_config = PrintConfig()) const;
269
276 public: std::optional<std::string> GetMaxValueAsString(
277 sdf::Errors &_errors,
278 const PrintConfig &_config = PrintConfig()) const;
279
285 public: bool SetFromString(const std::string &_value,
286 bool _ignoreParentAttributes);
287
294 public: bool SetFromString(const std::string &_value,
295 bool _ignoreParentAttributes,
296 sdf::Errors &_errors);
297
300 public: bool SetFromString(const std::string &_value);
301
305 public: bool SetFromString(const std::string &_value,
306 sdf::Errors &_errors);
307
312
318 public: bool SetParentElement(ElementPtr _parentElement);
319
326 public: bool SetParentElement(ElementPtr _parentElement,
327 sdf::Errors &_errors);
328
335 ElementPtr _parentElement);
336
338 public: void Reset();
339
351 public: bool Reparse();
352
365 public: bool Reparse(sdf::Errors &_errors);
366
369 public: const std::string &GetKey() const;
370
374 public: template<typename Type>
375 bool IsType() const;
376
379 public: const std::string &GetTypeName() const;
380
383 public: bool GetRequired() const;
384
387 public: bool GetSet() const;
388
393 public: bool IgnoresParentElementAttribute() const;
394
397 public: ParamPtr Clone() const;
398
402 public: template<typename T>
403 void SetUpdateFunc(T _updateFunc);
404
407 public: void Update();
408
412 public: void Update(sdf::Errors &_errors);
413
419 public: template<typename T>
420 bool Set(const T &_value);
421
428 public: template<typename T>
429 bool Set(const T &_value,
430 sdf::Errors &_errors);
431
435 public: bool GetAny(std::any &_anyVal) const;
436
441 public: bool GetAny(std::any &_anyVal, sdf::Errors &_errors) const;
442
447 public: template<typename T>
448 bool Get(T &_value) const;
449
455 public: template<typename T>
456 bool Get(T &_value,
457 sdf::Errors &_errors) const;
458
463 public: template<typename T>
464 bool GetDefault(T &_value) const;
465
471 public: template<typename T>
472 bool GetDefault(T &_value,
473 sdf::Errors &_errors) const;
474
477 public: void SetDescription(const std::string &_desc);
478
481 public: std::string GetDescription() const;
482
485 public: bool ValidateValue() const;
486
490 public: bool ValidateValue(sdf::Errors &_errors) const;
491
496 public: friend std::ostream &operator<<(std::ostream &_out,
497 const Param &_p)
498 {
499 _out << _p.GetAsString();
500 return _out;
501 }
502
504 private: std::unique_ptr<ParamPrivate> dataPtr;
505 };
506
510 {
512 public: std::string key;
513
515 public: bool required;
516
518 public: bool set;
519
521 public: std::string typeName;
522
524 public: std::string description;
525
528
530 public: std::function<std::any ()> updateFunc;
531
537 public: typedef std::variant<bool, char, std::string, int, std::uint64_t,
538 unsigned int, double, float, sdf::Time,
539 gz::math::Angle,
540 gz::math::Color,
541 gz::math::Vector2i,
542 gz::math::Vector2d,
543 gz::math::Vector3d,
544 gz::math::Quaterniond,
545 gz::math::Pose3d> ParamVariant;
546
549
554
556 public: std::optional<std::string> strValue;
557
559 public: std::string defaultStrValue;
560
563
565 public: std::optional<ParamVariant> minValue;
566
568 public: std::optional<ParamVariant> maxValue;
569
578 public: void Init(const std::string &_key, const std::string &_typeName,
579 const std::string &_default, bool _required,
580 sdf::Errors &_errors,
581 const std::string &_description);
582
593 public: void Init(const std::string &_key, const std::string &_typeName,
594 const std::string &_default, bool _required,
595 const std::string &_minValue, const std::string &_maxValue,
596 sdf::Errors &_errors,
597 const std::string &_description);
598
606 const std::string &_typeName,
607 const std::string &_valueStr,
608 ParamVariant &_valueToSet,
609 sdf::Errors &_errors) const;
610
620 const PrintConfig &_config,
621 const std::string &_typeName,
622 const ParamVariant &_value,
623 std::string &_valueStr,
624 sdf::Errors &_errors) const;
625
628 public: template<typename T>
629 static std::string TypeToString();
630 };
631
633 template<typename T>
635 {
636 // cppcheck-suppress syntaxError
637 if constexpr (std::is_same_v<T, bool>)
638 return "bool";
639 else if constexpr (std::is_same_v<T, char>)
640 return "char";
641 else if constexpr (std::is_same_v<T, std::string>)
642 return "string";
643 else if constexpr (std::is_same_v<T, int>)
644 return "int";
645 else if constexpr (std::is_same_v<T, std::uint64_t>)
646 return "uint64_t";
647 else if constexpr (std::is_same_v<T, unsigned int>)
648 return "unsigned int";
649 else if constexpr (std::is_same_v<T, double>)
650 return "double";
651 else if constexpr (std::is_same_v<T, float>)
652 return "float";
653 else if constexpr (std::is_same_v<T, sdf::Time>)
654 return "time";
655 else if constexpr (std::is_same_v<T, gz::math::Angle>)
656 return "angle";
657 else if constexpr (std::is_same_v<T, gz::math::Color>)
658 return "color";
659 else if constexpr (std::is_same_v<T, gz::math::Vector2i>)
660 return "vector2i";
661 else if constexpr (std::is_same_v<T, gz::math::Vector2d>)
662 return "vector2d";
663 else if constexpr (std::is_same_v<T, gz::math::Vector3d>)
664 return "vector3";
665 else if constexpr (std::is_same_v<T, gz::math::Quaterniond>)
666 return "quaternion";
667 else if constexpr (std::is_same_v<T, gz::math::Pose3d>)
668 return "pose";
669 else
670 return "";
671 }
672
674 template<typename T>
675 void Param::SetUpdateFunc(T _updateFunc)
676 {
677 this->dataPtr->updateFunc = _updateFunc;
678 }
679
681 template<typename T>
682 bool Param::Set(const T &_value)
683 {
684 sdf::Errors errors;
685 bool result = this->Set<T>(_value, errors);
686 if (!errors.empty())
687 sdferr << errors;
688 return result;
689 }
690
692 template<typename T>
693 bool Param::Set(const T &_value, sdf::Errors &_errors)
694 {
695 try
696 {
697 std::stringstream ss;
698 ss << ParamStreamer<T>{_value, std::numeric_limits<int>::max()};
699 return this->SetFromString(ss.str(), true, _errors);
700 }
701 catch(...)
702 {
703 _errors.push_back({ErrorCode::PARAMETER_ERROR,
704 "Unable to set parameter["
705 + this->dataPtr->key + "]."
706 + "Type used must have a stream input and output operator,"
707 + "which allows proper functioning of Param."});
708 return false;
709 }
710 }
711
713 template<typename T>
714 bool Param::Get(T &_value) const
715 {
716 sdf::Errors errors;
717 bool result = this->Get<T>(_value, errors);
718 if (!errors.empty())
719 sdferr << errors;
720 return result;
721 }
722
724 template<typename T>
725 bool Param::Get(T &_value, sdf::Errors &_errors) const
726 {
727 T *value = std::get_if<T>(&this->dataPtr->value);
728 if (value)
729 {
730 _value = *value;
731 }
732 else
733 {
734 std::string typeStr = this->dataPtr->TypeToString<T>();
735 if (typeStr.empty())
736 {
737 _errors.push_back({ErrorCode::UNKNOWN_PARAMETER_TYPE,
738 "Unknown parameter type[" + std::string(typeid(T).name()) + "]"});
739 return false;
740 }
741
742 std::string valueStr = this->GetAsString(_errors);
744 bool success = this->dataPtr->ValueFromStringImpl(
745 typeStr, valueStr, pv, _errors);
746
747 if (success)
748 {
749 _value = std::get<T>(pv);
750 }
751
752 return success;
753 }
754
755 return true;
756 }
757
759 template<typename T>
760 bool Param::GetDefault(T &_value) const
761 {
762 sdf::Errors errors;
763 bool result = this>GetDefault<T>(_value, errors);
764 if (!errors.empty())
765 sdferr << errors;
766 return result;
767 }
768
770 template<typename T>
771 bool Param::GetDefault(T &_value, sdf::Errors &_errors) const
772 {
773 std::stringstream ss;
774
775 try
776 {
777 ss << ParamStreamer{this->dataPtr->defaultValue,
778 std::numeric_limits<int>::max()};
779 ss >> _value;
780 }
781 catch(...)
782 {
783 _errors.push_back({ErrorCode::PARAMETER_ERROR,
784 "Unable to convert parameter["
785 + this->dataPtr->key + "] "
786 + "whose type is["
787 + this->dataPtr->typeName + "], to "
788 + "type[" + typeid(T).name() + "]"});
789 return false;
790 }
791
792 return true;
793 }
794
796 template<typename Type>
797 bool Param::IsType() const
798 {
799 return std::holds_alternative<Type>(this->dataPtr->value);
800 }
801 }
802}
803
804#ifdef _WIN32
805#pragma warning(pop)
806#endif
807
808#endif
Definition Param.hh:510
std::variant< bool, char, std::string, int, std::uint64_t, unsigned int, double, float, sdf::Time, gz::math::Angle, gz::math::Color, gz::math::Vector2i, gz::math::Vector2d, gz::math::Vector3d, gz::math::Quaterniond, gz::math::Pose3d > ParamVariant
Definition Param.hh:545
void Init(const std::string &_key, const std::string &_typeName, const std::string &_default, bool _required, sdf::Errors &_errors, const std::string &_description)
Initializer function to help Param constructors.
static std::string TypeToString()
Data type to string mapping.
Definition Param.hh:634
bool set
True if the parameter is set.
Definition Param.hh:518
bool required
True if the parameter is required.
Definition Param.hh:515
bool GZ_SDFORMAT_VISIBLE ValueFromStringImpl(const std::string &_typeName, const std::string &_valueStr, ParamVariant &_valueToSet, sdf::Errors &_errors) const
Method used to set the Param from a passed-in string.
ParamVariant defaultValue
This parameter's default value.
Definition Param.hh:562
ElementWeakPtr parentElement
Parent element.
Definition Param.hh:527
std::function< std::any()> updateFunc
Update function pointer.
Definition Param.hh:530
std::string defaultStrValue
This parameter's default value that was provided as a string.
Definition Param.hh:559
std::string typeName
Definition Param.hh:521
void Init(const std::string &_key, const std::string &_typeName, const std::string &_default, bool _required, const std::string &_minValue, const std::string &_maxValue, sdf::Errors &_errors, const std::string &_description)
Initializer function to help Param constructors.
bool StringFromValueImpl(const PrintConfig &_config, const std::string &_typeName, const ParamVariant &_value, std::string &_valueStr, sdf::Errors &_errors) const
Method used to get the string representation from a ParamVariant, or the string that was used to set ...
bool ignoreParentAttributes
True if the value has been parsed while ignoring its parent element's attributes, and will continue t...
Definition Param.hh:553
ParamVariant value
This parameter's value.
Definition Param.hh:548
std::string key
Key value.
Definition Param.hh:512
std::optional< ParamVariant > maxValue
This parameter's maximum allowed value.
Definition Param.hh:568
std::optional< ParamVariant > minValue
This parameter's minimum allowed value.
Definition Param.hh:565
std::optional< std::string > strValue
This parameter's value that was provided as a string.
Definition Param.hh:556
std::string description
Description of the parameter.
Definition Param.hh:524
A parameter class.
Definition Param.hh:136
Param(Param &&_param) noexcept=default
Move constructor.
bool SetFromString(const std::string &_value, bool _ignoreParentAttributes)
Set the parameter value from a string.
ElementPtr GetParentElement() const
Get the parent Element of this Param.
Param & operator=(Param &&_param) noexcept=default
Move assignment operator.
std::string GetAsString(const PrintConfig &_config=PrintConfig()) const
Get the value as a string.
void SetDescription(const std::string &_desc)
Set the description of the parameter.
bool SetParentElementNoReparse(ElementPtr _parentElement)
Set the parent Element of this Param without reparsing.
std::optional< std::string > GetMaxValueAsString(sdf::Errors &_errors, const PrintConfig &_config=PrintConfig()) const
Get the maximum allowed value as a string.
Param(const std::string &_key, const std::string &_typeName, const std::string &_default, bool _required, const std::string &_minValue, const std::string &_maxValue, const std::string &_description="")
Constructor with min and max values.
void Update()
Set the parameter's value using the updateFunc.
Param(const std::string &_key, const std::string &_typeName, const std::string &_default, bool _required, const std::string &_minValue, const std::string &_maxValue, sdf::Errors &_errors, const std::string &_description="")
Constructor with min and max values.
std::string GetAsString(sdf::Errors &_errors, const PrintConfig &_config=PrintConfig()) const
Get the value as a string.
bool Set(const T &_value)
Set the parameter's value.
Definition Param.hh:682
bool Reparse(sdf::Errors &_errors)
Reparse the parameter value.
void SetUpdateFunc(T _updateFunc)
Set the update function.
Definition Param.hh:675
std::string GetDefaultAsString(const PrintConfig &_config=PrintConfig()) const
Get the default value as a string.
bool SetFromString(const std::string &_value, bool _ignoreParentAttributes, sdf::Errors &_errors)
Set the parameter value from a string.
bool SetParentElement(ElementPtr _parentElement, sdf::Errors &_errors)
Set the parent Element of this Param.
ParamPtr Clone() const
Clone the parameter.
Param(const Param &_param)
Copy constructor Note that the updateFunc member does not get copied.
Param & operator=(const Param &_param)
Copy assignment operator Note that the updateFunc member will not get copied.
friend std::ostream & operator<<(std::ostream &_out, const Param &_p)
Ostream operator.
Definition Param.hh:496
const std::string & GetTypeName() const
Get the type name value.
bool SetFromString(const std::string &_value, sdf::Errors &_errors)
Set the parameter value from a string.
bool GetAny(std::any &_anyVal, sdf::Errors &_errors) const
Get the value of the parameter as a std::any.
Param(const std::string &_key, const std::string &_typeName, const std::string &_default, bool _required, const std::string &_description="")
Constructor.
std::optional< std::string > GetMinValueAsString(sdf::Errors &_errors, const PrintConfig &_config=PrintConfig()) const
Get the minimum allowed value as a string.
std::string GetDescription() const
Get the description of the parameter.
std::string GetDefaultAsString(sdf::Errors &_errors, const PrintConfig &_config=PrintConfig()) const
Get the default value as a string.
bool GetSet() const
Return true if the parameter has been set.
bool SetParentElement(ElementPtr _parentElement)
Set the parent Element of this Param.
Param(const std::string &_key, const std::string &_typeName, const std::string &_default, bool _required, sdf::Errors &_errors, const std::string &_description="")
Constructor.
void Update(sdf::Errors &_errors)
Set the parameter's value using the updateFunc.
std::optional< std::string > GetMinValueAsString(const PrintConfig &_config=PrintConfig()) const
Get the minimum allowed value as a string.
const std::string & GetKey() const
Get the key value.
bool ValidateValue() const
Validate the value against minimum and maximum allowed values.
bool Get(T &_value) const
Get the value of the parameter.
Definition Param.hh:714
bool Reparse()
Reparse the parameter value.
bool IsType() const
Return true if the param is a particular type.
Definition Param.hh:797
bool GetAny(std::any &_anyVal) const
Get the value of the parameter as a std::any.
bool IgnoresParentElementAttribute() const
Return true if the parameter ignores the parent element's attributes, or if the parameter has no pare...
void Reset()
Reset the parameter to the default value.
bool GetDefault(T &_value) const
Get the default value of the parameter.
Definition Param.hh:760
std::optional< std::string > GetMaxValueAsString(const PrintConfig &_config=PrintConfig()) const
Get the maximum allowed value as a string.
bool GetRequired() const
Return whether the parameter is required.
bool SetFromString(const std::string &_value)
Set the parameter value from a string.
bool ValidateValue(sdf::Errors &_errors) const
Validate the value against minimum and maximum allowed values.
This class contains configuration options for printing elements.
Definition PrintConfig.hh:33
A Time class, can be used to hold wall- or sim-time.
Definition Types.hh:92
#define sdferr
Output an error message.
Definition Console.hh:57
class GZ_SDFORMAT_VISIBLE Element
Definition Element.hh:51
std::shared_ptr< Param > ParamPtr
Definition Param.hh:70
std::vector< ParamPtr > Param_V
Definition Param.hh:74
std::ostream & operator<<(std::ostream &os, ParamStreamer< T > s)
Definition Param.hh:94
@ PARAMETER_ERROR
Generic error type for parameters (values of SDFormat elements or attributes).
@ UNKNOWN_PARAMETER_TYPE
The specified parameter (values of SDFormat elements or attributes) type is unknown.
std::vector< Error > Errors
A vector of Error.
Definition Types.hh:80
std::weak_ptr< Element > ElementWeakPtr
Definition Element.hh:63
std::shared_ptr< Element > ElementPtr
Definition Element.hh:55
namespace for Simulation Description Format parser
Definition Actor.hh:35
Definition Param.hh:81
const int precision
Definition Param.hh:83
const T & val
Definition Param.hh:82
ParamStreamer(const T &_val, int _precision=0)
Definition Param.hh:84
#define SDFORMAT_VISIBLE
Use to represent "symbol visible" if supported.
Definition system_util.hh:25