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 // See: https://bugreports.qt-project.org/browse/QTBUG-22829
22 #ifndef Q_MOC_RUN
23  #include <boost/lexical_cast.hpp>
24  #include <boost/any.hpp>
25  #include <boost/variant.hpp>
26  #include <boost/version.hpp>
27 #endif
28 
29 #include <memory>
30 #include <functional>
31 #include <algorithm>
32 #include <typeinfo>
33 #include <string>
34 #include <vector>
35 #include <ignition/math.hh>
36 
37 #include "sdf/Console.hh"
38 #include "sdf/system_util.hh"
39 #include "sdf/Types.hh"
40 
41 namespace sdf
42 {
44 
47  typedef std::shared_ptr<Param> ParamPtr;
48 
51  typedef std::vector<ParamPtr> Param_V;
52 
54  class ParamPrivate;
55 
59  {
67  public: Param(const std::string &_key, const std::string &_typeName,
68  const std::string &_default, bool _required,
69  const std::string &_description = "");
70 
72  public: virtual ~Param();
73 
76  public: std::string GetAsString() const;
77 
80  public: std::string GetDefaultAsString() const;
81 
84  public: bool SetFromString(const std::string &_value);
85 
87  public: void Reset();
88 
91  public: const std::string &GetKey() const;
92 
96  public: template<typename Type>
97  bool IsType() const;
98 
101  public: const std::string &GetTypeName() const;
102 
105  public: bool GetRequired() const;
106 
109  public: bool GetSet() const;
110 
113  public: ParamPtr Clone() const;
114 
118  public: template<typename T>
119  void SetUpdateFunc(T _updateFunc);
120 
123  public: void Update();
124 
131  public: template<typename T>
132  bool Set(const T &_value);
133 
137  public: bool GetAny(boost::any &_anyVal) const;
138 
143  public: template<typename T>
144  bool Get(T &_value) const;
145 
150  public: template<typename T>
151  bool GetDefault(T &_value) const;
152 
157  public: Param &operator=(const Param &_param);
158 
161  public: void SetDescription(const std::string &_desc);
162 
165  public: std::string GetDescription() const;
166 
171  public: friend std::ostream &operator<<(std::ostream &_out,
172  const Param &_p)
173  {
174  _out << _p.GetAsString();
175  return _out;
176  }
177 
180  private: template<typename T>
181  void Init(const std::string &_value);
182 
184  private: ParamPrivate *dataPtr;
185  };
186 
190  {
192  public: std::string key;
193 
195  public: bool required;
196 
198  public: bool set;
199 
201  public: std::string typeName;
202 
204  public: std::string description;
205 
207  public: std::function<boost::any ()> updateFunc;
208 
211  public: typedef boost::variant<bool, char, std::string, int, uint64_t,
212  unsigned int, double, float, sdf::Time, sdf::Color,
213  ignition::math::Vector3d, ignition::math::Vector2i,
214  ignition::math::Vector2d, ignition::math::Quaterniond,
215  ignition::math::Pose3d> ParamVariant;
216 
219 
222  };
223 
225  template<typename T>
226  void Param::SetUpdateFunc(T _updateFunc)
227  {
228  this->dataPtr->updateFunc = _updateFunc;
229  }
230 
232  template<typename T>
233  bool Param::Set(const T &_value)
234  {
235  try
236  {
237  this->SetFromString(boost::lexical_cast<std::string>(_value));
238  }
239  catch(...)
240  {
241  sdferr << "Unable to set parameter["
242  << this->dataPtr->key << "]."
243  << "Type type used must have a stream input and output"
244  << "operator, which allow boost::lexical_cast to"
245  << "function properly.\n";
246  return false;
247  }
248  return true;
249  }
250 
252  template<typename T>
253  bool Param::Get(T &_value) const
254  {
255  try
256  {
257  if (typeid(T) == typeid(bool) &&
258  this->dataPtr->typeName == "string")
259  {
260  std::string strValue =
261  boost::lexical_cast<std::string>(this->dataPtr->value);
262  std::transform(strValue.begin(), strValue.end(),
263  strValue.begin(), ::tolower);
264  if (strValue == "true" || strValue == "1")
265  _value = boost::lexical_cast<T>("1");
266  else
267  _value = boost::lexical_cast<T>("0");
268  }
269  else if (typeid(T) == this->dataPtr->value.type())
270  {
271 #if BOOST_VERSION < 105800
272  _value = boost::get<T>(this->dataPtr->value);
273 #else
274  _value = boost::relaxed_get<T>(this->dataPtr->value);
275 #endif
276  }
277  else
278  {
279  _value = boost::lexical_cast<T>(this->dataPtr->value);
280  }
281  }
282  catch(...)
283  {
284  sdferr << "Unable to convert parameter["
285  << this->dataPtr->key << "] "
286  << "whose type is["
287  << this->dataPtr->typeName << "], to "
288  << "type[" << typeid(T).name() << "]\n";
289  return false;
290  }
291  return true;
292  }
293 
295  template<typename T>
296  bool Param::GetDefault(T &_value) const
297  {
298  try
299  {
300  _value = boost::lexical_cast<T>(this->dataPtr->defaultValue);
301  }
302  catch(...)
303  {
304  sdferr << "Unable to convert parameter["
305  << this->dataPtr->key << "] "
306  << "whose type is["
307  << this->dataPtr->typeName << "], to "
308  << "type[" << typeid(T).name() << "]\n";
309  return false;
310  }
311  return true;
312  }
313 
315  template<typename T>
316  void Param::Init(const std::string &_value)
317  {
318  try
319  {
320  this->dataPtr->value = boost::lexical_cast<T>(_value);
321  }
322  catch(...)
323  {
324  if (this->dataPtr->typeName == "bool")
325  {
326  std::string strValue = _value;
327  std::transform(strValue.begin(), strValue.end(),
328  strValue.begin(), ::tolower);
329  if (strValue == "true" || strValue == "1")
330  this->dataPtr->value = true;
331  else
332  this->dataPtr->value = false;
333  }
334  else
335  {
336  sdferr << "Unable to init parameter value from string["
337  << _value << "]\n";
338  }
339  }
340 
341  this->dataPtr->defaultValue = this->dataPtr->value;
342  this->dataPtr->set = false;
343  }
344 
346  template<typename Type>
347  bool Param::IsType() const
348  {
349  return this->dataPtr->value.type() == typeid(Type);
350  }
351 }
352 #endif
A parameter class.
Definition: Param.hh:58
bool Set(const T &_value)
Set the parameter's value.
Definition: Param.hh:233
ParamVariant value
This parameter's value.
Definition: Param.hh:218
ParamVariant defaultValue
This parameter's default value.
Definition: Param.hh:221
friend std::ostream & operator<<(std::ostream &_out, const Param &_p)
Ostream operator.
Definition: Param.hh:171
class SDFORMAT_VISIBLE Param
Definition: Param.hh:43
boost::variant< bool, char, std::string, int, uint64_t, unsigned int, double, float, sdf::Time, sdf::Color, ignition::math::Vector3d, ignition::math::Vector2i, ignition::math::Vector2d, ignition::math::Quaterniond, ignition::math::Pose3d > ParamVariant
Definition: Param.hh:215
void SetUpdateFunc(T _updateFunc)
Set the update function.
Definition: Param.hh:226
bool set
True if the parameter is set.
Definition: Param.hh:198
std::string typeName
Definition: Param.hh:201
std::function< boost::any()> updateFunc
Update function pointer.
Definition: Param.hh:207
bool Get(T &_value) const
Get the value of the parameter.
Definition: Param.hh:253
bool required
True if the parameter is required.
Definition: Param.hh:195
#define SDFORMAT_VISIBLE
Use to represent "symbol visible" if supported.
Definition: system_util.hh:48
#define sdferr
Output an error message.
Definition: Console.hh:54
std::string description
Description of the parameter.
Definition: Param.hh:204
std::shared_ptr< Param > ParamPtr
Definition: Param.hh:47
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:51
Defines a color.
Definition: Types.hh:61
bool IsType() const
Return true if the param is a particular type.
Definition: Param.hh:347
A Time class, can be used to hold wall- or sim-time.
Definition: Types.hh:120
std::string key
Key value.
Definition: Param.hh:192
Definition: Param.hh:189
bool GetDefault(T &_value) const
Get the default value of the parameter.
Definition: Param.hh:296