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 #include <boost/any.hpp>
22 #ifdef __clang__
23  #pragma clang diagnostic push
24  #pragma clang diagnostic ignored "-Wc++98-c++11-compat"
25 #endif
26 #include <boost/variant.hpp>
27 #ifdef __clang__
28  #pragma clang diagnostic pop
29 #endif
30 
31 #include <algorithm>
32 #include <cctype>
33 #include <cstdint>
34 #include <functional>
35 #include <memory>
36 #include <sstream>
37 #include <string>
38 #include <typeinfo>
39 #include <vector>
40 
41 #include <ignition/math.hh>
42 
43 #include "sdf/Console.hh"
44 #include "sdf/system_util.hh"
45 #include "sdf/Types.hh"
46 
47 #ifdef _WIN32
48 // Disable warning C4251 which is triggered by
49 // std::unique_ptr
50 #pragma warning(push)
51 #pragma warning(disable: 4251)
52 #endif
53 
54 namespace sdf
55 {
57 
60  typedef std::shared_ptr<Param> ParamPtr;
61 
64  typedef std::vector<ParamPtr> Param_V;
65 
67  class ParamPrivate;
68 
72  {
81  public: Param(const std::string &_key, const std::string &_typeName,
82  const std::string &_default, bool _required,
83  const std::string &_description = "");
84 
86  public: virtual ~Param();
87 
90  public: std::string GetAsString() const;
91 
94  public: std::string GetDefaultAsString() const;
95 
98  public: bool SetFromString(const std::string &_value);
99 
101  public: void Reset();
102 
105  public: const std::string &GetKey() const;
106 
110  public: template<typename Type>
111  bool IsType() const;
112 
115  public: const std::string &GetTypeName() const;
116 
119  public: bool GetRequired() const;
120 
123  public: bool GetSet() const;
124 
127  public: ParamPtr Clone() const;
128 
132  public: template<typename T>
133  void SetUpdateFunc(T _updateFunc);
134 
137  public: void Update();
138 
144  public: template<typename T>
145  bool Set(const T &_value);
146 
150  public: bool GetAny(boost::any &_anyVal) const;
151 
156  public: template<typename T>
157  bool Get(T &_value) const;
158 
163  public: template<typename T>
164  bool GetDefault(T &_value) const;
165 
170  public: Param &operator=(const Param &_param);
171 
174  public: void SetDescription(const std::string &_desc);
175 
178  public: std::string GetDescription() const;
179 
184  public: friend std::ostream &operator<<(std::ostream &_out,
185  const Param &_p)
186  {
187  _out << _p.GetAsString();
188  return _out;
189  }
190 
193  private: bool ValueFromString(const std::string &_value);
194 
196  private: std::unique_ptr<ParamPrivate> dataPtr;
197  };
198 
202  {
204  public: std::string key;
205 
207  public: bool required;
208 
210  public: bool set;
211 
213  public: std::string typeName;
214 
216  public: std::string description;
217 
219  public: std::function<boost::any ()> updateFunc;
220 
223  public: typedef boost::variant<bool, char, std::string, int, std::uint64_t,
224  unsigned int, double, float, sdf::Time,
225  ignition::math::Color,
226  ignition::math::Vector2i,
227  ignition::math::Vector2d,
228  ignition::math::Vector3d,
229  ignition::math::Quaterniond,
230  ignition::math::Pose3d> ParamVariant;
231 
234 
237  };
238 
240  template<typename T>
241  void Param::SetUpdateFunc(T _updateFunc)
242  {
243  this->dataPtr->updateFunc = _updateFunc;
244  }
245 
247  template<typename T>
248  bool Param::Set(const T &_value)
249  {
250  try
251  {
252  std::stringstream ss;
253  ss << _value;
254  return this->SetFromString(ss.str());
255  }
256  catch(...)
257  {
258  sdferr << "Unable to set parameter["
259  << this->dataPtr->key << "]."
260  << "Type used must have a stream input and output operator,"
261  << "which allows proper functioning of Param.\n";
262  return false;
263  }
264  }
265 
267  template<typename T>
268  bool Param::Get(T &_value) const
269  {
270  try
271  {
272  if (typeid(T) == typeid(bool) && this->dataPtr->typeName == "string")
273  {
274  std::stringstream ss;
275  ss << this->dataPtr->value;
276 
277  std::string strValue;
278 
279  ss >> strValue;
280  std::transform(strValue.begin(), strValue.end(), strValue.begin(),
281  [](unsigned char c)
282  {
283  return static_cast<unsigned char>(std::tolower(c));
284  });
285 
286  std::stringstream tmp;
287  if (strValue == "true" || strValue == "1")
288  {
289  tmp << "1";
290  }
291  else
292  {
293  tmp << "0";
294  }
295  tmp >> _value;
296  }
297  else if (typeid(T) == this->dataPtr->value.type())
298  {
299 #if BOOST_VERSION < 105800
300  _value = boost::get<T>(this->dataPtr->value);
301 #else
302  _value = boost::relaxed_get<T>(this->dataPtr->value);
303 #endif
304  }
305  else
306  {
307  std::stringstream ss;
308  ss << this->dataPtr->value;
309  ss >> _value;
310  }
311  }
312  catch(...)
313  {
314  sdferr << "Unable to convert parameter["
315  << this->dataPtr->key << "] "
316  << "whose type is["
317  << this->dataPtr->typeName << "], to "
318  << "type[" << typeid(T).name() << "]\n";
319  return false;
320  }
321  return true;
322  }
323 
325  template<typename T>
326  bool Param::GetDefault(T &_value) const
327  {
328  std::stringstream ss;
329 
330  try
331  {
332  ss << this->dataPtr->defaultValue;
333  ss >> _value;
334  }
335  catch(...)
336  {
337  sdferr << "Unable to convert parameter["
338  << this->dataPtr->key << "] "
339  << "whose type is["
340  << this->dataPtr->typeName << "], to "
341  << "type[" << typeid(T).name() << "]\n";
342  return false;
343  }
344 
345  return true;
346  }
347 
349  template<typename Type>
350  bool Param::IsType() const
351  {
352  return this->dataPtr->value.type() == typeid(Type);
353  }
354 }
355 
356 #ifdef _WIN32
357 #pragma warning(pop)
358 #endif
359 
360 #endif
sdf::ParamPrivate::ParamVariant
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:230
sdf::Time
A Time class, can be used to hold wall- or sim-time.
Definition: Types.hh:133
sdf
namespace for Simulation Description Format parser
Definition: Console.hh:35
Console.hh
sdf::Param
A parameter class.
Definition: Param.hh:71
sdf::ParamPrivate::typeName
std::string typeName
Definition: Param.hh:213
sdf::ParamPrivate::required
bool required
True if the parameter is required.
Definition: Param.hh:207
sdf::Param::SetUpdateFunc
void SetUpdateFunc(T _updateFunc)
Set the update function.
Definition: Param.hh:241
sdf::Param::Set
bool Set(const T &_value)
Set the parameter's value.
Definition: Param.hh:248
sdf::ParamPrivate::value
ParamVariant value
This parameter's value.
Definition: Param.hh:233
Types.hh
SDFORMAT_VISIBLE
#define SDFORMAT_VISIBLE
Definition: system_util.hh:48
sdf::ParamPrivate::updateFunc
std::function< boost::any()> updateFunc
Update function pointer.
Definition: Param.hh:219
sdf::Param::GetAsString
std::string GetAsString() const
Get the value as a string.
sdf::ParamPrivate::set
bool set
True if the parameter is set.
Definition: Param.hh:210
sdf::Param_V
std::vector< ParamPtr > Param_V
Definition: Param.hh:64
sdf::ParamPtr
std::shared_ptr< Param > ParamPtr
Definition: Param.hh:60
sdf::ParamPrivate::defaultValue
ParamVariant defaultValue
This parameter's default value.
Definition: Param.hh:236
sdf::ParamPrivate::key
std::string key
Key value.
Definition: Param.hh:204
sdf::Param::SetFromString
bool SetFromString(const std::string &_value)
Set the parameter value from a string.
system_util.hh
sdf::Param::IsType
bool IsType() const
Return true if the param is a particular type.
Definition: Param.hh:350
sdf::Param::Get
bool Get(T &_value) const
Get the value of the parameter.
Definition: Param.hh:268
sdf::Param
class SDFORMAT_VISIBLE Param
Definition: Param.hh:56
sdferr
#define sdferr
Output an error message.
Definition: Console.hh:52
sdf::ParamPrivate
Definition: Param.hh:201
sdf::ParamPrivate::description
std::string description
Description of the parameter.
Definition: Param.hh:216
sdf::Param::operator<<
friend std::ostream & operator<<(std::ostream &_out, const Param &_p)
Ostream operator.
Definition: Param.hh:184
sdf::Param::GetDefault
bool GetDefault(T &_value) const
Get the default value of the parameter.
Definition: Param.hh:326