All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
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 <iostream>
45 #include <string>
46 #include <vector>
47 #include <map>
48 
49 namespace ompl
50 {
51  namespace base
52  {
53 
55 
56  OMPL_CLASS_FORWARD(GenericParam);
58 
65  {
66  public:
67 
69  GenericParam(const std::string &name) : name_(name)
70  {
71  }
72 
73  virtual ~GenericParam(void)
74  {
75  }
76 
78  const std::string& getName(void) const
79  {
80  return name_;
81  }
82 
84  void setName(const std::string &name)
85  {
86  name_ = name;
87  }
88 
90  virtual bool setValue(const std::string &value) = 0;
91 
93  virtual std::string getValue(void) const = 0;
94 
96  template<typename T>
97  GenericParam& operator=(const T &value)
98  {
99  try
100  {
101  setValue(boost::lexical_cast<std::string>(value));
102  }
103  catch (boost::bad_lexical_cast &e)
104  {
105  OMPL_WARN("Invalid value format specified for parameter '%s': %s", name_.c_str(), e.what());
106  }
107  return *this;
108  }
109 
111  void setRangeSuggestion(const std::string &rangeSuggestion)
112  {
113  rangeSuggestion_ = rangeSuggestion;
114  }
115 
117  const std::string& getRangeSuggestion(void) const
118  {
119  return rangeSuggestion_;
120  }
121 
122  protected:
123 
125  std::string name_;
126 
140  std::string rangeSuggestion_;
141  };
142 
143 
145  template<typename T>
147  {
148  public:
149 
151  typedef boost::function<void(T)> SetterFn;
152 
154  typedef boost::function<T()> GetterFn;
155 
158  SpecificParam(const std::string &name, const SetterFn &setter, const GetterFn &getter = GetterFn()) :
159  GenericParam(name), setter_(setter), getter_(getter)
160  {
161  if (!setter_)
162  OMPL_ERROR("Setter function must be specified for parameter");
163  }
164 
165  virtual ~SpecificParam(void)
166  {
167  }
168 
169  virtual bool setValue(const std::string &value)
170  {
171  bool result = true;
172  try
173  {
174  if (setter_)
175  setter_(boost::lexical_cast<T>(value));
176  }
177  catch (boost::bad_lexical_cast &e)
178  {
179  result = false;
180  OMPL_WARN("Invalid value format specified for parameter '%s': %s", name_.c_str(), e.what());
181  }
182 
183  if (getter_)
184  OMPL_DEBUG("The value of parameter '%s' is now: '%s'", name_.c_str(), getValue().c_str());
185  else
186  OMPL_DEBUG("The value of parameter '%s' was set to: '%s'", name_.c_str(), value.c_str());
187  return result;
188  }
189 
190  virtual std::string getValue(void) const
191  {
192  if (getter_)
193  try
194  {
195  return boost::lexical_cast<std::string>(getter_());
196  }
197  catch (boost::bad_lexical_cast &e)
198  {
199  OMPL_WARN("Unable to parameter '%s' to string: %s", name_.c_str(), e.what());
200  return "";
201  }
202  else
203  return "";
204  }
205 
206  protected:
207 
210 
213  };
214 
216  class ParamSet
217  {
218  public:
219 
221  template<typename T>
222  void declareParam(const std::string &name, const typename SpecificParam<T>::SetterFn &setter,
223  const typename SpecificParam<T>::GetterFn &getter = typename SpecificParam<T>::GetterFn())
224  {
225  params_[name].reset(new SpecificParam<T>(name, setter, getter));
226  }
227 
229  void add(const GenericParamPtr &param);
230 
232  void remove(const std::string &name);
233 
235  void include(const ParamSet &other, const std::string &prefix = "");
236 
250  bool setParam(const std::string &key, const std::string &value);
251 
253  bool getParam(const std::string &key, std::string &value) const;
254 
260  bool setParams(const std::map<std::string, std::string> &kv, bool ignoreUnknown = false);
261 
263  void getParams(std::map<std::string, std::string> &params) const;
264 
266  void getParamNames(std::vector<std::string> &params) const;
267 
269  void getParamValues(std::vector<std::string> &vals) const;
270 
272  const std::map<std::string, GenericParamPtr>& getParams(void) const;
273 
275  const GenericParamPtr& getParam(const std::string &key) const;
276 
278  bool hasParam(const std::string &key) const;
279 
281  GenericParam& operator[](const std::string &key);
282 
284  std::size_t size(void) const
285  {
286  return params_.size();
287  }
288 
290  void clear(void);
291 
293  void print(std::ostream &out) const;
294 
295  private:
296 
297  std::map<std::string, GenericParamPtr> params_;
298  };
299  }
300 }
301 
302 #endif