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/bind.hpp>
25  #include <boost/algorithm/string.hpp>
26  #include <boost/any.hpp>
27  #include <boost/shared_ptr.hpp>
28  #include <boost/variant.hpp>
29  #include <boost/function.hpp>
30 #endif
31 
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 boost::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: boost::shared_ptr<Param> 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: boost::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 
244 
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
293  {
294  _value = boost::lexical_cast<T>(this->dataPtr->value);
295  }
296  }
297  catch(...)
298  {
299  sdferr << "Unable to convert parameter["
300  << this->dataPtr->key << "] "
301  << "whose type is["
302  << this->dataPtr->typeName << "], to "
303  << "type[" << typeid(T).name() << "]\n";
304  return false;
305  }
306  return true;
307  }
308 
310  template<typename T>
311  bool Param::GetDefault(T &_value) const
312  {
313  try
314  {
315  _value = boost::lexical_cast<T>(this->dataPtr->defaultValue);
316  }
317  catch(...)
318  {
319  sdferr << "Unable to convert parameter["
320  << this->dataPtr->key << "] "
321  << "whose type is["
322  << this->dataPtr->typeName << "], to "
323  << "type[" << typeid(T).name() << "]\n";
324  return false;
325  }
326  return true;
327  }
328 
330  template<typename T>
331  void Param::Init(const std::string &_value)
332  {
333  try
334  {
335  this->dataPtr->value = boost::lexical_cast<T>(_value);
336  }
337  catch(...)
338  {
339  if (this->dataPtr->typeName == "bool")
340  {
341  std::string strValue = _value;
342  boost::algorithm::to_lower(strValue);
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:170
A parameter class.
Definition: Param.hh:67
bool Set(const T &_value)
Set the parameter's value.
Definition: Param.hh:258
ParamVariant value
This parameter's value.
Definition: Param.hh:243
boost::function< boost::any()> updateFunc
Update function pointer.
Definition: Param.hh:222
A quaternion class.
Definition: Types.hh:325
ParamVariant defaultValue
This parameter'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:121
bool set
True if the parameter is set.
Definition: Param.hh:213
std::string typeName
Definition: Param.hh:216
bool Get(T &_value) const
Get the value of the parameter.
Definition: Param.hh:278
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:47
std::string description
Description of the parameter.
Definition: Param.hh:219
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:60
Defines a color.
Definition: Types.hh:62
Param * ParamPtr
Definition: Param.hh:56
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:697
std::string key
Key value.
Definition: Param.hh:207
Definition: Param.hh:204
Encapsulates a position and rotation in three space.
Definition: Types.hh:598
bool GetDefault(T &_value) const
Get the default value of the parameter.
Definition: Param.hh:311
#define SDF_DEPRECATED(version)
Definition: Types.hh:37
The Vector3 class represents the generic vector containing 3 elements. Since it's commonly used to ke...
Definition: Types.hh:223