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 <memory>
27 #include <optional>
28 #include <sstream>
29 #include <string>
30 #include <typeinfo>
31 #include <variant>
32 #include <vector>
33 
34 #include <ignition/math.hh>
35 
36 #include "sdf/Console.hh"
37 #include "sdf/sdf_config.h"
38 #include "sdf/system_util.hh"
39 #include "sdf/Types.hh"
40 
41 #ifdef _WIN32
42 // Disable warning C4251 which is triggered by
43 // std::unique_ptr
44 #pragma warning(push)
45 #pragma warning(disable: 4251)
46 #endif
47 
48 namespace sdf
49 {
50  // Inline bracket to help doxygen filtering.
51  inline namespace SDF_VERSION_NAMESPACE {
52  //
53 
55 
58  typedef std::shared_ptr<Param> ParamPtr;
59 
62  typedef std::vector<ParamPtr> Param_V;
63 
65  class ParamPrivate;
66 
67  template<class T>
69  {
70  const T &val;
71  };
72 
73  template<class T> ParamStreamer(T) -> ParamStreamer<T>;
74 
75  template<class T>
76  std::ostream& operator<<(std::ostream &os, ParamStreamer<T> s)
77  {
78  os << s.val;
79  return os;
80  }
81 
82  template<class... Ts>
83  std::ostream& operator<<(std::ostream& os,
84  ParamStreamer<std::variant<Ts...>> sv)
85  {
86  std::visit([&os](auto const &v)
87  {
88  os << ParamStreamer{v};
89  }, sv.val);
90  return os;
91  }
92 
96  {
105  public: Param(const std::string &_key, const std::string &_typeName,
106  const std::string &_default, bool _required,
107  const std::string &_description = "");
108 
119  public: Param(const std::string &_key, const std::string &_typeName,
120  const std::string &_default, bool _required,
121  const std::string &_minValue, const std::string &_maxValue,
122  const std::string &_description = "");
123 
127  public: Param(const Param &_param);
128 
131  public: Param(Param &&_param) noexcept = default;
132 
137  public: Param &operator=(const Param &_param);
138 
142  public: Param &operator=(Param &&_param) noexcept = default;
143 
145  public: virtual ~Param();
146 
149  public: std::string GetAsString() const;
150 
153  public: std::string GetDefaultAsString() const;
154 
159  public: std::optional<std::string> GetMinValueAsString() const;
160 
165  public: std::optional<std::string> GetMaxValueAsString() const;
166 
169  public: bool SetFromString(const std::string &_value);
170 
172  public: void Reset();
173 
176  public: const std::string &GetKey() const;
177 
181  public: template<typename Type>
182  bool IsType() const;
183 
186  public: const std::string &GetTypeName() const;
187 
190  public: bool GetRequired() const;
191 
194  public: bool GetSet() const;
195 
198  public: ParamPtr Clone() const;
199 
203  public: template<typename T>
204  void SetUpdateFunc(T _updateFunc);
205 
208  public: void Update();
209 
215  public: template<typename T>
216  bool Set(const T &_value);
217 
221  public: bool GetAny(std::any &_anyVal) const;
222 
227  public: template<typename T>
228  bool Get(T &_value) const;
229 
234  public: template<typename T>
235  bool GetDefault(T &_value) const;
236 
239  public: void SetDescription(const std::string &_desc);
240 
243  public: std::string GetDescription() const;
244 
247  public: bool ValidateValue() const;
248 
253  public: friend std::ostream &operator<<(std::ostream &_out,
254  const Param &_p)
255  {
256  _out << _p.GetAsString();
257  return _out;
258  }
259 
262  private: bool ValueFromString(const std::string &_value);
263 
265  private: std::unique_ptr<ParamPrivate> dataPtr;
266  };
267 
271  {
273  public: std::string key;
274 
276  public: bool required;
277 
279  public: bool set;
280 
282  public: std::string typeName;
283 
285  public: std::string description;
286 
288  public: std::function<std::any ()> updateFunc;
289 
292  public: typedef std::variant<bool, char, std::string, int, std::uint64_t,
293  unsigned int, double, float, sdf::Time,
294  ignition::math::Angle,
295  ignition::math::Color,
296  ignition::math::Vector2i,
297  ignition::math::Vector2d,
298  ignition::math::Vector3d,
299  ignition::math::Quaterniond,
300  ignition::math::Pose3d> ParamVariant;
301 
303  public: ParamVariant value;
304 
306  public: ParamVariant defaultValue;
307 
309  public: std::optional<ParamVariant> minValue;
310 
312  public: std::optional<ParamVariant> maxValue;
313  };
314 
316  template<typename T>
317  void Param::SetUpdateFunc(T _updateFunc)
318  {
319  this->dataPtr->updateFunc = _updateFunc;
320  }
321 
323  template<typename T>
324  bool Param::Set(const T &_value)
325  {
326  try
327  {
328  std::stringstream ss;
329  ss << _value;
330  return this->SetFromString(ss.str());
331  }
332  catch(...)
333  {
334  sdferr << "Unable to set parameter["
335  << this->dataPtr->key << "]."
336  << "Type used must have a stream input and output operator,"
337  << "which allows proper functioning of Param.\n";
338  return false;
339  }
340  }
341 
343  template<typename T>
344  bool Param::Get(T &_value) const
345  {
346  try
347  {
348  if (typeid(T) == typeid(bool) && this->dataPtr->typeName == "string")
349  {
350  std::string strValue = std::get<std::string>(this->dataPtr->value);
351  std::transform(strValue.begin(), strValue.end(), strValue.begin(),
352  [](unsigned char c)
353  {
354  return static_cast<unsigned char>(std::tolower(c));
355  });
356 
357  std::stringstream tmp;
358  if (strValue == "true" || strValue == "1")
359  {
360  tmp << "1";
361  }
362  else
363  {
364  tmp << "0";
365  }
366  tmp >> _value;
367  }
368  else
369  {
370  T *value = std::get_if<T>(&this->dataPtr->value);
371  if (value)
372  _value = *value;
373  else
374  {
375  std::stringstream ss;
376  ss << ParamStreamer{this->dataPtr->value};
377  ss >> _value;
378  }
379  }
380  }
381  catch(...)
382  {
383  sdferr << "Unable to convert parameter["
384  << this->dataPtr->key << "] "
385  << "whose type is["
386  << this->dataPtr->typeName << "], to "
387  << "type[" << typeid(T).name() << "]\n";
388  return false;
389  }
390  return true;
391  }
392 
394  template<typename T>
395  bool Param::GetDefault(T &_value) const
396  {
397  std::stringstream ss;
398 
399  try
400  {
401  ss << ParamStreamer{this->dataPtr->defaultValue};
402  ss >> _value;
403  }
404  catch(...)
405  {
406  sdferr << "Unable to convert parameter["
407  << this->dataPtr->key << "] "
408  << "whose type is["
409  << this->dataPtr->typeName << "], to "
410  << "type[" << typeid(T).name() << "]\n";
411  return false;
412  }
413 
414  return true;
415  }
416 
418  template<typename Type>
419  bool Param::IsType() const
420  {
421  return std::holds_alternative<Type>(this->dataPtr->value);
422  }
423  }
424 }
425 
426 #ifdef _WIN32
427 #pragma warning(pop)
428 #endif
429 
430 #endif
std::optional< ParamVariant > maxValue
This parameter&#39;s maximum allowed value.
Definition: Param.hh:312
std::function< std::any()> updateFunc
Update function pointer.
Definition: Param.hh:288
Definition: Param.hh:68
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:300
ParamStreamer(T) -> ParamStreamer< T >
std::vector< ParamPtr > Param_V
Definition: Param.hh:62
A parameter class.
Definition: Param.hh:95
Definition: Param.hh:270
friend std::ostream & operator<<(std::ostream &_out, const Param &_p)
Ostream operator.
Definition: Param.hh:253
class SDFORMAT_VISIBLE Param
Definition: Param.hh:54
#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::optional< ParamVariant > minValue
This parameter&#39;s minimum allowed value.
Definition: Param.hh:309
std::shared_ptr< Param > ParamPtr
Definition: Param.hh:58
std::string GetAsString() const
Get the value as a string.
const T & val
Definition: Param.hh:70
std::string typeName
Definition: Param.hh:282
bool required
True if the parameter is required.
Definition: Param.hh:276
ParamVariant value
This parameter&#39;s value.
Definition: Param.hh:303
std::string description
Description of the parameter.
Definition: Param.hh:285
namespace for Simulation Description Format parser
Definition: Actor.hh:32
ParamVariant defaultValue
This parameter&#39;s default value.
Definition: Param.hh:306
std::ostream & operator<<(std::ostream &os, ParamStreamer< std::variant< Ts... >> sv)
Definition: Param.hh:83
std::string key
Key value.
Definition: Param.hh:273