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 <sstream>
28 #include <string>
29 #include <typeinfo>
30 #include <variant>
31 #include <vector>
32 
33 #include <ignition/math.hh>
34 
35 #include "sdf/Console.hh"
36 #include "sdf/sdf_config.h"
37 #include "sdf/system_util.hh"
38 #include "sdf/Types.hh"
39 
40 #ifdef _WIN32
41 // Disable warning C4251 which is triggered by
42 // std::unique_ptr
43 #pragma warning(push)
44 #pragma warning(disable: 4251)
45 #endif
46 
47 namespace sdf
48 {
49  // Inline bracket to help doxygen filtering.
50  inline namespace SDF_VERSION_NAMESPACE {
51  //
52 
54 
57  typedef std::shared_ptr<Param> ParamPtr;
58 
61  typedef std::vector<ParamPtr> Param_V;
62 
64  class ParamPrivate;
65 
66  template<class T>
68  {
69  const T &val;
70  };
71 
72  template<class T> ParamStreamer(T) -> ParamStreamer<T>;
73 
74  template<class T>
75  std::ostream& operator<<(std::ostream &os, ParamStreamer<T> s)
76  {
77  os << s.val;
78  return os;
79  }
80 
81  template<class... Ts>
82  std::ostream& operator<<(std::ostream& os,
83  ParamStreamer<std::variant<Ts...>> sv)
84  {
85  std::visit([&os](auto const &v)
86  {
87  os << ParamStreamer{v};
88  }, sv.val);
89  return os;
90  }
91 
95  {
104  public: Param(const std::string &_key, const std::string &_typeName,
105  const std::string &_default, bool _required,
106  const std::string &_description = "");
107 
109  public: virtual ~Param();
110 
113  public: std::string GetAsString() const;
114 
117  public: std::string GetDefaultAsString() const;
118 
121  public: bool SetFromString(const std::string &_value);
122 
124  public: void Reset();
125 
128  public: const std::string &GetKey() const;
129 
133  public: template<typename Type>
134  bool IsType() const;
135 
138  public: const std::string &GetTypeName() const;
139 
142  public: bool GetRequired() const;
143 
146  public: bool GetSet() const;
147 
150  public: ParamPtr Clone() const;
151 
155  public: template<typename T>
156  void SetUpdateFunc(T _updateFunc);
157 
160  public: void Update();
161 
167  public: template<typename T>
168  bool Set(const T &_value);
169 
173  public: bool GetAny(std::any &_anyVal) const;
174 
179  public: template<typename T>
180  bool Get(T &_value) const;
181 
186  public: template<typename T>
187  bool GetDefault(T &_value) const;
188 
193  public: Param &operator=(const Param &_param);
194 
197  public: void SetDescription(const std::string &_desc);
198 
201  public: std::string GetDescription() const;
202 
207  public: friend std::ostream &operator<<(std::ostream &_out,
208  const Param &_p)
209  {
210  _out << _p.GetAsString();
211  return _out;
212  }
213 
216  private: bool ValueFromString(const std::string &_value);
217 
219  private: std::unique_ptr<ParamPrivate> dataPtr;
220  };
221 
225  {
227  public: std::string key;
228 
230  public: bool required;
231 
233  public: bool set;
234 
236  public: std::string typeName;
237 
239  public: std::string description;
240 
242  public: std::function<std::any ()> updateFunc;
243 
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,
254  ignition::math::Pose3d> ParamVariant;
255 
257  public: ParamVariant value;
258 
260  public: ParamVariant defaultValue;
261  };
262 
264  template<typename T>
265  void Param::SetUpdateFunc(T _updateFunc)
266  {
267  this->dataPtr->updateFunc = _updateFunc;
268  }
269 
271  template<typename T>
272  bool Param::Set(const T &_value)
273  {
274  try
275  {
276  std::stringstream ss;
277  ss << _value;
278  return this->SetFromString(ss.str());
279  }
280  catch(...)
281  {
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";
286  return false;
287  }
288  }
289 
291  template<typename T>
292  bool Param::Get(T &_value) const
293  {
294  try
295  {
296  if (typeid(T) == typeid(bool) && this->dataPtr->typeName == "string")
297  {
298  std::string strValue = std::get<std::string>(this->dataPtr->value);
299  std::transform(strValue.begin(), strValue.end(), strValue.begin(),
300  [](unsigned char c)
301  {
302  return static_cast<unsigned char>(std::tolower(c));
303  });
304 
305  std::stringstream tmp;
306  if (strValue == "true" || strValue == "1")
307  {
308  tmp << "1";
309  }
310  else
311  {
312  tmp << "0";
313  }
314  tmp >> _value;
315  }
316  else
317  {
318  T *value = std::get_if<T>(&this->dataPtr->value);
319  if (value)
320  _value = *value;
321  else
322  {
323  std::stringstream ss;
324  ss << ParamStreamer{this->dataPtr->value};
325  ss >> _value;
326  }
327  }
328  }
329  catch(...)
330  {
331  sdferr << "Unable to convert parameter["
332  << this->dataPtr->key << "] "
333  << "whose type is["
334  << this->dataPtr->typeName << "], to "
335  << "type[" << typeid(T).name() << "]\n";
336  return false;
337  }
338  return true;
339  }
340 
342  template<typename T>
343  bool Param::GetDefault(T &_value) const
344  {
345  std::stringstream ss;
346 
347  try
348  {
349  ss << ParamStreamer{this->dataPtr->defaultValue};
350  ss >> _value;
351  }
352  catch(...)
353  {
354  sdferr << "Unable to convert parameter["
355  << this->dataPtr->key << "] "
356  << "whose type is["
357  << this->dataPtr->typeName << "], to "
358  << "type[" << typeid(T).name() << "]\n";
359  return false;
360  }
361 
362  return true;
363  }
364 
366  template<typename Type>
367  bool Param::IsType() const
368  {
369  return std::holds_alternative<Type>(this->dataPtr->value);
370  }
371  }
372 }
373 
374 #ifdef _WIN32
375 #pragma warning(pop)
376 #endif
377 
378 #endif
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
bool required
True if the parameter is required.
Definition: Param.hh:230
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
std::string typeName
Definition: Param.hh:236
ParamStreamer(T) -> ParamStreamer< T >
Definition: Param.hh:224
Definition: Param.hh:67
std::string GetAsString() const
Get the value as a string.
std::string key
Key value.
Definition: Param.hh:227
#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 description
Description of the parameter.
Definition: Param.hh:239
std::shared_ptr< Param > ParamPtr
Definition: Param.hh:57
class SDFORMAT_VISIBLE Param
Definition: Param.hh:53
namespace for Simulation Description Format parser
Definition: Actor.hh:32
friend std::ostream & operator<<(std::ostream &_out, const Param &_p)
Ostream operator.
Definition: Param.hh:207
std::vector< ParamPtr > Param_V
Definition: Param.hh:61
ParamVariant value
This parameter&#39;s value.
Definition: Param.hh:257
std::ostream & operator<<(std::ostream &os, ParamStreamer< std::variant< Ts... >> sv)
Definition: Param.hh:82
ParamVariant defaultValue
This parameter&#39;s default value.
Definition: Param.hh:260