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 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 SDFORMAT_PARAM_HH_
19 #define SDFORMAT_PARAM_HH_
20 
21 // See: https://bugreports.qt-project.org/browse/QTBUG-22829
22 #ifndef Q_MOC_RUN
23  #include <boost/any.hpp>
24  #include <boost/variant.hpp>
25 #endif
26 
27 #include <algorithm>
28 #include <cstdint>
29 #include <functional>
30 #include <memory>
31 #include <sstream>
32 #include <string>
33 #include <typeinfo>
34 #include <vector>
35 
36 #include <ignition/math.hh>
37 
38 #include "sdf/Console.hh"
39 #include "sdf/system_util.hh"
40 #include "sdf/Types.hh"
41 
42 #ifdef _WIN32
43 // Disable warning C4251 which is triggered by
44 // std::unique_ptr
45 #pragma warning(push)
46 #pragma warning(disable: 4251)
47 #endif
48 
49 namespace sdf
50 {
52 
55  typedef std::shared_ptr<Param> ParamPtr;
56 
59  typedef std::vector<ParamPtr> Param_V;
60 
62  class ParamPrivate;
63 
67  {
76  public: Param(const std::string &_key, const std::string &_typeName,
77  const std::string &_default, bool _required,
78  const std::string &_description = "");
79 
81  public: virtual ~Param();
82 
85  public: std::string GetAsString() const;
86 
89  public: std::string GetDefaultAsString() const;
90 
93  public: bool SetFromString(const std::string &_value);
94 
96  public: void Reset();
97 
100  public: const std::string &GetKey() const;
101 
105  public: template<typename Type>
106  bool IsType() const;
107 
110  public: const std::string &GetTypeName() const;
111 
114  public: bool GetRequired() const;
115 
118  public: bool GetSet() const;
119 
122  public: ParamPtr Clone() const;
123 
127  public: template<typename T>
128  void SetUpdateFunc(T _updateFunc);
129 
132  public: void Update();
133 
139  public: template<typename T>
140  bool Set(const T &_value);
141 
145  public: bool GetAny(boost::any &_anyVal) const;
146 
151  public: template<typename T>
152  bool Get(T &_value) const;
153 
158  public: template<typename T>
159  bool GetDefault(T &_value) const;
160 
165  public: Param &operator=(const Param &_param);
166 
169  public: void SetDescription(const std::string &_desc);
170 
173  public: std::string GetDescription() const;
174 
179  public: friend std::ostream &operator<<(std::ostream &_out,
180  const Param &_p)
181  {
182  _out << _p.GetAsString();
183  return _out;
184  }
185 
188  private: bool ValueFromString(const std::string &_value);
189 
191  private: std::unique_ptr<ParamPrivate> dataPtr;
192  };
193 
197  {
199  public: std::string key;
200 
202  public: bool required;
203 
205  public: bool set;
206 
208  public: std::string typeName;
209 
211  public: std::string description;
212 
214  public: std::function<boost::any ()> updateFunc;
215 
218  public: typedef boost::variant<bool, char, std::string, int, std::uint64_t,
219  unsigned int, double, float, sdf::Time,
220  ignition::math::Color,
221  ignition::math::Vector2i,
222  ignition::math::Vector2d,
223  ignition::math::Vector3d,
224  ignition::math::Quaterniond,
225  ignition::math::Pose3d> ParamVariant;
226 
229 
232  };
233 
235  template<typename T>
236  void Param::SetUpdateFunc(T _updateFunc)
237  {
238  this->dataPtr->updateFunc = _updateFunc;
239  }
240 
242  template<typename T>
243  bool Param::Set(const T &_value)
244  {
245  try
246  {
247  std::stringstream ss;
248  ss << _value;
249  return this->SetFromString(ss.str());
250  }
251  catch(...)
252  {
253  sdferr << "Unable to set parameter["
254  << this->dataPtr->key << "]."
255  << "Type used must have a stream input and output operator,"
256  << "which allows proper functioning of Param.\n";
257  return false;
258  }
259  }
260 
262  template<typename T>
263  bool Param::Get(T &_value) const
264  {
265  try
266  {
267  if (typeid(T) == typeid(bool) && this->dataPtr->typeName == "string")
268  {
269  std::stringstream ss;
270  ss << this->dataPtr->value;
271 
272  std::string strValue;
273 
274  ss >> strValue;
275  std::transform(strValue.begin(), strValue.end(),
276  strValue.begin(), ::tolower);
277 
278  std::stringstream tmp;
279  if (strValue == "true" || strValue == "1")
280  {
281  tmp << "1";
282  }
283  else
284  {
285  tmp << "0";
286  }
287  tmp >> _value;
288  }
289  else if (typeid(T) == this->dataPtr->value.type())
290  {
291 #if BOOST_VERSION < 105800
292  _value = boost::get<T>(this->dataPtr->value);
293 #else
294  _value = boost::relaxed_get<T>(this->dataPtr->value);
295 #endif
296  }
297  else
298  {
299  std::stringstream ss;
300  ss << this->dataPtr->value;
301  ss >> _value;
302  }
303  }
304  catch(...)
305  {
306  sdferr << "Unable to convert parameter["
307  << this->dataPtr->key << "] "
308  << "whose type is["
309  << this->dataPtr->typeName << "], to "
310  << "type[" << typeid(T).name() << "]\n";
311  return false;
312  }
313  return true;
314  }
315 
317  template<typename T>
318  bool Param::GetDefault(T &_value) const
319  {
320  std::stringstream ss;
321 
322  try
323  {
324  ss << this->dataPtr->defaultValue;
325  ss >> _value;
326  }
327  catch(...)
328  {
329  sdferr << "Unable to convert parameter["
330  << this->dataPtr->key << "] "
331  << "whose type is["
332  << this->dataPtr->typeName << "], to "
333  << "type[" << typeid(T).name() << "]\n";
334  return false;
335  }
336 
337  return true;
338  }
339 
341  template<typename Type>
342  bool Param::IsType() const
343  {
344  return this->dataPtr->value.type() == typeid(Type);
345  }
346 }
347 
348 #ifdef _WIN32
349 #pragma warning(pop)
350 #endif
351 
352 #endif
boost::variant< bool, char, std::string, int, std::uint64_t, unsigned int, double, float, sdf::Time, ignition::math::Color, ignition::math::Vector2i, ignition::math::Vector2d, ignition::math::Vector3d, ignition::math::Quaterniond, ignition::math::Pose3d > ParamVariant
Definition: Param.hh:225
A parameter class.
Definition: Param.hh:66
bool Set(const T &_value)
Set the parameter's value.
Definition: Param.hh:243
ParamVariant value
This parameter's value.
Definition: Param.hh:228
ParamVariant defaultValue
This parameter's default value.
Definition: Param.hh:231
friend std::ostream & operator<<(std::ostream &_out, const Param &_p)
Ostream operator.
Definition: Param.hh:179
class SDFORMAT_VISIBLE Param
Definition: Param.hh:51
void SetUpdateFunc(T _updateFunc)
Set the update function.
Definition: Param.hh:236
bool set
True if the parameter is set.
Definition: Param.hh:205
std::string typeName
Definition: Param.hh:208
std::function< boost::any()> updateFunc
Update function pointer.
Definition: Param.hh:214
bool Get(T &_value) const
Get the value of the parameter.
Definition: Param.hh:263
bool required
True if the parameter is required.
Definition: Param.hh:202
#define SDFORMAT_VISIBLE
Use to represent "symbol visible" if supported.
Definition: system_util.hh:48
#define sdferr
Output an error message.
Definition: Console.hh:52
std::string description
Description of the parameter.
Definition: Param.hh:211
std::shared_ptr< Param > ParamPtr
Definition: Param.hh:55
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:59
bool IsType() const
Return true if the param is a particular type.
Definition: Param.hh:342
A Time class, can be used to hold wall- or sim-time.
Definition: Types.hh:133
std::string key
Key value.
Definition: Param.hh:199
Definition: Param.hh:196
bool GetDefault(T &_value) const
Get the default value of the parameter.
Definition: Param.hh:318