All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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 /* Desc: A parameter
18  * Author: Nate Koenig
19  * Date: 14 Aug 2008
20  */
21 
22 #ifndef SDF_PARAM_HH
23 #define SDF_PARAM_HH
24 
25 #ifndef Q_MOC_RUN // See: https://bugreports.qt-project.org/browse/QTBUG-22829
26 # include <boost/lexical_cast.hpp>
27 #endif
28 #include <boost/bind.hpp>
29 #include <boost/algorithm/string.hpp>
30 #include <boost/any.hpp>
31 #include <boost/function.hpp>
32 #include <typeinfo>
33 #include <string>
34 #include <vector>
35 
36 #include "gazebo/common/Console.hh"
37 #include "gazebo/common/Color.hh"
38 #include "gazebo/common/Time.hh"
39 #include "gazebo/math/Vector3.hh"
40 #include "gazebo/math/Vector2i.hh"
41 #include "gazebo/math/Vector2d.hh"
42 #include "gazebo/math/Pose.hh"
44 
45 namespace sdf
46 {
47  class Param;
48  typedef boost::shared_ptr< Param > ParamPtr;
49  typedef std::vector< ParamPtr > Param_V;
50 
52  class Param
53  {
55  public: Param(Param *_newParam) GAZEBO_DEPRECATED(1.6);
56 
58  public: virtual ~Param() GAZEBO_DEPRECATED(1.6);
59 
61  public: virtual std::string GetAsString() const GAZEBO_DEPRECATED(1.6)
62  {return std::string();}
63  public: virtual std::string GetDefaultAsString() const
64  GAZEBO_DEPRECATED(1.6)
65  {return std::string();}
66 
68  public: virtual bool SetFromString(const std::string &)
69  GAZEBO_DEPRECATED(1.6)
70  {return true;}
72  public: virtual void Reset() GAZEBO_DEPRECATED(1.6) = 0;
73 
74  public: const std::string &GetKey() const GAZEBO_DEPRECATED(1.6)
75  {return this->key;}
76  public: std::string GetTypeName() const GAZEBO_DEPRECATED(1.6);
77  public: bool GetRequired() const GAZEBO_DEPRECATED(1.6)
78  { return this->required; }
80  public: bool GetSet() const GAZEBO_DEPRECATED(1.6)
81  { return this->set; }
82  public: virtual boost::shared_ptr<Param> Clone() const
83  GAZEBO_DEPRECATED(1.6) = 0;
84 
86  public: template<typename T> void SetUpdateFunc(T _updateFunc)
87  { this->updateFunc = _updateFunc; }
88  public: virtual void Update() GAZEBO_DEPRECATED(1.6) = 0;
89 
94  public: template<typename T>
95  bool Get(T &_value)
96  {
97  try
98  {
99  _value = boost::lexical_cast<T>(this->GetAsString());
100  }
101  catch(...)
102  {
103  gzerr << "Unable to convert parameter[" << this->key << "] "
104  << "whose type is[" << this->typeName << "], to "
105  << "type[" << typeid(T).name() << "]\n";
106  return false;
107  }
108  return true;
109  }
110 
111  public: bool IsBool() const GAZEBO_DEPRECATED(1.6);
112  public: bool IsInt() const GAZEBO_DEPRECATED(1.6);
113  public: bool IsUInt() const GAZEBO_DEPRECATED(1.6);
114  public: bool IsFloat() const GAZEBO_DEPRECATED(1.6);
115  public: bool IsDouble() const GAZEBO_DEPRECATED(1.6);
116  public: bool IsChar() const GAZEBO_DEPRECATED(1.6);
117  public: bool IsStr() const GAZEBO_DEPRECATED(1.6);
118  public: bool IsVector3() const GAZEBO_DEPRECATED(1.6);
119  public: bool IsVector2i() const GAZEBO_DEPRECATED(1.6);
120  public: bool IsVector2d() const GAZEBO_DEPRECATED(1.6);
121  public: bool IsQuaternion() const GAZEBO_DEPRECATED(1.6);
122  public: bool IsPose() const GAZEBO_DEPRECATED(1.6);
123  public: bool IsColor() const GAZEBO_DEPRECATED(1.6);
124  public: bool IsTime() const GAZEBO_DEPRECATED(1.6);
125 
126  public: bool Set(const bool &_value) GAZEBO_DEPRECATED(1.6);
127  public: bool Set(const int &_value) GAZEBO_DEPRECATED(1.6);
128  public: bool Set(const unsigned int &_value) GAZEBO_DEPRECATED(1.6);
129  public: bool Set(const float &_value) GAZEBO_DEPRECATED(1.6);
130  public: bool Set(const double &_value) GAZEBO_DEPRECATED(1.6);
131  public: bool Set(const char &_value) GAZEBO_DEPRECATED(1.6);
132  public: bool Set(const std::string &_value) GAZEBO_DEPRECATED(1.6);
133  public: bool Set(const char *_value) GAZEBO_DEPRECATED(1.6);
134  public: bool Set(
135  const gazebo::math::Vector3 &_value) GAZEBO_DEPRECATED(1.6);
136  public: bool Set(
137  const gazebo::math::Vector2i &_value) GAZEBO_DEPRECATED(1.6);
138  public: bool Set(
139  const gazebo::math::Vector2d &_value) GAZEBO_DEPRECATED(1.6);
140  public: bool Set(
141  const gazebo::math::Quaternion &_value) GAZEBO_DEPRECATED(1.6);
142  public: bool Set(
143  const gazebo::math::Pose &_value) GAZEBO_DEPRECATED(1.6);
144  public: bool Set(
145  const gazebo::common::Color &_value) GAZEBO_DEPRECATED(1.6);
146  public: bool Set(
147  const gazebo::common::Time &_value) GAZEBO_DEPRECATED(1.6);
148 
149  public: bool Get(bool &_value) GAZEBO_DEPRECATED(1.6);
150  public: bool Get(int &_value) GAZEBO_DEPRECATED(1.6);
151  public: bool Get(unsigned int &_value) GAZEBO_DEPRECATED(1.6);
152  public: bool Get(float &_value) GAZEBO_DEPRECATED(1.6);
153  public: bool Get(double &_value) GAZEBO_DEPRECATED(1.6);
154  public: bool Get(char &_value) GAZEBO_DEPRECATED(1.6);
155  public: bool Get(std::string &_value) GAZEBO_DEPRECATED(1.6);
156  public: bool Get(gazebo::math::Vector3 &_value) GAZEBO_DEPRECATED(1.6);
157  public: bool Get(gazebo::math::Vector2i &_value) GAZEBO_DEPRECATED(1.6);
158  public: bool Get(gazebo::math::Vector2d &_value) GAZEBO_DEPRECATED(1.6);
159  public: bool Get(gazebo::math::Quaternion &_value) GAZEBO_DEPRECATED(1.6);
160  public: bool Get(gazebo::math::Pose &_value) GAZEBO_DEPRECATED(1.6);
161  public: bool Get(gazebo::common::Color &_value) GAZEBO_DEPRECATED(1.6);
162  public: bool Get(gazebo::common::Time &_value) GAZEBO_DEPRECATED(1.6);
163 
165  public: void SetDescription(
166  const std::string &_desc) GAZEBO_DEPRECATED(1.6);
167 
169  public: std::string GetDescription() const GAZEBO_DEPRECATED(1.6);
170 
172  private: static std::vector<Param*> *params GAZEBO_DEPRECATED(1.6);
173 
174  protected: std::string key;
175  protected: bool required;
176  protected: bool set;
177  protected: std::string typeName;
178  protected: std::string description;
179 
180  protected: boost::function<boost::any ()> updateFunc;
181  };
182 
184  template< typename T>
185  class ParamT : public Param
186  {
188  public: ParamT(const std::string &_key, const std::string &_default,
189  bool _required, const std::string &_typeName = "",
190  const std::string &_description = "")
191  GAZEBO_DEPRECATED(1.6)
192  : Param(this)
193  {
194  this->key = _key;
195  this->required = _required;
196  if (_typeName.empty())
197  this->typeName = typeid(T).name();
198  else
199  this->typeName = _typeName;
200  this->description = _description;
201 
202  this->Set(_default);
203  this->defaultValue = this->value;
204  this->set = false;
205  }
206 
208  public: virtual ~ParamT() GAZEBO_DEPRECATED(1.6) {}
209 
211  public: virtual void Update() GAZEBO_DEPRECATED(1.6)
212  {
213  if (this->updateFunc)
214  {
215  try
216  {
217  const T v = boost::any_cast<T>(this->updateFunc());
218  Param::Set(v);
219  }
220  catch(boost::bad_any_cast &e)
221  {
222  gzerr << "boost any_cast error:" << e.what() << "\n";
223  }
224  }
225  }
226 
228  public: virtual std::string GetAsString() const GAZEBO_DEPRECATED(1.6)
229  {
230  std::ostringstream stream;
231  stream << std::fixed << this->value;
232  return stream.str();
233  }
234 
236  public: virtual bool SetFromString(const std::string &_value)
237  GAZEBO_DEPRECATED(1.6)
238  { return this->Set(_value); }
239 
240  public: virtual std::string GetDefaultAsString() const
241  GAZEBO_DEPRECATED(1.6)
242  {
243  return boost::lexical_cast<std::string>(this->defaultValue);
244  }
245 
247  public: virtual bool Set(const std::string &_str)
248  GAZEBO_DEPRECATED(1.6)
249  {
250  std::string str = _str;
251  boost::trim(str);
252  if (str.empty() && this->required)
253  {
254  gzerr << "Empty string used when setting a required parameter. Key["
255  << this->GetKey() << "]\n";
256  return false;
257  }
258  else if (str.empty())
259  {
260  this->value = this->defaultValue;
261  return true;
262  }
263 
264  std::string tmp(str);
265  std::string lowerTmp(str);
266  boost::to_lower(lowerTmp);
267 
268  // "true" and "false" doesn't work properly
269  if (lowerTmp == "true")
270  tmp = "1";
271  else if (lowerTmp == "false")
272  tmp = "0";
273 
274  try
275  {
276  this->value = boost::lexical_cast<T>(tmp);
277  }
278  catch(boost::bad_lexical_cast &e)
279  {
280  if (str == "inf" || str == "-inf")
281  {
282  // in this case, the parser complains, but seems to assign the
283  // right values
284  gzmsg << "INFO [sdf::Param]: boost throws when lexical casting "
285  << "inf's, but the values are usually passed through correctly\n";
286  }
287  else
288  {
289  gzerr << "Unable to set value [" << str
290  << "] for key[" << this->key << "]\n";
291  return false;
292  }
293  }
294 
295  this->set = true;
296  return this->set;
297  }
298 
300  public: T GetValue() const GAZEBO_DEPRECATED(1.6)
301  {
302  return this->value;
303  }
304 
306  public: T GetDefaultValue() const GAZEBO_DEPRECATED(1.6)
307  {
308  return this->defaultValue;
309  }
310 
312  public: void SetValue(const T &_value) GAZEBO_DEPRECATED(1.6)
313  {
314  this->value = _value;
315  this->set = true;
316  }
317 
319  public: virtual void Reset() GAZEBO_DEPRECATED(1.6)
320  {
321  this->value = this->defaultValue;
322  this->set = false;
323  }
324 
325  public: virtual boost::shared_ptr<Param> Clone() const
326  GAZEBO_DEPRECATED(1.6)
327  {
328  boost::shared_ptr<ParamT<T> > clone(
329  new ParamT<T>(this->GetKey(), this->GetAsString(),
330  this->required, this->typeName,
331  this->description));
332  return clone;
333  }
334 
335  public: inline T operator*() const GAZEBO_DEPRECATED(1.6) {return value;}
336  public: friend std::ostream &operator<<(std::ostream &_out,
337  const ParamT<T> &_p) GAZEBO_DEPRECATED(1.6)
338  {
339  _out << _p.value;
340  return _out;
341  }
342 
343  protected: T value;
344  protected: T defaultValue;
345  };
346 }
347 #endif