Parametrizable.cpp
Go to the documentation of this file.
1 // kate: replace-tabs off; indent-width 4; indent-mode normal
2 // vim: ts=4:sw=4:noexpandtab
3 /*
4 
5 Copyright (c) 2010--2012,
6 François Pomerleau and Stephane Magnenat, ASL, ETHZ, Switzerland
7 You can contact the authors at <f dot pomerleau at gmail dot com> and
8 <stephane at magnenat dot net>
9 
10 All rights reserved.
11 
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
14  * Redistributions of source code must retain the above copyright
15  notice, this list of conditions and the following disclaimer.
16  * Redistributions in binary form must reproduce the above copyright
17  notice, this list of conditions and the following disclaimer in the
18  documentation and/or other materials provided with the distribution.
19  * Neither the name of the <organization> nor the
20  names of its contributors may be used to endorse or promote products
21  derived from this software without specific prior written permission.
22 
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 DISCLAIMED. IN NO EVENT SHALL ETH-ASL BE LIABLE FOR ANY
27 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 
34 */
35 
36 #include "Parametrizable.h"
37 #include <boost/format.hpp>
38 #include <boost/typeof/typeof.hpp>
39 #include <string>
40 
41 namespace PointMatcherSupport
42 {
43  using namespace std;
44 
47  runtime_error(reason)
48  {}
49 
51  std::ostream& operator<< (std::ostream& o, const Parametrizable::ParameterDoc& p)
52  {
53  o << p.name << " (default: " << p.defaultValue << ") - " << p.doc;
54  if (!p.minValue.empty())
55  o << " - min: " << p.minValue;
56  if (!p.maxValue.empty())
57  o << " - max: " << p.maxValue;
58  return o;
59  }
60 
62  std::ostream& operator<< (std::ostream& o, const Parametrizable& p)
63  {
64  o << p.parametersDoc;
65  return o;
66  }
67 
69  std::ostream& operator<< (std::ostream& o, const Parametrizable::ParametersDoc& p)
70  {
71  for (BOOST_AUTO(it,p.begin()); it != p.end(); ++it)
72  o << "- " << *it << endl;
73  return o;
74  }
75 
78  {
79  return false;
80  }
81 
82  /*template<typename S>
83  bool ScalarLexicalComparison(std::string a, std::string b)
84  {
85  return boost::lexical_cast<S>(a) < boost::lexical_cast<S>(b);
86  }
87 
88  Uncomment once most people have gcc >= 4.5
89  Shame on bug 9050 (http://gcc.gnu.org/bugzilla/show_bug.cgi?id=9050)
90  template<typename S>
91  Parametrizable::ParameterDoc::ParameterDoc(const std::string& name, const std::string& doc, const S defaultValue, const S minValue, const S maxValue):
92  name(name),
93  doc(doc),
94  defaultValue(boost::lexical_cast<string>(defaultValue)),
95  minValue(boost::lexical_cast<string>(minValue)),
96  maxValue(boost::lexical_cast<string>(maxValue)),
97  comp(ScalarLexicalComparison<S>)
98  {}
99 
100  template<typename S>
101  Parametrizable::ParameterDoc::ParameterDoc(const std::string& name, const std::string& doc, const S defaultValue):
102  name(name),
103  doc(doc),
104  defaultValue(boost::lexical_cast<string>(defaultValue)),
105  minValue(""),
106  maxValue(""),
107  comp(TrueLexicalComparison)
108  {}
109  */
110 
112  Parametrizable::ParameterDoc::ParameterDoc(const std::string& name, const std::string& doc, const std::string& defaultValue, const std::string& minValue, const std::string& maxValue, LexicalComparison comp):
113  name(name),
114  doc(doc),
115  defaultValue(defaultValue),
116  minValue(minValue),
117  maxValue(maxValue),
118  comp(comp)
119  {}
120 
122  Parametrizable::ParameterDoc::ParameterDoc(const std::string& name, const std::string& doc, const std::string& defaultValue):
123  name(name),
124  doc(doc),
125  defaultValue(defaultValue),
126  minValue(""),
127  maxValue(""),
129  {}
130 
132  /*ParametersDoc(const std::vector<ParameterDoc>& list):
133  std::vector<ParameterDoc>(list)
134  {}*/
135 
136  /*
137  Again, not used because fo gcc bug 9050
138 
139  template<typename S>
140  Parametrizable::Parameter::Parameter(const S value):
141  std::string(boost::lexical_cast<string>(value))
142  {}
143 
144  // force instantiation of constructors
145  template Parametrizable::ParameterDoc::ParameterDoc<int>(const std::string, const std::string, const int);
146  template Parametrizable::ParameterDoc::ParameterDoc<float>(const std::string, const std::string, const float);
147  template Parametrizable::ParameterDoc::ParameterDoc<double>(const std::string, const std::string, const double);
148  template Parametrizable::ParameterDoc::ParameterDoc<bool>(const std::string, const std::string, const bool);
149  template Parametrizable::ParameterDoc::ParameterDoc<std::string>(const std::string, const std::string, const std::string);
150  template Parametrizable::ParameterDoc::ParameterDoc<const char*>(const std::string, const std::string, const char*);
151 
152  template Parametrizable::ParameterDoc::ParameterDoc<int>(const std::string, const std::string, const int, const int, const int);
153  template Parametrizable::ParameterDoc::ParameterDoc<float>(const std::string, const std::string, const float, const float, const float);
154  template Parametrizable::ParameterDoc::ParameterDoc<double>(const std::string, const std::string, const double, const double, const double);
155 
156  template Parametrizable::Parameter::Parameter<int>(const int);
157  template Parametrizable::Parameter::Parameter<float>(const float);
158  template Parametrizable::Parameter::Parameter<double>(const double);
159  template Parametrizable::Parameter::Parameter<bool>(const bool);
160  template Parametrizable::Parameter::Parameter<std::string>(const std::string);
161 
162  */
163 
166  className("unknown")
167  {}
168 
171  const std::string& className, const ParametersDoc paramsDoc, const Parameters& params):
172  className(className),
173  parametersDoc(paramsDoc)
174  {
175  // fill current parameters from either values passed as argument, or default value
176  for (BOOST_AUTO(it, parametersDoc.begin()); it != parametersDoc.end(); ++it)
177  {
178  const string& paramName(it->name);
179  Parameters::const_iterator paramIt(params.find(paramName));
180  if (paramIt != params.end())
181  {
182  const string& val(paramIt->second);
183  if (it->comp(val, it->minValue))
184  throw InvalidParameter((boost::format("Value %1% of parameter %2% in class %3% is smaller than minimum admissible value %4%") % val % paramName % className % it->minValue).str());
185  if (it->comp(it->maxValue, val))
186  throw InvalidParameter((boost::format("Value %1% of parameter %2% in class %3% is larger than maximum admissible value %4%") % val % paramName % className % it->maxValue).str());
187  parameters[paramName] = val;
188  }
189  else
190  parameters[paramName] = it->defaultValue;
191  }
192  }
193 
196  {}
197 
200  {
201  Parameters::const_iterator paramIt(parameters.find(paramName));
202  if (paramIt == parameters.end())
203  throw InvalidParameter((boost::format("Parameter %1% does not exist in class %2%") % paramName % className).str());
204  // TODO: use string distance to propose close one, copy/paste code from Aseba
205  this->parametersUsed.insert(paramIt->first);
206  return paramIt->second;
207  }
208 } // namespace PointMatcherSupport
PointMatcherSupport::Parametrizable::operator<<
friend std::ostream & operator<<(std::ostream &o, const Parametrizable &p)
Dump the documentation of this object to a stream.
Definition: Parametrizable.cpp:62
PointMatcherSupport::Parametrizable::Parametrizable
Parametrizable()
Construct a documentation of parameters from a description in the source.
Definition: Parametrizable.cpp:165
PointMatcherSupport::Parametrizable::parametersDoc
const ParametersDoc parametersDoc
documentation of parameters
Definition: Parametrizable.h:160
PointMatcherSupport::Parametrizable::ParameterDoc::doc
std::string doc
short documentation
Definition: Parametrizable.h:120
PointMatcherSupport::Parametrizable::parametersUsed
ParametersUsed parametersUsed
parameters whose value has actually been read
Definition: Parametrizable.h:162
Parametrizable.h
testing::internal::string
::std::string string
Definition: gtest.h:1979
PointMatcherSupport::Parametrizable::LexicalComparison
bool(* LexicalComparison)(std::string a, std::string b)
A function that returns whether a is smaller than b.
Definition: Parametrizable.h:107
PointMatcherSupport::Parametrizable::ParametersDoc
std::vector< ParameterDoc > ParametersDoc
The documentation of all parameters.
Definition: Parametrizable.h:144
PointMatcherSupport::Parametrizable::ParameterDoc::defaultValue
std::string defaultValue
default value
Definition: Parametrizable.h:121
PointMatcherSupport::Parametrizable::parameters
Parameters parameters
parameters with their values encoded in string
Definition: Parametrizable.h:161
PointMatcherSupport::Parametrizable::ParameterDoc::ParameterDoc
ParameterDoc(const std::string &name, const std::string &doc, const std::string &defaultValue, const std::string &minValue, const std::string &maxValue, LexicalComparison comp)
Construct a parameter documentation with bounds.
Definition: Parametrizable.cpp:112
PointMatcherSupport::Parametrizable::ParameterDoc::maxValue
std::string maxValue
if bounds are checked, maxmimu value
Definition: Parametrizable.h:123
PointMatcherSupport::Parametrizable::ParameterDoc::name
std::string name
name
Definition: Parametrizable.h:119
PointMatcherSupport::Parametrizable::InvalidParameter
An exception thrown when one tries to fetch the value of an unexisting parameter.
Definition: Parametrizable.h:101
PointMatcherSupport::Parametrizable::getParamValueString
std::string getParamValueString(const std::string &paramName)
Get the value of a parameter, as a string.
Definition: Parametrizable.cpp:199
PointMatcherSupport::Parametrizable::~Parametrizable
virtual ~Parametrizable()
Virtual destructor, do nothing.
Definition: Parametrizable.cpp:195
PointMatcherSupport::Parametrizable::InvalidParameter::InvalidParameter
InvalidParameter(const std::string &reason)
Construct an invalid-parameter exception.
Definition: Parametrizable.cpp:46
PointMatcherSupport::Parametrizable
The superclass of classes that are constructed using generic parameters. This class provides the para...
Definition: Parametrizable.h:98
std
PointMatcherSupport::Parametrizable::ParameterDoc
The documentation of a parameter.
Definition: Parametrizable.h:117
PointMatcherSupport
Functions and classes that are not dependant on scalar type are defined in this namespace.
Definition: Bibliography.cpp:45
PointMatcherSupport::Parametrizable::ParameterDoc::minValue
std::string minValue
if bounds are checked, minimum value
Definition: Parametrizable.h:122
PointMatcherSupport::Parametrizable::className
const std::string className
name of the class
Definition: Parametrizable.h:159
PointMatcherSupport::Parametrizable::Parameters
std::map< std::string, Parameter > Parameters
Parameters stored as a map of string->string.
Definition: Parametrizable.h:156
PointMatcherSupport::FalseLexicalComparison
bool FalseLexicalComparison(std::string, std::string)
Return always false.
Definition: Parametrizable.cpp:77


mp2p_icp
Author(s):
autogenerated on Fri Dec 20 2024 03:45:59