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 _SDF_PARAM_HH_
19 #define _SDF_PARAM_HH_
20 
21 // See: https://bugreports.qt-project.org/browse/QTBUG-22829
22 #ifndef Q_MOC_RUN
23  #include <boost/lexical_cast.hpp>
24  #include <boost/any.hpp>
25  #include <boost/variant.hpp>
26  #include <boost/version.hpp>
27 #endif
28 
29 #include <memory>
30 #include <functional>
31 #include <algorithm>
32 #include <typeinfo>
33 #include <string>
34 #include <vector>
35 #include <ignition/math.hh>
36 
37 #include "sdf/Console.hh"
38 #include "sdf/system_util.hh"
39 
41 #ifndef _WIN32
42 #pragma GCC diagnostic push
43 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
44 #endif
45 #include "sdf/Types.hh"
46 #ifndef _WIN32
47 #pragma GCC diagnostic pop
48 #endif
49 
50 namespace sdf
51 {
53 
56  typedef std::shared_ptr<Param> ParamPtr;
57 
60  typedef std::vector<ParamPtr> Param_V;
61 
63  class ParamPrivate;
64 
68  {
76  public: Param(const std::string &_key, const std::string &_typeName,
77  const std::string &_default, bool _required,
78  const std::string &_description = "");
79 
81  public: virtual ~Param();
82 
85  public: std::string GetAsString() const;
86 
89  public: std::string GetDefaultAsString() const;
90 
93  public: bool SetFromString(const std::string &_value);
94 
96  public: void Reset();
97 
100  public: const std::string &GetKey() const;
101 
106  public: const std::type_info &GetType() const SDF_DEPRECATED(4.0);
107 
111  public: template<typename Type>
112  bool IsType() const;
113 
116  public: const std::string &GetTypeName() const;
117 
120  public: bool GetRequired() const;
121 
124  public: bool GetSet() const;
125 
128  public: ParamPtr Clone() const;
129 
133  public: template<typename T>
134  void SetUpdateFunc(T _updateFunc);
135 
138  public: void Update();
139 
146  public: template<typename T>
147  bool Set(const T &_value);
148 
152  public: bool GetAny(boost::any &_anyVal) const;
153 
158  public: template<typename T>
159  bool Get(T &_value) const;
160 
165  public: template<typename T>
166  bool GetDefault(T &_value) const;
167 
172  public: Param &operator=(const Param &_param);
173 
176  public: void SetDescription(const std::string &_desc);
177 
180  public: std::string GetDescription() const;
181 
186  public: friend std::ostream &operator<<(std::ostream &_out,
187  const Param &_p)
188  {
189  _out << _p.GetAsString();
190  return _out;
191  }
192 
195  private: template<typename T>
196  void Init(const std::string &_value);
197 
199  private: ParamPrivate *dataPtr;
200  };
201 
205  {
207  public: std::string key;
208 
210  public: bool required;
211 
213  public: bool set;
214 
216  public: std::string typeName;
217 
219  public: std::string description;
220 
222  public: std::function<boost::any ()> updateFunc;
223 
225 #ifndef _WIN32
226 #pragma GCC diagnostic push
227 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
228 #endif
229  public: typedef boost::variant<bool, char, std::string, int, uint64_t,
232  unsigned int, double, float, sdf::Time, sdf::Color,
235  ignition::math::Vector3d, ignition::math::Vector2i,
236  ignition::math::Vector2d, ignition::math::Quaterniond,
237  ignition::math::Pose3d> ParamVariant;
238 #ifndef _WIN32
239 #pragma GCC diagnostic pop
240 #endif
241 
243  public: ParamVariant value;
244 
246  public: ParamVariant defaultValue;
247  };
248 
250  template<typename T>
251  void Param::SetUpdateFunc(T _updateFunc)
252  {
253  this->dataPtr->updateFunc = _updateFunc;
254  }
255 
257  template<typename T>
258  bool Param::Set(const T &_value)
259  {
260  try
261  {
262  this->SetFromString(boost::lexical_cast<std::string>(_value));
263  }
264  catch(...)
265  {
266  sdferr << "Unable to set parameter["
267  << this->dataPtr->key << "]."
268  << "Type type used must have a stream input and output"
269  << "operator, which allow boost::lexical_cast to"
270  << "function properly.\n";
271  return false;
272  }
273  return true;
274  }
275 
277  template<typename T>
278  bool Param::Get(T &_value) const
279  {
280  try
281  {
282  if (typeid(T) == typeid(bool) &&
283  this->dataPtr->typeName == "string")
284  {
285  std::string strValue =
286  boost::lexical_cast<std::string>(this->dataPtr->value);
287  if (strValue == "true" || strValue == "1")
288  _value = boost::lexical_cast<T>("1");
289  else
290  _value = boost::lexical_cast<T>("0");
291  }
292  else if (typeid(T) == this->dataPtr->value.type())
293  {
294 #if BOOST_VERSION < 105800
295  _value = boost::get<T>(this->dataPtr->value);
296 #else
297  _value = boost::relaxed_get<T>(this->dataPtr->value);
298 #endif
299  }
300  else
301  {
302  _value = boost::lexical_cast<T>(this->dataPtr->value);
303  }
304  }
305  catch(...)
306  {
307  sdferr << "Unable to convert parameter["
308  << this->dataPtr->key << "] "
309  << "whose type is["
310  << this->dataPtr->typeName << "], to "
311  << "type[" << typeid(T).name() << "]\n";
312  return false;
313  }
314  return true;
315  }
316 
318  template<typename T>
319  bool Param::GetDefault(T &_value) const
320  {
321  try
322  {
323  _value = boost::lexical_cast<T>(this->dataPtr->defaultValue);
324  }
325  catch(...)
326  {
327  sdferr << "Unable to convert parameter["
328  << this->dataPtr->key << "] "
329  << "whose type is["
330  << this->dataPtr->typeName << "], to "
331  << "type[" << typeid(T).name() << "]\n";
332  return false;
333  }
334  return true;
335  }
336 
338  template<typename T>
339  void Param::Init(const std::string &_value)
340  {
341  try
342  {
343  this->dataPtr->value = boost::lexical_cast<T>(_value);
344  }
345  catch(...)
346  {
347  if (this->dataPtr->typeName == "bool")
348  {
349  std::string strValue = _value;
350  std::transform(strValue.begin(), strValue.end(),
351  strValue.begin(), ::tolower);
352  if (strValue == "true" || strValue == "1")
353  this->dataPtr->value = true;
354  else
355  this->dataPtr->value = false;
356  }
357  else
358  {
359  sdferr << "Unable to init parameter value from string["
360  << _value << "]\n";
361  }
362  }
363 
364  this->dataPtr->defaultValue = this->dataPtr->value;
365  this->dataPtr->set = false;
366  }
367 
369  template<typename Type>
370  bool Param::IsType() const
371  {
372  return this->dataPtr->value.type() == typeid(Type);
373  }
374 }
375 #endif
Generic double x, y vector.
Definition: Types.hh:169
std::string GetAsString() const
Get the value as a string.
A parameter class.
Definition: Param.hh:67
bool Set(const T &_value)
Set the parameter&#39;s value.
Definition: Param.hh:258
ParamVariant value
This parameter&#39;s value.
Definition: Param.hh:243
bool Get(T &_value) const
Get the value of the parameter.
Definition: Param.hh:278
A quaternion class.
Definition: Types.hh:324
bool IsType() const
Return true if the param is a particular type.
Definition: Param.hh:370
ParamVariant defaultValue
This parameter&#39;s default value.
Definition: Param.hh:246
boost::variant< bool, char, std::string, int, uint64_t, unsigned int, double, float, sdf::Time, sdf::Color, sdf::Vector3, sdf::Vector2i, sdf::Vector2d, sdf::Quaternion, sdf::Pose, ignition::math::Vector3d, ignition::math::Vector2i, ignition::math::Vector2d, ignition::math::Quaterniond, ignition::math::Pose3d > ParamVariant
Definition: Param.hh:237
friend std::ostream & operator<<(std::ostream &_out, const Param &_p)
Ostream operator.
Definition: Param.hh:186
class SDFORMAT_VISIBLE Param
Definition: Param.hh:52
void SetUpdateFunc(T _updateFunc)
Set the update function.
Definition: Param.hh:251
Generic integer x, y vector.
Definition: Types.hh:120
std::string typeName
Definition: Param.hh:216
std::function< boost::any()> updateFunc
Update function pointer.
Definition: Param.hh:222
bool required
True if the parameter is required.
Definition: Param.hh:210
#define SDFORMAT_VISIBLE
Use to represent "symbol visible" if supported.
Definition: system_util.hh:48
#define sdferr
Output an error message.
Definition: Console.hh:54
std::string description
Description of the parameter.
Definition: Param.hh:219
std::shared_ptr< Param > ParamPtr
Definition: Param.hh:56
std::vector< ParamPtr > Param_V
Definition: Param.hh:60
Defines a color.
Definition: Types.hh:61
bool GetDefault(T &_value) const
Get the default value of the parameter.
Definition: Param.hh:319
namespace for Simulation Description Format parser
Definition: Console.hh:36
A Time class, can be used to hold wall- or sim-time.
Definition: Types.hh:696
std::string key
Key value.
Definition: Param.hh:207
Definition: Param.hh:204
Encapsulates a position and rotation in three space.
Definition: Types.hh:597
#define SDF_DEPRECATED(version)
Definition: Types.hh:36
The Vector3 class represents the generic vector containing 3 elements.
Definition: Types.hh:222