All Classes Namespaces Files Functions Variables Typedefs Friends Macros Modules
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 #ifndef Q_MOC_RUN // See: https://bugreports.qt-project.org/browse/QTBUG-22829
22 # include <boost/lexical_cast.hpp>
23 # include <boost/bind.hpp>
24 # include <boost/algorithm/string.hpp>
25 # include <boost/variant.hpp>
26 # include <boost/any.hpp>
27 # include <boost/function.hpp>
28 # include <boost/shared_ptr.hpp>
29 #endif
30 
31 #include <typeinfo>
32 #include <string>
33 #include <vector>
34 
35 #include "sdf/Console.hh"
36 #include "sdf/Types.hh"
37 #include "sdf/system_util.hh"
38 
39 namespace sdf
40 {
42 
45  typedef boost::shared_ptr<Param> ParamPtr;
46 
49  typedef std::vector<ParamPtr> Param_V;
50 
54  {
62  public: Param(const std::string &_key, const std::string &_typeName,
63  const std::string &_default, bool _required,
64  const std::string &_description = "");
65 
67  public: virtual ~Param();
68 
71  public: std::string GetAsString() const;
72 
75  public: std::string GetDefaultAsString() const;
76 
79  public: bool SetFromString(const std::string &_value);
80 
82  public: void Reset();
83 
86  public: const std::string &GetKey() const {return this->key;}
87 
90  public: const std::type_info &GetType() const;
91 
94  public: const std::string &GetTypeName() const;
95 
98  public: bool GetRequired() const {return this->required;}
99 
102  public: bool GetSet() const {return this->set;}
103 
106  public: boost::shared_ptr<Param> Clone() const;
107 
111  public: template<typename T> void SetUpdateFunc(T _updateFunc)
112  {this->updateFunc = _updateFunc;}
113 
116  public: void Update();
117 
124  public: template<typename T>
125  bool Set(const T &_value)
126  {
127  try
128  {
129  this->SetFromString(boost::lexical_cast<std::string>(_value));
130  }
131  catch(...)
132  {
133  sdferr << "Unable to set parameter[" << this->key << "]."
134  << "Type type used must have a stream input and output"
135  << "operator, which allow boost::lexical_cast to"
136  << "function properly.\n";
137  return false;
138  }
139  return true;
140  }
141 
146  public: template<typename T>
147  bool Get(T &_value)
148  {
149  try
150  {
151  if (typeid(T) == typeid(bool) && this->typeName == "string")
152  {
153  std::string strValue =
154  boost::lexical_cast<std::string>(this->value);
155  if (strValue == "true" || strValue == "1")
156  _value = boost::lexical_cast<T>("1");
157  else
158  _value = boost::lexical_cast<T>("0");
159  }
160  else
161  {
162  _value = boost::lexical_cast<T>(this->value);
163  }
164  }
165  catch(...)
166  {
167  sdferr << "Unable to convert parameter[" << this->key << "] "
168  << "whose type is[" << this->typeName << "], to "
169  << "type[" << typeid(T).name() << "]\n";
170  return false;
171  }
172  return true;
173  }
174 
179  public: template<typename T>
180  bool GetDefault(T &_value)
181  {
182  try
183  {
184  _value = boost::lexical_cast<T>(this->defaultValue);
185  }
186  catch(...)
187  {
188  sdferr << "Unable to convert parameter[" << this->key << "] "
189  << "whose type is[" << this->typeName << "], to "
190  << "type[" << typeid(T).name() << "]\n";
191  return false;
192  }
193  return true;
194  }
195 
200  public: Param &operator =(const Param &_param)
201  {
202  this->value = _param.value;
203  this->defaultValue = _param.defaultValue;
204  return *this;
205  }
206 
209  public: void SetDescription(const std::string &_desc);
210 
213  public: std::string GetDescription() const;
214 
219  public: friend std::ostream &operator<<(std::ostream &_out,
220  const Param &_p)
221  {
222  _out << _p.GetAsString();
223  return _out;
224  }
225 
228  private: template<typename T>
229  void Init(const std::string &_value)
230  {
231  try
232  {
233  this->value = boost::lexical_cast<T>(_value);
234  }
235  catch(...)
236  {
237  if (this->typeName == "bool")
238  {
239  std::string strValue = _value;
240  boost::algorithm::to_lower(strValue);
241  if (strValue == "true" || strValue == "1")
242  this->value = true;
243  else
244  this->value = false;
245  }
246  else
247  sdferr << "Unable to init parameter value from string["
248  << _value << "]\n";
249  }
250 
251  this->defaultValue = this->value;
252  this->set = false;
253  }
254 
256  private: std::string key;
257 
259  private: bool required;
260 
262  private: bool set;
263 
265  private: std::string typeName;
266 
268  private: std::string description;
269 
271  private: boost::function<boost::any ()> updateFunc;
272 
275  private: typedef boost::variant<bool, char, std::string, int,
276  unsigned int, double, float, sdf::Vector3, sdf::Vector2i,
278  sdf::Time> ParamVariant;
279 
281  protected: ParamVariant value;
282 
284  protected: ParamVariant defaultValue;
285  };
286 }
287 #endif
Generic double x, y vector.
Definition: Types.hh:157
bool Get(T &_value)
Get the value of the parameter.
Definition: Param.hh:147
A parameter class.
Definition: Param.hh:53
ParamVariant defaultValue
This parameter's default value.
Definition: Param.hh:284
const std::string & GetKey() const
Get the key value.
Definition: Param.hh:86
bool Set(const T &_value)
Set the parameter's value.
Definition: Param.hh:125
bool GetDefault(T &_value)
Get the default value of the parameter.
Definition: Param.hh:180
ParamVariant value
This parameter's value.
Definition: Param.hh:281
A quaternion class.
Definition: Types.hh:310
bool GetRequired() const
Return whether the parameter is required.
Definition: Param.hh:98
friend std::ostream & operator<<(std::ostream &_out, const Param &_p)
Ostream operator.
Definition: Param.hh:219
class SDFORMAT_VISIBLE Param
Definition: Param.hh:41
void SetUpdateFunc(T _updateFunc)
Set the update function.
Definition: Param.hh:111
Generic integer x, y vector.
Definition: Types.hh:109
#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 GetAsString() const
Get the value as a string.
std::vector< ParamPtr > Param_V
Definition: Param.hh:49
Defines a color.
Definition: Types.hh:51
Param * ParamPtr
Definition: Param.hh:45
namespace for Simulation Description Format parser
Definition: Console.hh:29
bool GetSet() const
Return true if the parameter has been set.
Definition: Param.hh:102
A Time class, can be used to hold wall- or sim-time.
Definition: Types.hh:681
Encapsulates a position and rotation in three space.
Definition: Types.hh:582
The Vector3 class represents the generic vector containing 3 elements.
Definition: Types.hh:209