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 2011 Nate Koenig
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 #include <boost/lexical_cast.hpp>
26 #include <boost/bind.hpp>
27 #include <boost/algorithm/string.hpp>
28 #include <boost/any.hpp>
29 #include <boost/function.hpp>
30 #include <typeinfo>
31 #include <string>
32 #include <vector>
33 
34 #include "common/Console.hh"
35 #include "common/Color.hh"
36 #include "common/Time.hh"
37 #include "math/Vector3.hh"
38 #include "math/Vector2i.hh"
39 #include "math/Vector2d.hh"
40 #include "math/Pose.hh"
41 #include "math/Quaternion.hh"
42 
43 namespace sdf
44 {
45  class Param;
46  typedef boost::shared_ptr< Param > ParamPtr;
47  typedef std::vector< ParamPtr > Param_V;
48 
50  class Param
51  {
53  public: Param(Param *_newParam);
54 
56  public: virtual ~Param();
57 
59  public: virtual std::string GetAsString() const
60  {return std::string();}
61  public: virtual std::string GetDefaultAsString() const
62  {return std::string();}
63 
65  public: virtual bool SetFromString(const std::string &)
66  {return true;}
68  public: virtual void Reset() = 0;
69 
70  public: const std::string &GetKey() const {return this->key;}
71  public: std::string GetTypeName() const;
72  public: bool GetRequired() const { return this->required; }
74  public: bool GetSet() const { return this->set; }
75  public: virtual boost::shared_ptr<Param> Clone() const = 0;
76 
78  public: template<typename T> void SetUpdateFunc(T _updateFunc)
79  { this->updateFunc = _updateFunc; }
80  public: virtual void Update() = 0;
81 
82  public: bool IsBool() const;
83  public: bool IsInt() const;
84  public: bool IsUInt() const;
85  public: bool IsFloat() const;
86  public: bool IsDouble() const;
87  public: bool IsChar() const;
88  public: bool IsStr() const;
89  public: bool IsVector3() const;
90  public: bool IsVector2i() const;
91  public: bool IsVector2d() const;
92  public: bool IsQuaternion() const;
93  public: bool IsPose() const;
94  public: bool IsColor() const;
95  public: bool IsTime() const;
96 
97  public: bool Set(const bool &_value);
98  public: bool Set(const int &_value);
99  public: bool Set(const unsigned int &_value);
100  public: bool Set(const float &_value);
101  public: bool Set(const double &_value);
102  public: bool Set(const char &_value);
103  public: bool Set(const std::string &_value);
104  public: bool Set(const char *_value);
105  public: bool Set(const gazebo::math::Vector3 &_value);
106  public: bool Set(const gazebo::math::Vector2i &_value);
107  public: bool Set(const gazebo::math::Vector2d &_value);
108  public: bool Set(const gazebo::math::Quaternion &_value);
109  public: bool Set(const gazebo::math::Pose &_value);
110  public: bool Set(const gazebo::common::Color &_value);
111  public: bool Set(const gazebo::common::Time &_value);
112 
113  public: bool Get(bool &_value);
114  public: bool Get(int &_value);
115  public: bool Get(unsigned int &_value);
116  public: bool Get(float &_value);
117  public: bool Get(double &_value);
118  public: bool Get(char &_value);
119  public: bool Get(std::string &_value);
120  public: bool Get(gazebo::math::Vector3 &_value);
121  public: bool Get(gazebo::math::Vector2i &_value);
122  public: bool Get(gazebo::math::Vector2d &_value);
123  public: bool Get(gazebo::math::Quaternion &_value);
124  public: bool Get(gazebo::math::Pose &_value);
125  public: bool Get(gazebo::common::Color &_value);
126  public: bool Get(gazebo::common::Time &_value);
127 
129  public: void SetDescription(const std::string &_desc);
130 
132  public: std::string GetDescription() const;
133 
135  private: static std::vector<Param*> *params;
136 
137  protected: std::string key;
138  protected: bool required;
139  protected: bool set;
140  protected: std::string typeName;
141  protected: std::string description;
142 
143  protected: boost::function<boost::any ()> updateFunc;
144  };
145 
146 
148  template< typename T>
149  class ParamT : public Param
150  {
152  public: ParamT(const std::string &_key, const std::string &_default,
153  bool _required, const std::string &_typeName = "",
154  const std::string &_description = "")
155  : Param(this)
156  {
157  this->key = _key;
158  this->required = _required;
159  if (_typeName.empty())
160  this->typeName = typeid(T).name();
161  else
162  this->typeName = _typeName;
163  this->description = _description;
164 
165  this->Set(_default);
166  this->defaultValue = this->value;
167  this->set = false;
168  }
169 
171  public: virtual ~ParamT() {}
172 
174  public: virtual void Update()
175  {
176  if (this->updateFunc)
177  {
178  try
179  {
180  const T v = boost::any_cast<T>(this->updateFunc());
181  Param::Set(v);
182  }
183  catch(boost::bad_any_cast &e)
184  {
185  gzerr << "boost any_cast error:" << e.what() << "\n";
186  }
187  }
188  }
189 
191  public: virtual std::string GetAsString() const
192  {
193  std::ostringstream stream;
194  stream << std::fixed << this->value;
195  return stream.str();
196  }
197 
199  public: virtual bool SetFromString(const std::string &_value)
200  { return this->Set(_value); }
201 
202  public: virtual std::string GetDefaultAsString() const
203  {
204  return boost::lexical_cast<std::string>(this->defaultValue);
205  }
206 
208  public: virtual bool Set(const std::string &_str)
209  {
210  std::string str = _str;
211  boost::trim(str);
212  if (str.empty() && this->required)
213  {
214  gzerr << "Empty string used when setting a required parameter. Key["
215  << this->GetKey() << "]\n";
216  return false;
217  }
218  else if (str.empty())
219  {
220  this->value = this->defaultValue;
221  return true;
222  }
223 
224  std::string tmp(str);
225  std::string lowerTmp(str);
226  boost::to_lower(lowerTmp);
227 
228  // "true" and "false" doesn't work properly
229  if (lowerTmp == "true")
230  tmp = "1";
231  else if (lowerTmp == "false")
232  tmp = "0";
233 
234  try
235  {
236  this->value = boost::lexical_cast<T>(tmp);
237  }
238  catch(boost::bad_lexical_cast &e)
239  {
240  if (str == "inf" || str == "-inf")
241  {
242  // in this case, the parser complains, but seems to assign the
243  // right values
244  gzmsg << "INFO [sdf::Param]: boost throws when lexical casting "
245  << "inf's, but the values are usually passed through correctly\n";
246  }
247  else
248  {
249  gzerr << "Unable to set value [" << str
250  << "] for key[" << this->key << "]\n";
251  return false;
252  }
253  }
254 
255  this->set = true;
256  return this->set;
257  }
258 
260  public: T GetValue() const
261  {
262  return this->value;
263  }
264 
266  public: T GetDefaultValue() const
267  {
268  return this->defaultValue;
269  }
270 
272  public: void SetValue(const T &_value)
273  {
274  this->value = _value;
275  this->set = true;
276  }
277 
279  public: virtual void Reset()
280  {
281  this->value = this->defaultValue;
282  this->set = false;
283  }
284 
285  public: virtual boost::shared_ptr<Param> Clone() const
286  {
287  boost::shared_ptr<ParamT<T> > clone(
288  new ParamT<T>(this->GetKey(), this->GetAsString(),
289  this->required, this->typeName,
290  this->description));
291  return clone;
292  }
293 
294  public: inline T operator*() const {return value;}
295  public: friend std::ostream &operator<<(std::ostream &_out,
296  const ParamT<T> &_p)
297  {
298  _out << _p.value;
299  return _out;
300  }
301 
302  protected: T value;
303  protected: T defaultValue;
304  };
305 }
306 #endif