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 <functional>
43 #include <boost/lexical_cast.hpp>
44 #include <iostream>
45 #include <string>
46 #include <utility>
47 #include <vector>
48 #include <map>
49 
50 namespace ompl
51 {
52  namespace base
53  {
55 
56  OMPL_CLASS_FORWARD(GenericParam);
58 
65  {
66  public:
68  GenericParam(std::string name) : name_(std::move(name))
69  {
70  }
71 
72  virtual ~GenericParam() = default;
73 
75  const std::string &getName() const
76  {
77  return name_;
78  }
79 
81  void setName(const std::string &name)
82  {
83  name_ = name;
84  }
85 
88  virtual bool setValue(const std::string &value) = 0;
89 
91  virtual std::string getValue() const = 0;
92 
94  template <typename T>
95  GenericParam &operator=(const T &value)
96  {
97  try
98  {
99  setValue(boost::lexical_cast<std::string>(value));
100  }
101  catch (boost::bad_lexical_cast &e)
102  {
103  OMPL_WARN("Invalid value format specified for parameter '%s': %s", name_.c_str(), e.what());
104  }
105  return *this;
106  }
107 
109  void setRangeSuggestion(const std::string &rangeSuggestion)
110  {
111  rangeSuggestion_ = rangeSuggestion;
112  }
113 
115  const std::string &getRangeSuggestion() const
116  {
117  return rangeSuggestion_;
118  }
119 
120  protected:
123  template <typename T>
124  const std::string &maybeWrapBool(const std::string &value) const
125  {
126  return boost::is_same<T, bool>::value ? truthValueTo01Str(value) : value;
127  }
128 
130  std::string name_;
131 
145  std::string rangeSuggestion_;
146 
147  private:
149  static const std::string &truthValueTo01Str(const std::string &value);
150  };
151 
153  template <typename T>
155  {
156  public:
158  typedef std::function<void(T)> SetterFn;
159 
161  typedef std::function<T()> GetterFn;
162 
166  SpecificParam(const std::string &name, SetterFn setter, GetterFn getter = GetterFn())
167  : GenericParam(name), setter_(std::move(setter)), getter_(std::move(getter))
168  {
169  if (!setter_ && !getter_)
170  OMPL_ERROR("At least one setter or getter function must be specified for parameter");
171  }
172 
173  ~SpecificParam() override = default;
174 
175  bool setValue(const std::string &value) override
176  {
177  bool result = true;
178  try
179  {
180  if (setter_)
181  setter_(boost::lexical_cast<T>(GenericParam::maybeWrapBool<T>(value)));
182  }
183  catch (boost::bad_lexical_cast &e)
184  {
185  result = false;
186  OMPL_WARN("Invalid value format specified for parameter '%s': %s", name_.c_str(), e.what());
187  }
188 
189  if (getter_)
190  OMPL_DEBUG("The value of parameter '%s' is now: '%s'", name_.c_str(), getValue().c_str());
191  else
192  OMPL_DEBUG("The value of parameter '%s' was set to: '%s'", name_.c_str(), value.c_str());
193  return result;
194  }
195 
196  std::string getValue() const override
197  {
198  if (getter_)
199  try
200  {
201  return boost::lexical_cast<std::string>(getter_());
202  }
203  catch (boost::bad_lexical_cast &e)
204  {
205  OMPL_WARN("Unable to parameter '%s' to string: %s", name_.c_str(), e.what());
206  return "";
207  }
208  else
209  return "";
210  }
211 
212  protected:
214  SetterFn setter_;
215 
217  GetterFn getter_;
218  };
219 
221 
222  OMPL_CLASS_FORWARD(ParamSet);
224 
226  class ParamSet
227  {
228  public:
231  template <typename T>
232  void declareParam(const std::string &name, const typename SpecificParam<T>::SetterFn &setter,
233  const typename SpecificParam<T>::GetterFn &getter = typename SpecificParam<T>::GetterFn())
234  {
235  params_[name] = std::make_shared<SpecificParam<T>>(name, setter, getter);
236  }
237 
239  void add(const GenericParamPtr &param);
240 
242  void remove(const std::string &name);
243 
246  void include(const ParamSet &other, const std::string &prefix = "");
247 
261  bool setParam(const std::string &key, const std::string &value);
262 
265  bool getParam(const std::string &key, std::string &value) const;
266 
273  bool setParams(const std::map<std::string, std::string> &kv, bool ignoreUnknown = false);
274 
276  void getParams(std::map<std::string, std::string> &params) const;
277 
279  void getParamNames(std::vector<std::string> &params) const;
280 
282  void getParamValues(std::vector<std::string> &vals) const;
283 
285  const std::map<std::string, GenericParamPtr> &getParams() const;
286 
289  const GenericParamPtr &getParam(const std::string &key) const;
290 
292  bool hasParam(const std::string &key) const;
293 
296  GenericParam &operator[](const std::string &key);
297 
299  std::size_t size() const
300  {
301  return params_.size();
302  }
303 
305  void clear();
306 
308  void print(std::ostream &out) const;
309 
310  private:
311  std::map<std::string, GenericParamPtr> params_;
312  };
313  }
314 }
315 
316 #endif
std::string name_
The name of the parameter.
Definition: GenericParam.h:130
const std::string & getName() const
Get the name of the parameter.
Definition: GenericParam.h:75
virtual std::string getValue() const =0
Retrieve the value of the parameter, as a string.
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:64
std::function< T()> GetterFn
The type for the &#39;getter&#39; function for this parameter.
Definition: GenericParam.h:161
STL namespace.
This is a helper class that instantiates parameters with different data types.
Definition: GenericParam.h:154
GenericParam(std::string name)
The constructor of a parameter takes the name of the parameter (name)
Definition: GenericParam.h:68
Maintain a set of parameters.
Definition: GenericParam.h:226
std::size_t size() const
Get the number of parameters maintained by this instance.
Definition: GenericParam.h:299
std::function< void(T)> SetterFn
The type for the &#39;setter&#39; function for this parameter.
Definition: GenericParam.h:158
Main namespace. Contains everything in this library.
Definition: AppBase.h:21
#define OMPL_ERROR(fmt,...)
Log a formatted error string.
Definition: Console.h:64
SetterFn setter_
The setter function for this parameter.
Definition: GenericParam.h:214
bool setValue(const std::string &value) override
Set the value of the parameter. The value is taken in as a string, but converted to the type of that ...
Definition: GenericParam.h:175
void setName(const std::string &name)
Set the name of the parameter.
Definition: GenericParam.h:81
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:124
#define OMPL_WARN(fmt,...)
Log a formatted warning string.
Definition: Console.h:66
GenericParam & operator=(const T &value)
Assignment operator by type. This is just for convenience, as it just calls setValue() ...
Definition: GenericParam.h:95
const std::string & getRangeSuggestion() const
Get the suggested range of values.
Definition: GenericParam.h:115
#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:232
SpecificParam(const std::string &name, SetterFn setter, GetterFn getter=GetterFn())
An explicit instantiation of a parameter name requires the setter function and optionally the getter ...
Definition: GenericParam.h:166
std::string getValue() const override
Retrieve the value of the parameter, as a string.
Definition: GenericParam.h:196
void setRangeSuggestion(const std::string &rangeSuggestion)
Set a suggested range.
Definition: GenericParam.h:109
std::string rangeSuggestion_
Suggested range for the parameter.
Definition: GenericParam.h:145
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:217