GenericParam.h
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2011, Willow Garage
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the Willow Garage nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34 
35 /* Author: Ioan Sucan */
36 
37 #ifndef OMPL_BASE_GENERIC_PARAM_
38 #define OMPL_BASE_GENERIC_PARAM_
39 
40 #include "ompl/util/Console.h"
41 #include "ompl/util/ClassForward.h"
42 #include <boost/function.hpp>
43 #include <boost/lexical_cast.hpp>
44 #include <boost/type_traits.hpp>
45 #include <iostream>
46 #include <string>
47 #include <vector>
48 #include <map>
49 
50 namespace ompl
51 {
52  namespace base
53  {
54 
56 
57  OMPL_CLASS_FORWARD(GenericParam);
59 
66  {
67  public:
68 
70  GenericParam(const std::string &name) : name_(name)
71  {
72  }
73 
74  virtual ~GenericParam()
75  {
76  }
77 
79  const std::string& getName() const
80  {
81  return name_;
82  }
83 
85  void setName(const std::string &name)
86  {
87  name_ = name;
88  }
89 
91  virtual bool setValue(const std::string &value) = 0;
92 
94  virtual std::string getValue() const = 0;
95 
97  template<typename T>
98  GenericParam& operator=(const T &value)
99  {
100  try
101  {
102  setValue(boost::lexical_cast<std::string>(value));
103  }
104  catch (boost::bad_lexical_cast &e)
105  {
106  OMPL_WARN("Invalid value format specified for parameter '%s': %s", name_.c_str(), e.what());
107  }
108  return *this;
109  }
110 
112  void setRangeSuggestion(const std::string &rangeSuggestion)
113  {
114  rangeSuggestion_ = rangeSuggestion;
115  }
116 
118  const std::string& getRangeSuggestion() const
119  {
120  return rangeSuggestion_;
121  }
122 
123  protected:
124 
127  template<typename T>
128  const std::string& maybeWrapBool(const std::string &value) const
129  {
130  return boost::is_same<T, bool>::value ? truthValueTo01Str(value) : value;
131  }
132 
134  std::string name_;
135 
149  std::string rangeSuggestion_;
150 
151  private:
153  static const std::string& truthValueTo01Str(const std::string &value);
154  };
155 
156 
158  template<typename T>
160  {
161  public:
162 
164  typedef boost::function<void(T)> SetterFn;
165 
167  typedef boost::function<T()> GetterFn;
168 
171  SpecificParam(const std::string &name, const SetterFn &setter, const GetterFn &getter = GetterFn()) :
172  GenericParam(name), setter_(setter), getter_(getter)
173  {
174  if (!setter_ && !getter_)
175  OMPL_ERROR("At least one setter or getter function must be specified for parameter");
176  }
177 
178  virtual ~SpecificParam()
179  {
180  }
181 
182  virtual bool setValue(const std::string &value)
183  {
184  bool result = true;
185  try
186  {
187  if (setter_)
188  setter_(boost::lexical_cast<T>(GenericParam::maybeWrapBool<T>(value)));
189  }
190  catch (boost::bad_lexical_cast &e)
191  {
192  result = false;
193  OMPL_WARN("Invalid value format specified for parameter '%s': %s", name_.c_str(), e.what());
194  }
195 
196  if (getter_)
197  OMPL_DEBUG("The value of parameter '%s' is now: '%s'", name_.c_str(), getValue().c_str());
198  else
199  OMPL_DEBUG("The value of parameter '%s' was set to: '%s'", name_.c_str(), value.c_str());
200  return result;
201  }
202 
203  virtual std::string getValue() const
204  {
205  if (getter_)
206  try
207  {
208  return boost::lexical_cast<std::string>(getter_());
209  }
210  catch (boost::bad_lexical_cast &e)
211  {
212  OMPL_WARN("Unable to parameter '%s' to string: %s", name_.c_str(), e.what());
213  return "";
214  }
215  else
216  return "";
217  }
218 
219  protected:
220 
222  SetterFn setter_;
223 
225  GetterFn getter_;
226  };
227 
229 
230  OMPL_CLASS_FORWARD(ParamSet);
232 
234  class ParamSet
235  {
236  public:
237 
239  template<typename T>
240  void declareParam(const std::string &name, const typename SpecificParam<T>::SetterFn &setter,
241  const typename SpecificParam<T>::GetterFn &getter = typename SpecificParam<T>::GetterFn())
242  {
243  params_[name].reset(new SpecificParam<T>(name, setter, getter));
244  }
245 
247  void add(const GenericParamPtr &param);
248 
250  void remove(const std::string &name);
251 
253  void include(const ParamSet &other, const std::string &prefix = "");
254 
268  bool setParam(const std::string &key, const std::string &value);
269 
271  bool getParam(const std::string &key, std::string &value) const;
272 
278  bool setParams(const std::map<std::string, std::string> &kv, bool ignoreUnknown = false);
279 
281  void getParams(std::map<std::string, std::string> &params) const;
282 
284  void getParamNames(std::vector<std::string> &params) const;
285 
287  void getParamValues(std::vector<std::string> &vals) const;
288 
290  const std::map<std::string, GenericParamPtr>& getParams() const;
291 
293  const GenericParamPtr& getParam(const std::string &key) const;
294 
296  bool hasParam(const std::string &key) const;
297 
299  GenericParam& operator[](const std::string &key);
300 
302  std::size_t size() const
303  {
304  return params_.size();
305  }
306 
308  void clear();
309 
311  void print(std::ostream &out) const;
312 
313  private:
314 
315  std::map<std::string, GenericParamPtr> params_;
316  };
317  }
318 }
319 
320 #endif
boost::function< void(T)> SetterFn
The type for the &#39;setter&#39; function for this parameter.
Definition: GenericParam.h:164
std::string name_
The name of the parameter.
Definition: GenericParam.h:134
GenericParam(const std::string &name)
The constructor of a parameter takes the name of the parameter (name)
Definition: GenericParam.h:70
Motion planning algorithms often employ parameters to guide their exploration process. (e.g., goal biasing). Motion planners (and some of their components) use this class to declare what the parameters are, in a generic way, so that they can be set externally.
Definition: GenericParam.h:65
virtual bool setValue(const std::string &value)
Set the value of the parameter. The value is taken in as a string, but converted to the type of that ...
Definition: GenericParam.h:182
std::size_t size() const
Get the number of parameters maintained by this instance.
Definition: GenericParam.h:302
virtual std::string getValue() const
Retrieve the value of the parameter, as a string.
Definition: GenericParam.h:203
This is a helper class that instantiates parameters with different data types.
Definition: GenericParam.h:159
Maintain a set of parameters.
Definition: GenericParam.h:234
Main namespace. Contains everything in this library.
Definition: Cost.h:42
const std::string & maybeWrapBool(const std::string &value) const
Bool values such as "false" cannot be converted to bool using lexical_cast. We need to map those to "...
Definition: GenericParam.h:128
const std::string & getRangeSuggestion() const
Get the suggested range of values.
Definition: GenericParam.h:118
#define OMPL_ERROR(fmt,...)
Log a formatted error string.
Definition: Console.h:64
SetterFn setter_
The setter function for this parameter.
Definition: GenericParam.h:222
void setName(const std::string &name)
Set the name of the parameter.
Definition: GenericParam.h:85
virtual std::string getValue() const =0
Retrieve the value of the parameter, as a string.
GenericParam & operator=(const T &value)
Assignment operator by type. This is just for convenience, as it just calls setValue() ...
Definition: GenericParam.h:98
#define OMPL_DEBUG(fmt,...)
Log a formatted debugging string.
Definition: Console.h:70
void declareParam(const std::string &name, const typename SpecificParam< T >::SetterFn &setter, const typename SpecificParam< T >::GetterFn &getter=typename SpecificParam< T >::GetterFn())
This function declares a parameter name, and specifies the setter and getter functions.
Definition: GenericParam.h:240
boost::function< T()> GetterFn
The type for the &#39;getter&#39; function for this parameter.
Definition: GenericParam.h:167
const std::string & getName() const
Get the name of the parameter.
Definition: GenericParam.h:79
void setRangeSuggestion(const std::string &rangeSuggestion)
Set a suggested range.
Definition: GenericParam.h:112
std::string rangeSuggestion_
Suggested range for the parameter.
Definition: GenericParam.h:149
SpecificParam(const std::string &name, const SetterFn &setter, const GetterFn &getter=GetterFn())
An explicit instantiation of a parameter name requires the setter function and optionally the getter ...
Definition: GenericParam.h:171
virtual bool setValue(const std::string &value)=0
Set the value of the parameter. The value is taken in as a string, but converted to the type of that ...
GetterFn getter_
The getter function for this parameter.
Definition: GenericParam.h:225
#define OMPL_WARN(fmt,...)
Log a formatted warning string.
Definition: Console.h:66