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/String.h"
42 #include "ompl/util/ClassForward.h"
43 #include <functional>
44 #include <iostream>
45 #include <string>
46 #include <sstream>
47 #include <utility>
48 #include <vector>
49 #include <map>
50 
51 namespace ompl
52 {
53  namespace base
54  {
56 
57  OMPL_CLASS_FORWARD(GenericParam);
59 
65  class GenericParam
66  {
67  public:
69  GenericParam(std::string name) : name_(std::move(name))
70  {
71  }
72 
73  virtual ~GenericParam() = default;
74 
76  const std::string &getName() const
77  {
78  return name_;
79  }
80 
82  void setName(const std::string &name)
83  {
84  name_ = name;
85  }
86 
89  virtual bool setValue(const std::string &value) = 0;
90 
92  virtual std::string getValue() const = 0;
93 
95  template <typename T>
96  GenericParam &operator=(const T &value)
97  {
98  setValue(std::to_string(value));
99  return *this;
100  }
101 
103  void setRangeSuggestion(const std::string &rangeSuggestion)
104  {
105  rangeSuggestion_ = rangeSuggestion;
106  }
107 
109  const std::string &getRangeSuggestion() const
110  {
111  return rangeSuggestion_;
112  }
113 
114  protected:
116  std::string name_;
117 
131  std::string rangeSuggestion_;
132  };
133 
135  template <typename T>
136  class SpecificParam : public GenericParam
137  {
138  public:
140  using SetterFn = std::function<void(T)>;
141 
143  using GetterFn = std::function<T()>;
144 
148  SpecificParam(const std::string &name, SetterFn setter, GetterFn getter = GetterFn())
149  : GenericParam(name), setter_(std::move(setter)), getter_(std::move(getter))
150  {
151  if (!setter_ && !getter_)
152  OMPL_ERROR("At least one setter or getter function must be specified for parameter");
153  }
154 
155  ~SpecificParam() override = default;
156 
157  bool setValue(const std::string &value) override
158  {
159  bool result = true;
160  try
161  {
162  if (setter_)
163  setter_(lexical_cast(value));
164  }
165  catch (std::invalid_argument &e)
166  {
167  result = false;
168  OMPL_WARN("Invalid value format specified for parameter '%s': %s", name_.c_str(), e.what());
169  }
170 
171  if (getter_)
172  OMPL_DEBUG("The value of parameter '%s' is now: '%s'", name_.c_str(), getValue().c_str());
173  else
174  OMPL_DEBUG("The value of parameter '%s' was set to: '%s'", name_.c_str(), value.c_str());
175  return result;
176  }
177 
178  std::string getValue() const override
179  {
180  return getter_ ? std::to_string(getter_()) : "";
181  }
182 
183  protected:
189  T lexical_cast(const std::string &value) const;
190 
193 
196  };
197 
198  template <>
200  {
201  return getter_ ? ompl::toString(getter_()) : "";
202  }
203  template <>
205  {
206  return getter_ ? ompl::toString(getter_()) : "";
207  }
208  template <>
209  inline std::string ompl::base::SpecificParam<long double>::getValue() const
210  {
211  return getter_ ? ompl::toString(getter_()) : "";
212  }
213  template <>
214  inline std::string ompl::base::SpecificParam<std::string>::getValue() const
215  {
216  return getter_ ? getter_() : "";
217  }
218 
220 
221  OMPL_CLASS_FORWARD(ParamSet);
223 
225  class ParamSet
226  {
227  public:
230  template <typename T>
231  void declareParam(const std::string &name, const typename SpecificParam<T>::SetterFn &setter,
232  const typename SpecificParam<T>::GetterFn &getter = [] { return T(); })
233  {
234  params_[name] = std::make_shared<SpecificParam<T>>(name, setter, getter);
235  }
236 
238  void add(const GenericParamPtr &param);
239 
241  void remove(const std::string &name);
242 
245  void include(const ParamSet &other, const std::string &prefix = "");
246 
260  bool setParam(const std::string &key, const std::string &value);
261 
264  bool getParam(const std::string &key, std::string &value) const;
265 
272  bool setParams(const std::map<std::string, std::string> &kv, bool ignoreUnknown = false);
273 
275  void getParams(std::map<std::string, std::string> &params) const;
276 
278  void getParamNames(std::vector<std::string> &params) const;
279 
281  void getParamValues(std::vector<std::string> &vals) const;
282 
284  const std::map<std::string, GenericParamPtr> &getParams() const;
285 
288  const GenericParamPtr &getParam(const std::string &key) const;
289 
291  bool hasParam(const std::string &key) const;
292 
295  GenericParam &operator[](const std::string &key);
296 
298  std::size_t size() const
299  {
300  return params_.size();
301  }
302 
304  void clear();
305 
307  void print(std::ostream &out) const;
308 
309  private:
310  std::map<std::string, GenericParamPtr> params_;
311  };
312  } // namespace base
313 } // namespace ompl
314 
315 #endif
GenericParam & operator[](const std::string &key)
Access operator for parameters, by name. If the parameter is not defined, an exception is thrown.
bool setParam(const std::string &key, const std::string &value)
Algorithms in OMPL often have parameters that can be set externally. While each algorithm will have t...
void setRangeSuggestion(const std::string &rangeSuggestion)
Set a suggested range.
Definition: GenericParam.h:199
bool hasParam(const std::string &key) const
Check whether this set of parameters includes the parameter named key.
GenericParam & operator=(const T &value)
Assignment operator by type. This is just for convenience, as it just calls setValue()
Definition: GenericParam.h:192
Maintain a set of parameters.
Definition: GenericParam.h:289
void getParamValues(std::vector< std::string > &vals) const
List the values of the known parameters, in the same order as getParamNames()
const std::string & getRangeSuggestion() const
Get the suggested range of values.
Definition: GenericParam.h:205
const std::map< std::string, GenericParamPtr > & getParams() const
Get the map from parameter names to parameter descriptions.
bool setParams(const std::map< std::string, std::string > &kv, bool ignoreUnknown=false)
Set the values for a set of parameters. The parameter names are the keys in the map kv....
void setName(const std::string &name)
Set the name of the parameter.
Definition: GenericParam.h:178
std::string name_
The name of the parameter.
Definition: GenericParam.h:212
std::function< void(T)> SetterFn
The type for the 'setter' function for this parameter.
Definition: GenericParam.h:204
T lexical_cast(const std::string &value) const
Helper function to convert strings into objects of type T.
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....
Definition: GenericParam.h:129
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:259
void clear()
Clear all the set parameters.
std::string rangeSuggestion_
Suggested range for the parameter.
Definition: GenericParam.h:227
void print(std::ostream &out) const
Print the parameters to a stream.
std::string getValue() const override
Retrieve the value of the parameter, as a string.
Definition: GenericParam.h:242
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:212
bool getParam(const std::string &key, std::string &value) const
Get the value of the parameter named key. Store the value as string in value and return true if the p...
#define OMPL_WARN(fmt,...)
Log a formatted warning string.
Definition: Console.h:66
SetterFn setter_
The setter function for this parameter.
Definition: GenericParam.h:256
const std::string & getName() const
Get the name of the parameter.
Definition: GenericParam.h:172
void declareParam(const std::string &name, const typename SpecificParam< T >::SetterFn &setter, const typename SpecificParam< T >::GetterFn &getter=[] { return T();})
This function declares a parameter name, and specifies the setter and getter functions.
Definition: GenericParam.h:295
void getParamNames(std::vector< std::string > &params) const
List the names of the known parameters.
#define OMPL_ERROR(fmt,...)
Log a formatted error string.
Definition: Console.h:64
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:221
std::size_t size() const
Get the number of parameters maintained by this instance.
Definition: GenericParam.h:362
GenericParam(std::string name)
The constructor of a parameter takes the name of the parameter (name)
Definition: GenericParam.h:165
std::string toString(float val)
convert float to string using classic "C" locale semantics
Definition: String.cpp:82
std::function< T()> GetterFn
The type for the 'getter' function for this parameter.
Definition: GenericParam.h:207
#define OMPL_DEBUG(fmt,...)
Log a formatted debugging string.
Definition: Console.h:70
void add(const GenericParamPtr &param)
Add a parameter to the set.
void remove(const std::string &name)
Remove a parameter from the set.
Main namespace. Contains everything in this library.
void include(const ParamSet &other, const std::string &prefix="")
Include the params of a different ParamSet into this one. Optionally include a prefix for each of the...