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 #ifndef Q_MOC_RUN // See: https://bugreports.qt-project.org/browse/QTBUG-22829
22 # include <boost/lexical_cast.hpp>
23 #endif
24 #include <boost/bind.hpp>
25 #include <boost/algorithm/string.hpp>
26 #include <boost/variant.hpp>
27 #include <boost/any.hpp>
28 #include <boost/function.hpp>
29 #include <boost/shared_ptr.hpp>
30 
31 #include <typeinfo>
32 #include <string>
33 #include <vector>
34 
35 #include "sdf/Console.hh"
36 #include "sdf/Types.hh"
37 
38 namespace sdf
39 {
40  class Param;
41 
44  typedef boost::shared_ptr<Param> ParamPtr;
45 
48  typedef std::vector<ParamPtr> Param_V;
49 
52  class Param
53  {
61  public: Param(const std::string &_key, const std::string &_typeName,
62  const std::string &_default, bool _required,
63  const std::string &_description = "");
64 
66  public: virtual ~Param();
67 
70  public: std::string GetAsString() const;
71 
74  public: std::string GetDefaultAsString() const;
75 
78  public: bool SetFromString(const std::string &_value);
79 
81  public: void Reset();
82 
85  public: const std::string &GetKey() const {return this->key;}
86 
89  public: const std::type_info &GetType() const;
90 
93  public: const std::string &GetTypeName() const;
94 
97  public: bool GetRequired() const {return this->required;}
98 
101  public: bool GetSet() const {return this->set;}
102 
105  public: boost::shared_ptr<Param> Clone() const;
106 
110  public: template<typename T> void SetUpdateFunc(T _updateFunc)
111  {this->updateFunc = _updateFunc;}
112 
115  public: void Update();
116 
123  public: template<typename T>
124  bool Set(const T &_value)
125  {
126  try
127  {
128  this->SetFromString(boost::lexical_cast<std::string>(_value));
129  }
130  catch(...)
131  {
132  sdferr << "Unable to set parameter[" << this->key << "]."
133  << "Type type used must have a stream input and output"
134  << "operator, which allow boost::lexical_cast to"
135  << "function properly.\n";
136  return false;
137  }
138  return true;
139  }
140 
145  public: template<typename T>
146  bool Get(T &_value)
147  {
148  try
149  {
150  _value = boost::lexical_cast<T>(this->value);
151  }
152  catch(...)
153  {
154  sdferr << "Unable to convert parameter[" << this->key << "] "
155  << "whose type is[" << this->typeName << "], to "
156  << "type[" << typeid(T).name() << "]\n";
157  return false;
158  }
159  return true;
160  }
161 
166  public: template<typename T>
167  bool GetDefault(T &_value)
168  {
169  try
170  {
171  _value = boost::lexical_cast<T>(this->defaultValue);
172  }
173  catch(...)
174  {
175  sdferr << "Unable to convert parameter[" << this->key << "] "
176  << "whose type is[" << this->typeName << "], to "
177  << "type[" << typeid(T).name() << "]\n";
178  return false;
179  }
180  return true;
181  }
182 
187  public: Param &operator =(const Param &_param)
188  {
189  this->value = _param.value;
190  this->defaultValue = _param.defaultValue;
191  return *this;
192  }
193 
196  public: void SetDescription(const std::string &_desc);
197 
200  public: std::string GetDescription() const;
201 
206  public: friend std::ostream &operator<<(std::ostream &_out,
207  const Param &_p)
208  {
209  _out << _p.GetAsString();
210  return _out;
211  }
212 
215  private: template<typename T>
216  void Init(const std::string &_value)
217  {
218  try
219  {
220  this->value = boost::lexical_cast<T>(_value);
221  }
222  catch(...)
223  {
224  if (this->typeName == "bool")
225  {
226  std::string strValue = _value;
227  boost::algorithm::to_lower(strValue);
228  if (strValue == "true" || strValue == "1")
229  this->value = true;
230  else
231  this->value = false;
232  }
233  else
234  sdferr << "Unable to init parameter value from string["
235  << _value << "]\n";
236  }
237 
238  this->defaultValue = this->value;
239  this->set = false;
240  }
241 
243  private: std::string key;
244 
246  private: bool required;
247 
249  private: bool set;
250 
252  private: std::string typeName;
253 
255  private: std::string description;
256 
258  private: boost::function<boost::any ()> updateFunc;
259 
262  private: typedef boost::variant<bool, char, std::string, int,
263  unsigned int, double, float, sdf::Vector3, sdf::Vector2i,
265  sdf::Time> ParamVariant;
266 
268  protected: ParamVariant value;
269 
271  protected: ParamVariant defaultValue;
272  };
273 }
274 #endif
Generic double x, y vector.
Definition: Types.hh:155
std::string GetDefaultAsString() const
Get the default value as a string.
const std::string & GetTypeName() const
Get the type name value.
void SetDescription(const std::string &_desc)
Set the description of the parameter.
bool Get(T &_value)
Get the value of the parameter.
Definition: Param.hh:146
A parameter class.
Definition: Param.hh:52
ParamVariant defaultValue
This parameter's default value.
Definition: Param.hh:271
const std::string & GetKey() const
Get the key value.
Definition: Param.hh:85
bool Set(const T &_value)
Set the parameter's value.
Definition: Param.hh:124
std::string GetDescription() const
Get the description of the parameter.
bool GetDefault(T &_value)
Get the default value of the parameter.
Definition: Param.hh:167
ParamVariant value
This parameter's value.
Definition: Param.hh:268
A quaternion class.
Definition: Types.hh:308
bool GetRequired() const
Return whether the parameter is required.
Definition: Param.hh:97
friend std::ostream & operator<<(std::ostream &_out, const Param &_p)
Ostream operator.
Definition: Param.hh:206
void SetUpdateFunc(T _updateFunc)
Set the update function.
Definition: Param.hh:110
Generic integer x, y vector.
Definition: Types.hh:107
virtual ~Param()
Destructor.
Param & operator=(const Param &_param)
Equal operator.
Definition: Param.hh:187
void Update()
Set the parameter's value using the updateFunc.
#define sdferr
Output an error message.
Definition: Console.hh:45
Param(const std::string &_key, const std::string &_typeName, const std::string &_default, bool _required, const std::string &_description="")
Constructor.
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:48
Param * Clone() const
Clone the parameter.
void Reset()
Reset the parameter to the default value.
const std::type_info & GetType() const
Get the type of the value stored.
Defines a color.
Definition: Types.hh:49
Param * ParamPtr
Definition: Param.hh:44
bool GetSet() const
Return true if the parameter has been set.
Definition: Param.hh:101
A Time class, can be used to hold wall- or sim-time.
Definition: Types.hh:679
Encapsulates a position and rotation in three space.
Definition: Types.hh:580
The Vector3 class represents the generic vector containing 3 elements.
Definition: Types.hh:207