Parametrizable.h
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 #ifndef __POINTMATCHER_PARAMETRIZABLE_H
37 #define __POINTMATCHER_PARAMETRIZABLE_H
38 
39 #include <stdexcept>
40 #include <vector>
41 #include <map>
42 #include <set>
43 #include <string>
44 #include <boost/lexical_cast.hpp>
45 #include <limits>
46 #define BOOST_ASSIGN_MAX_PARAMS 6
47 #include <boost/assign/list_inserter.hpp>
48 
49 
50 namespace PointMatcherSupport
51 {
53  template<typename Target>
54  inline Target lexical_cast_scalar_to_string(const std::string& arg)
55  {
56  if (arg == "inf")
57  return std::numeric_limits<Target>::infinity();
58  else if (arg == "-inf")
59  return -std::numeric_limits<Target>::infinity();
60  else if (arg == "nan")
61  return std::numeric_limits<Target>::quiet_NaN();
62  else
63  return boost::lexical_cast<Target>(arg);
64  }
65 
67  template<typename Target>
68  inline Target lexical_cast_scalar_to_string(const char*& arg)
69  {
70  return lexical_cast_scalar_to_string<Target>(std::string(arg));
71  }
72 
73 
75  template<typename Target, typename Source>
76  inline Target lexical_cast(const Source& arg)
77  {
78  return boost::lexical_cast<Target>(arg);
79  }
80 
82  template<>
83  inline float lexical_cast(const std::string& arg) { return lexical_cast_scalar_to_string<float>(arg); }
85  template<>
86  inline double lexical_cast(const std::string& arg) { return lexical_cast_scalar_to_string<double>(arg); }
87 
88  //
89 
91  template<typename S>
92  std::string toParam(const S& value)
93  {
94  return lexical_cast<std::string>(value);
95  }
96 
99  {
101  struct InvalidParameter: std::runtime_error
102  {
103  InvalidParameter(const std::string& reason);
104  };
105 
108 
110  template<typename S>
111  static bool Comp(std::string a, std::string b)
112  {
113  return lexical_cast<S>(a) < lexical_cast<S>(b);
114  }
115 
118  {
125 
126  /*
127  This code is beautiful, this code is correct, this code does not work ;-(
128  Blame gcc bug 9050 (http://gcc.gnu.org/bugzilla/show_bug.cgi?id=9050), shame
129  on them forever and beyond. People being laaaazzzy adopters, I'm forced to use
130  something that works on gcc 4.4.
131 
132  template<typename S>
133  ParameterDoc(const std::string& name, const std::string& doc, const S defaultValue, const S minValue, const S maxValue = std::numeric_limits<S>::max());
134  template<typename S>
135  ParameterDoc(const std::string& name, const std::string& doc, const S defaultValue);
136  */
137  ParameterDoc(const std::string& name, const std::string& doc, const std::string& defaultValue, const std::string& minValue, const std::string& maxValue, LexicalComparison comp);
138  ParameterDoc(const std::string& name, const std::string& doc, const std::string& defaultValue);
139 
140  friend std::ostream& operator<< (std::ostream& o, const ParameterDoc& p);
141  };
142 
144  typedef std::vector<ParameterDoc> ParametersDoc;
145 
146  /*
147  Again, not used because fo gcc bug 9050
148  struct Parameter: public std::string
149  {
150  template<typename S>
151  Parameter(const S value);
152  Parameter(){}
153  };
154  */
156  typedef std::map<std::string, Parameter> Parameters;
157  typedef std::set<std::string> ParametersUsed;
158 
160  const ParametersDoc parametersDoc;
161  Parameters parameters;
162  ParametersUsed parametersUsed;
163 
164  Parametrizable();
165  Parametrizable(const std::string& className, const ParametersDoc paramsDoc, const Parameters& params);
166  virtual ~Parametrizable();
167 
168  std::string getParamValueString(const std::string& paramName);
169 
171  template<typename S>
172  S get(const std::string& paramName) { return lexical_cast<S>(getParamValueString(paramName)); }
173 
174  friend std::ostream& operator<< (std::ostream& o, const Parametrizable& p);
175  };
176  std::ostream& operator<< (std::ostream& o, const Parametrizable::ParametersDoc& p);
177 } // namespace PointMatcherSupport
178 
179 #endif // __POINTMATCHER_PARAMETRIZABLE_H
InvalidParameter(const std::string &reason)
Construct an invalid-parameter exception.
virtual ~Parametrizable()
Virtual destructor, do nothing.
std::string toParam(const S &value)
Return the a string value using lexical_cast.
::std::string string
Definition: gtest.h:1979
ParametersUsed parametersUsed
parameters whose value has actually been read
Parametrizable()
Construct a documentation of parameters from a description in the source.
std::set< std::string > ParametersUsed
Parameters whose value has been read.
Target lexical_cast_scalar_to_string(const std::string &arg)
A lexical casting function that is an improvements over boost::lexical_cast that can handle "inf"...
bool(* LexicalComparison)(std::string a, std::string b)
A function that returns whether a is smaller than b.
std::string minValue
if bounds are checked, minimum value
std::map< std::string, Parameter > Parameters
Parameters stored as a map of string->string.
LexicalComparison comp
pointer to comparison function
Functions and classes that are not dependant on scalar type are defined in this namespace.
std::string maxValue
if bounds are checked, maxmimu value
Parameters parameters
parameters with their values encoded in string
The documentation of a parameter.
friend std::ostream & operator<<(std::ostream &o, const ParameterDoc &p)
Dump the documentation of this parameter to a stream.
Target lexical_cast(const Source &arg)
General case of lexical cast, use boost.
The superclass of classes that are constructed using generic parameters. This class provides the para...
An exception thrown when one tries to fetch the value of an unexisting parameter. ...
std::string getParamValueString(const std::string &paramName)
Get the value of a parameter, as a string.
std::vector< ParameterDoc > ParametersDoc
The documentation of all parameters.
static bool Comp(std::string a, std::string b)
Return whether a < b, lexically casted to S.
const std::string className
name of the class
double lexical_cast(const std::string &arg)
Special case of lexical cast to float, use lexical_cast_scalar_to_string.
const ParametersDoc parametersDoc
documentation of parameters
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.


libpointmatcher
Author(s):
autogenerated on Sat May 27 2023 02:38:03