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 <ignition/math.hh>
37 
38 #include "sdf/Console.hh"
39 #include "sdf/sdf_config.h"
40 #include "sdf/system_util.hh"
41 #include "sdf/Types.hh"
42 
43 #ifdef _WIN32
44 // Disable warning C4251 which is triggered by
45 // std::unique_ptr
46 #pragma warning(push)
47 #pragma warning(disable: 4251)
48 #endif
49 
50 namespace sdf
51 {
52  // Inline bracket to help doxygen filtering.
53  inline namespace SDF_VERSION_NAMESPACE {
54  //
55 
57 
60  typedef std::shared_ptr<Param> ParamPtr;
61 
64  typedef std::vector<ParamPtr> Param_V;
65 
67  class ParamPrivate;
68 
69  template<class T>
71  {
72  const T &val;
73  };
74 
75  template<class T> ParamStreamer(T) -> ParamStreamer<T>;
76 
77  template<class T>
78  std::ostream& operator<<(std::ostream &os, ParamStreamer<T> s)
79  {
80  os << s.val;
81  return os;
82  }
83 
84  template<>
85  inline std::ostream& operator<<(std::ostream &os, ParamStreamer<double> s)
86  {
87  os << std::setprecision(std::numeric_limits<double>::max_digits10) << s.val;
88  return os;
89  }
90 
91  template<>
92  inline std::ostream& operator<<(std::ostream &os, ParamStreamer<float> s)
93  {
94  os << std::setprecision(std::numeric_limits<float>::max_digits10) << s.val;
95  return os;
96  }
97 
98  template<class... Ts>
99  std::ostream& operator<<(std::ostream& os,
100  ParamStreamer<std::variant<Ts...>> sv)
101  {
102  std::visit([&os](auto const &v)
103  {
104  os << ParamStreamer{v};
105  }, sv.val);
106  return os;
107  }
108 
112  {
121  public: Param(const std::string &_key, const std::string &_typeName,
122  const std::string &_default, bool _required,
123  const std::string &_description = "");
124 
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 = "");
139 
143  public: Param(const Param &_param);
144 
147  public: Param(Param &&_param) noexcept = default;
148 
153  public: Param &operator=(const Param &_param);
154 
158  public: Param &operator=(Param &&_param) noexcept = default;
159 
161  public: virtual ~Param();
162 
165  public: std::string GetAsString() const;
166 
169  public: std::string GetDefaultAsString() const;
170 
175  public: std::optional<std::string> GetMinValueAsString() const;
176 
181  public: std::optional<std::string> GetMaxValueAsString() const;
182 
185  public: bool SetFromString(const std::string &_value);
186 
188  public: void Reset();
189 
192  public: const std::string &GetKey() const;
193 
197  public: template<typename Type>
198  bool IsType() const;
199 
202  public: const std::string &GetTypeName() const;
203 
206  public: bool GetRequired() const;
207 
210  public: bool GetSet() const;
211 
214  public: ParamPtr Clone() const;
215 
219  public: template<typename T>
220  void SetUpdateFunc(T _updateFunc);
221 
224  public: void Update();
225 
231  public: template<typename T>
232  bool Set(const T &_value);
233 
237  public: bool GetAny(std::any &_anyVal) const;
238 
243  public: template<typename T>
244  bool Get(T &_value) const;
245 
250  public: template<typename T>
251  bool GetDefault(T &_value) const;
252 
255  public: void SetDescription(const std::string &_desc);
256 
259  public: std::string GetDescription() const;
260 
263  public: bool ValidateValue() const;
264 
269  public: friend std::ostream &operator<<(std::ostream &_out,
270  const Param &_p)
271  {
272  _out << _p.GetAsString();
273  return _out;
274  }
275 
278  private: bool ValueFromString(const std::string &_value);
279 
281  private: std::unique_ptr<ParamPrivate> dataPtr;
282  };
283 
287  {
289  public: std::string key;
290 
292  public: bool required;
293 
295  public: bool set;
296 
298  public: std::string typeName;
299 
301  public: std::string description;
302 
304  public: std::function<std::any ()> updateFunc;
305 
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,
316  ignition::math::Pose3d> ParamVariant;
317 
320 
323 
325  public: std::optional<ParamVariant> minValue;
326 
328  public: std::optional<ParamVariant> maxValue;
329  };
330 
332  template<typename T>
333  void Param::SetUpdateFunc(T _updateFunc)
334  {
335  this->dataPtr->updateFunc = _updateFunc;
336  }
337 
339  template<typename T>
340  bool Param::Set(const T &_value)
341  {
342  try
343  {
344  std::stringstream ss;
345  ss << _value;
346  return this->SetFromString(ss.str());
347  }
348  catch(...)
349  {
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";
354  return false;
355  }
356  }
357 
359  template<typename T>
360  bool Param::Get(T &_value) const
361  {
362  try
363  {
364  if (typeid(T) == typeid(bool) && this->dataPtr->typeName == "string")
365  {
366  std::string strValue = std::get<std::string>(this->dataPtr->value);
367  std::transform(strValue.begin(), strValue.end(), strValue.begin(),
368  [](unsigned char c)
369  {
370  return static_cast<unsigned char>(std::tolower(c));
371  });
372 
373  std::stringstream tmp;
374  if (strValue == "true" || strValue == "1")
375  {
376  tmp << "1";
377  }
378  else
379  {
380  tmp << "0";
381  }
382  tmp >> _value;
383  }
384  else
385  {
386  T *value = std::get_if<T>(&this->dataPtr->value);
387  if (value)
388  _value = *value;
389  else
390  {
391  std::stringstream ss;
392  ss << ParamStreamer{this->dataPtr->value};
393  ss >> _value;
394  }
395  }
396  }
397  catch(...)
398  {
399  sdferr << "Unable to convert parameter["
400  << this->dataPtr->key << "] "
401  << "whose type is["
402  << this->dataPtr->typeName << "], to "
403  << "type[" << typeid(T).name() << "]\n";
404  return false;
405  }
406  return true;
407  }
408 
410  template<typename T>
411  bool Param::GetDefault(T &_value) const
412  {
413  std::stringstream ss;
414 
415  try
416  {
417  ss << ParamStreamer{this->dataPtr->defaultValue};
418  ss >> _value;
419  }
420  catch(...)
421  {
422  sdferr << "Unable to convert parameter["
423  << this->dataPtr->key << "] "
424  << "whose type is["
425  << this->dataPtr->typeName << "], to "
426  << "type[" << typeid(T).name() << "]\n";
427  return false;
428  }
429 
430  return true;
431  }
432 
434  template<typename Type>
435  bool Param::IsType() const
436  {
437  return std::holds_alternative<Type>(this->dataPtr->value);
438  }
439  }
440 }
441 
442 #ifdef _WIN32
443 #pragma warning(pop)
444 #endif
445 
446 #endif
sdf::v11::ParamStreamer
Definition: Param.hh:70
sdf::v11::ParamPrivate::required
bool required
True if the parameter is required.
Definition: Param.hh:292
sdf::v11::ParamPrivate::set
bool set
True if the parameter is set.
Definition: Param.hh:295
sdf::v11::ParamPrivate::defaultValue
ParamVariant defaultValue
This parameter's default value.
Definition: Param.hh:322
sdf::v11::ParamPtr
std::shared_ptr< Param > ParamPtr
Definition: Param.hh:60
sdf::v11::ParamPrivate::minValue
std::optional< ParamVariant > minValue
This parameter's minimum allowed value.
Definition: Param.hh:325
sdf::v11::ParamPrivate::typeName
std::string typeName
Definition: Param.hh:298
sdf
namespace for Simulation Description Format parser
Definition: Actor.hh:33
sdf::v11::ParamStreamer::val
const T & val
Definition: Param.hh:72
Console.hh
sdf::v11::ParamPrivate
Definition: Param.hh:286
sdf::v11::ParamPrivate::ParamVariant
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
sdf::v11::ParamPrivate::description
std::string description
Description of the parameter.
Definition: Param.hh:301
sdf::v11::ParamPrivate::value
ParamVariant value
This parameter's value.
Definition: Param.hh:319
Types.hh
sdf::v11::Param_V
std::vector< ParamPtr > Param_V
Definition: Param.hh:64
SDFORMAT_VISIBLE
#define SDFORMAT_VISIBLE
Definition: system_util.hh:41
sdf::v11::Param
A parameter class.
Definition: Param.hh:111
sdf::v11::ParamStreamer
ParamStreamer(T) -> ParamStreamer< T >
sdf::v11::Time
A Time class, can be used to hold wall- or sim-time.
Definition: Types.hh:177
sdf::v11::ParamPrivate::updateFunc
std::function< std::any()> updateFunc
Update function pointer.
Definition: Param.hh:304
sdf::v11::Param::operator<<
friend std::ostream & operator<<(std::ostream &_out, const Param &_p)
Ostream operator.
Definition: Param.hh:269
sdf::v11::Param
class SDFORMAT_VISIBLE Param
Definition: Param.hh:56
sdf::v11::Param::GetAsString
std::string GetAsString() const
Get the value as a string.
system_util.hh
sdf::v11::ParamPrivate::key
std::string key
Key value.
Definition: Param.hh:289
sdf::v11::operator<<
std::ostream & operator<<(std::ostream &os, ParamStreamer< T > s)
Definition: Param.hh:78
sdferr
#define sdferr
Output an error message.
Definition: Console.hh:57
sdf::v11::ParamPrivate::maxValue
std::optional< ParamVariant > maxValue
This parameter's maximum allowed value.
Definition: Param.hh:328