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  ClassForward(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  logWarn("Invalid value format specified for parameter '%s': %s", name_.c_str(), e.what());
106  }
107  return *this;
108  }
109 
110  protected:
111 
113  std::string name_;
114  };
115 
116 
118  template<typename T>
120  {
121  public:
122 
124  typedef boost::function<void(T)> SetterFn;
125 
127  typedef boost::function<T()> GetterFn;
128 
131  SpecificParam(const std::string &name, const SetterFn &setter, const GetterFn &getter = GetterFn()) :
132  GenericParam(name), setter_(setter), getter_(getter)
133  {
134  if (!setter_)
135  logError("Setter function must be specified for parameter");
136  }
137 
138  virtual ~SpecificParam(void)
139  {
140  }
141 
142  virtual bool setValue(const std::string &value)
143  {
144  bool result = true;
145  try
146  {
147  if (setter_)
148  setter_(boost::lexical_cast<T>(value));
149  }
150  catch (boost::bad_lexical_cast &e)
151  {
152  result = false;
153  logWarn("Invalid value format specified for parameter '%s': %s", name_.c_str(), e.what());
154  }
155 
156  if (getter_)
157  logDebug("The value of parameter '%s' is now: '%s'", name_.c_str(), getValue().c_str());
158  else
159  logDebug("The value of parameter '%s' was set to: '%s'", name_.c_str(), value.c_str());
160  return result;
161  }
162 
163  virtual std::string getValue(void) const
164  {
165  if (getter_)
166  try
167  {
168  return boost::lexical_cast<std::string>(getter_());
169  }
170  catch (boost::bad_lexical_cast &e)
171  {
172  logWarn("Unable to parameter '%s' to string: %s", name_.c_str(), e.what());
173  return "";
174  }
175  else
176  return "";
177  }
178 
179  protected:
180 
183 
186  };
187 
189  class ParamSet
190  {
191  public:
192 
194  template<typename T>
195  void declareParam(const std::string &name, const typename SpecificParam<T>::SetterFn &setter,
196  const typename SpecificParam<T>::GetterFn &getter = typename SpecificParam<T>::GetterFn())
197  {
198  params_[name].reset(new SpecificParam<T>(name, setter, getter));
199  }
200 
202  void add(const GenericParamPtr &param);
203 
205  void remove(const std::string &name);
206 
208  void include(const ParamSet &other, const std::string &prefix = "");
209 
223  bool setParam(const std::string &key, const std::string &value);
224 
226  bool getParam(const std::string &key, std::string &value) const;
227 
233  bool setParams(const std::map<std::string, std::string> &kv, bool ignoreUnknown = false);
234 
236  void getParams(std::map<std::string, std::string> &params) const;
237 
239  void getParamNames(std::vector<std::string> &params) const;
240 
242  void getParamValues(std::vector<std::string> &vals) const;
243 
245  const std::map<std::string, GenericParamPtr>& getParams(void) const;
246 
248  const GenericParamPtr& getParam(const std::string &key) const;
249 
251  bool hasParam(const std::string &key) const;
252 
254  GenericParam& operator[](const std::string &key);
255 
257  std::size_t size(void) const
258  {
259  return params_.size();
260  }
261 
263  void clear(void);
264 
266  void print(std::ostream &out) const;
267 
268  private:
269 
270  std::map<std::string, GenericParamPtr> params_;
271  };
272  }
273 }
274 
275 #endif