All Classes Namespaces Files Functions Variables Typedefs Friends Macros Groups Pages
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 #endif
27 
28 #include <memory>
29 #include <functional>
30 #include <algorithm>
31 #include <typeinfo>
32 #include <string>
33 #include <vector>
34 #include <ignition/math.hh>
35 
36 #include "sdf/Console.hh"
37 #include "sdf/system_util.hh"
38 
40 #ifndef _WIN32
41 #pragma GCC diagnostic push
42 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
43 #endif
44 #include "sdf/Types.hh"
45 #ifndef _WIN32
46 #pragma GCC diagnostic pop
47 #endif
48 
49 namespace sdf
50 {
52 
55  typedef std::shared_ptr<Param> ParamPtr;
56 
59  typedef std::vector<ParamPtr> Param_V;
60 
62  class ParamPrivate;
63 
67  {
75  public: Param(const std::string &_key, const std::string &_typeName,
76  const std::string &_default, bool _required,
77  const std::string &_description = "");
78 
80  public: virtual ~Param();
81 
84  public: std::string GetAsString() const;
85 
88  public: std::string GetDefaultAsString() const;
89 
92  public: bool SetFromString(const std::string &_value);
93 
95  public: void Reset();
96 
99  public: const std::string &GetKey() const;
100 
105  public: const std::type_info &GetType() const SDF_DEPRECATED(4.0);
106 
110  public: template<typename Type>
111  bool IsType() const;
112 
115  public: const std::string &GetTypeName() const;
116 
119  public: bool GetRequired() const;
120 
123  public: bool GetSet() const;
124 
127  public: ParamPtr Clone() const;
128 
132  public: template<typename T>
133  void SetUpdateFunc(T _updateFunc);
134 
137  public: void Update();
138 
145  public: template<typename T>
146  bool Set(const T &_value);
147 
151  public: bool GetAny(boost::any &_anyVal) const;
152 
157  public: template<typename T>
158  bool Get(T &_value) const;
159 
164  public: template<typename T>
165  bool GetDefault(T &_value) const;
166 
171  public: Param &operator=(const Param &_param);
172 
175  public: void SetDescription(const std::string &_desc);
176 
179  public: std::string GetDescription() const;
180 
185  public: friend std::ostream &operator<<(std::ostream &_out,
186  const Param &_p)
187  {
188  _out << _p.GetAsString();
189  return _out;
190  }
191 
194  private: template<typename T>
195  void Init(const std::string &_value);
196 
198  private: ParamPrivate *dataPtr;
199  };
200 
204  {
206  public: std::string key;
207 
209  public: bool required;
210 
212  public: bool set;
213 
215  public: std::string typeName;
216 
218  public: std::string description;
219 
221  public: std::function<boost::any ()> updateFunc;
222 
224 #ifndef _WIN32
225 #pragma GCC diagnostic push
226 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
227 #endif
228  public: typedef boost::variant<bool, char, std::string, int, uint64_t,
231  unsigned int, double, float, sdf::Time, sdf::Color,
234  ignition::math::Vector3d, ignition::math::Vector2i,
235  ignition::math::Vector2d, ignition::math::Quaterniond,
236  ignition::math::Pose3d> ParamVariant;
237 #ifndef _WIN32
238 #pragma GCC diagnostic pop
239 #endif
240 
243 
246  };
247 
249  template<typename T>
250  void Param::SetUpdateFunc(T _updateFunc)
251  {
252  this->dataPtr->updateFunc = _updateFunc;
253  }
254 
256  template<typename T>
257  bool Param::Set(const T &_value)
258  {
259  try
260  {
261  this->SetFromString(boost::lexical_cast<std::string>(_value));
262  }
263  catch(...)
264  {
265  sdferr << "Unable to set parameter["
266  << this->dataPtr->key << "]."
267  << "Type type used must have a stream input and output"
268  << "operator, which allow boost::lexical_cast to"
269  << "function properly.\n";
270  return false;
271  }
272  return true;
273  }
274 
276  template<typename T>
277  bool Param::Get(T &_value) const
278  {
279  try
280  {
281  if (typeid(T) == typeid(bool) &&
282  this->dataPtr->typeName == "string")
283  {
284  std::string strValue =
285  boost::lexical_cast<std::string>(this->dataPtr->value);
286  if (strValue == "true" || strValue == "1")
287  _value = boost::lexical_cast<T>("1");
288  else
289  _value = boost::lexical_cast<T>("0");
290  }
291  else
292  {
293  _value = boost::lexical_cast<T>(this->dataPtr->value);
294  }
295  }
296  catch(...)
297  {
298  sdferr << "Unable to convert parameter["
299  << this->dataPtr->key << "] "
300  << "whose type is["
301  << this->dataPtr->typeName << "], to "
302  << "type[" << typeid(T).name() << "]\n";
303  return false;
304  }
305  return true;
306  }
307 
309  template<typename T>
310  bool Param::GetDefault(T &_value) const
311  {
312  try
313  {
314  _value = boost::lexical_cast<T>(this->dataPtr->defaultValue);
315  }
316  catch(...)
317  {
318  sdferr << "Unable to convert parameter["
319  << this->dataPtr->key << "] "
320  << "whose type is["
321  << this->dataPtr->typeName << "], to "
322  << "type[" << typeid(T).name() << "]\n";
323  return false;
324  }
325  return true;
326  }
327 
329  template<typename T>
330  void Param::Init(const std::string &_value)
331  {
332  try
333  {
334  this->dataPtr->value = boost::lexical_cast<T>(_value);
335  }
336  catch(...)
337  {
338  if (this->dataPtr->typeName == "bool")
339  {
340  std::string strValue = _value;
341  std::transform(strValue.begin(), strValue.end(),
342  strValue.begin(), ::tolower);
343  if (strValue == "true" || strValue == "1")
344  this->dataPtr->value = true;
345  else
346  this->dataPtr->value = false;
347  }
348  else
349  {
350  sdferr << "Unable to init parameter value from string["
351  << _value << "]\n";
352  }
353  }
354 
355  this->dataPtr->defaultValue = this->dataPtr->value;
356  this->dataPtr->set = false;
357  }
358 
360  template<typename Type>
361  bool Param::IsType() const
362  {
363  return this->dataPtr->value.type() == typeid(Type);
364  }
365 }
366 #endif
Generic double x, y vector.
Definition: Types.hh:169
A parameter class.
Definition: Param.hh:66
bool Set(const T &_value)
Set the parameter's value.
Definition: Param.hh:257
ParamVariant value
This parameter's value.
Definition: Param.hh:242
A quaternion class.
Definition: Types.hh:324
ParamVariant defaultValue
This parameter's default value.
Definition: Param.hh:245
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:236
friend std::ostream & operator<<(std::ostream &_out, const Param &_p)
Ostream operator.
Definition: Param.hh:185
class SDFORMAT_VISIBLE Param
Definition: Param.hh:51
void SetUpdateFunc(T _updateFunc)
Set the update function.
Definition: Param.hh:250
Generic integer x, y vector.
Definition: Types.hh:120
bool set
True if the parameter is set.
Definition: Param.hh:212
std::string typeName
Definition: Param.hh:215
std::function< boost::any()> updateFunc
Update function pointer.
Definition: Param.hh:221
bool Get(T &_value) const
Get the value of the parameter.
Definition: Param.hh:277
bool required
True if the parameter is required.
Definition: Param.hh:209
#define SDFORMAT_VISIBLE
Use to represent "symbol visible" if supported.
Definition: system_util.hh:48
#define sdferr
Output an error message.
Definition: Console.hh:47
std::string description
Description of the parameter.
Definition: Param.hh:218
std::shared_ptr< Param > ParamPtr
Definition: Param.hh:55
bool SetFromString(const std::string &_value)
Set the parameter value from a string.
std::string GetAsString() const
Get the value as a string.
std::vector< ParamPtr > Param_V
Definition: Param.hh:59
Defines a color.
Definition: Types.hh:61
bool IsType() const
Return true if the param is a particular type.
Definition: Param.hh:361
A Time class, can be used to hold wall- or sim-time.
Definition: Types.hh:696
std::string key
Key value.
Definition: Param.hh:206
Definition: Param.hh:203
Encapsulates a position and rotation in three space.
Definition: Types.hh:597
bool GetDefault(T &_value) const
Get the default value of the parameter.
Definition: Param.hh:310
#define SDF_DEPRECATED(version)
Definition: Types.hh:36
The Vector3 class represents the generic vector containing 3 elements. Since it's commonly used to ke...
Definition: Types.hh:222