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  public: virtual void Update()
173  {
174  if (this->updateFunc)
175  {
176  const T v = boost::any_cast<T>(this->updateFunc());
177  Param::Set(v);
178  }
179  }
180 
182  public: virtual std::string GetAsString() const
183  {
184  std::ostringstream stream;
185  stream << std::fixed << this->value;
186  return stream.str();
187  }
188 
190  public: virtual bool SetFromString(const std::string &_value)
191  { return this->Set(_value); }
192 
193  public: virtual std::string GetDefaultAsString() const
194  {
195  return boost::lexical_cast<std::string>(this->defaultValue);
196  }
197 
199  public: virtual bool Set(const std::string &_str)
200  {
201  std::string str = _str;
202  boost::trim(str);
203  if (str.empty() && this->required)
204  {
205  gzerr << "Empty string used when setting a required parameter. Key["
206  << this->GetKey() << "]\n";
207  return false;
208  }
209  else if (str.empty())
210  {
211  this->value = this->defaultValue;
212  return true;
213  }
214 
215  std::string tmp(str);
216  std::string lowerTmp(str);
217  boost::to_lower(lowerTmp);
218 
219  // "true" and "false" doesn't work properly
220  if (lowerTmp == "true")
221  tmp = "1";
222  else if (lowerTmp == "false")
223  tmp = "0";
224 
225  try
226  {
227  this->value = boost::lexical_cast<T>(tmp);
228  }
229  catch(boost::bad_lexical_cast &e)
230  {
231  if (str == "inf" || str == "-inf")
232  {
233  // in this case, the parser complains, but seems to assign the
234  // right values
235  gzmsg << "INFO [sdf::Param]: boost throws when lexical casting "
236  << "inf's, but the values are usually passed through correctly\n";
237  }
238  else
239  {
240  gzerr << "Unable to set value [" << str
241  << "] for key[" << this->key << "]\n";
242  return false;
243  }
244  }
245 
246  this->set = true;
247  return this->set;
248  }
249 
251  public: T GetValue() const
252  {
253  return this->value;
254  }
255 
257  public: T GetDefaultValue() const
258  {
259  return this->defaultValue;
260  }
261 
263  public: void SetValue(const T &_value)
264  {
265  this->value = _value;
266  this->set = true;
267  }
268 
270  public: virtual void Reset()
271  {
272  this->value = this->defaultValue;
273  this->set = false;
274  }
275 
276  public: virtual boost::shared_ptr<Param> Clone() const
277  {
278  boost::shared_ptr<ParamT<T> > clone(
279  new ParamT<T>(this->GetKey(), this->GetAsString(),
280  this->required, this->typeName,
281  this->description));
282  return clone;
283  }
284 
285  public: inline T operator*() const {return value;}
286  public: friend std::ostream &operator<<(std::ostream &_out,
287  const ParamT<T> &_p)
288  {
289  _out << _p.value;
290  return _out;
291  }
292 
293  protected: T value;
294  protected: T defaultValue;
295  };
296 }
297 #endif