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 #include <boost/algorithm/string.hpp>
49 
50 
51 namespace PointMatcherSupport
52 {
54  template<typename Target>
55  inline Target lexical_cast_scalar_to_string(const std::string& arg)
56  {
57  if (arg == "inf")
58  return std::numeric_limits<Target>::infinity();
59  else if (arg == "-inf")
60  return -std::numeric_limits<Target>::infinity();
61  else if (arg == "nan")
62  return std::numeric_limits<Target>::quiet_NaN();
63  else
64  return boost::lexical_cast<Target>(arg);
65  }
66 
68  template<typename Target>
69  inline Target lexical_cast_scalar_to_string(const char*& arg)
70  {
71  return lexical_cast_scalar_to_string<Target>(std::string(arg));
72  }
73 
74 
76  template<typename Target, typename Source>
77  inline Target lexical_cast(const Source& arg)
78  {
79  return boost::lexical_cast<Target>(arg);
80  }
81 
83  template<typename T>
84  inline std::vector<T> lexical_cast_vector(const std::string& arg)
85  {
86  std::string cleanedInput = arg;
87  if (cleanedInput.find('[') != 0)
88  {
89  throw std::runtime_error("Vector parameter '" + arg + "' must start with '['");
90  }
91  if (cleanedInput.find(']') != cleanedInput.size()-1)
92  {
93  throw std::runtime_error("Vector parameter '" + arg + "' must end with ']'");
94  }
95  cleanedInput.erase(std::remove(cleanedInput.begin(), cleanedInput.end(), '['), cleanedInput.end());
96  cleanedInput.erase(std::remove(cleanedInput.begin(), cleanedInput.end(), ' '), cleanedInput.end());
97  cleanedInput.erase(std::remove(cleanedInput.begin(), cleanedInput.end(), ']'), cleanedInput.end());
98 
99  // Split the string into a vector of strings using commas as the delimiter
100  std::vector<std::string> tokens;
101  boost::algorithm::split(tokens, cleanedInput, boost::algorithm::is_any_of(","));
102 
103  std::vector<T> result;
104  result.reserve(tokens.size());
105  for(const auto& token: tokens)
106  {
107  auto value = lexical_cast_scalar_to_string<T>(token);
108  result.push_back(value);
109  }
110  return result;
111  }
113  template<>
114  inline float lexical_cast(const std::string& arg) { return lexical_cast_scalar_to_string<float>(arg); }
116  template<>
117  inline double lexical_cast(const std::string& arg) { return lexical_cast_scalar_to_string<double>(arg); }
118 
119  //
120 
122  template<typename S>
123  inline std::string toParam(const S& value)
124  {
125  return lexical_cast<std::string>(value);
126  }
127 
129  template<typename T>
130  inline std::string toParam(const std::vector<T>& input)
131  {
132  std::ostringstream oss;
133  oss << "[";
134  std::copy(input.begin(), input.end() - 1,
135  std::ostream_iterator<T>(oss, ", "));
136  oss << input.back() << "]";
137  return oss.str();
138  }
139 
142  {
144  struct InvalidParameter: std::runtime_error
145  {
146  InvalidParameter(const std::string& reason);
147  };
148 
151 
153  template<typename S>
154  static bool Comp(std::string a, std::string b)
155  {
156  return lexical_cast<S>(a) < lexical_cast<S>(b);
157  }
158 
161  {
168 
169  /*
170  This code is beautiful, this code is correct, this code does not work ;-(
171  Blame gcc bug 9050 (http://gcc.gnu.org/bugzilla/show_bug.cgi?id=9050), shame
172  on them forever and beyond. People being laaaazzzy adopters, I'm forced to use
173  something that works on gcc 4.4.
174 
175  template<typename S>
176  ParameterDoc(const std::string& name, const std::string& doc, const S defaultValue, const S minValue, const S maxValue = std::numeric_limits<S>::max());
177  template<typename S>
178  ParameterDoc(const std::string& name, const std::string& doc, const S defaultValue);
179  */
182 
183  friend std::ostream& operator<< (std::ostream& o, const ParameterDoc& p);
184  };
185 
187  typedef std::vector<ParameterDoc> ParametersDoc;
188 
189  /*
190  Again, not used because fo gcc bug 9050
191  struct Parameter: public std::string
192  {
193  template<typename S>
194  Parameter(const S value);
195  Parameter(){}
196  };
197  */
199  typedef std::map<std::string, Parameter> Parameters;
200  typedef std::set<std::string> ParametersUsed;
201 
206 
207  Parametrizable();
208  Parametrizable(const std::string& className, const ParametersDoc paramsDoc, const Parameters& params);
209  virtual ~Parametrizable();
210 
211  std::string getParamValueString(const std::string& paramName);
212 
214  template<typename S>
215  S get(const std::string& paramName) { return lexical_cast<S>(getParamValueString(paramName)); }
216 
218  template<typename T>
219  inline std::vector<T> getVector(const std::string& paramName) {return lexical_cast_vector<T>(getParamValueString(paramName)); }
220 
221  friend std::ostream& operator<< (std::ostream& o, const Parametrizable& p);
222  };
223  std::ostream& operator<< (std::ostream& o, const Parametrizable::ParametersDoc& p);
224 } // namespace PointMatcherSupport
225 
226 #endif // __POINTMATCHER_PARAMETRIZABLE_H
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::ParameterDoc::comp
LexicalComparison comp
pointer to comparison function
Definition: Parametrizable.h:167
PointMatcherSupport::Parametrizable::Parametrizable
Parametrizable()
Construct a documentation of parameters from a description in the source.
Definition: Parametrizable.cpp:165
PointMatcherSupport::toParam
std::string toParam(const S &value)
Return the string value using lexical_cast.
Definition: Parametrizable.h:123
PointMatcherSupport::Parametrizable::parametersDoc
const ParametersDoc parametersDoc
documentation of parameters
Definition: Parametrizable.h:203
PointMatcherSupport::Parametrizable::ParameterDoc::doc
std::string doc
short documentation
Definition: Parametrizable.h:163
PointMatcherSupport::Parametrizable::parametersUsed
ParametersUsed parametersUsed
parameters whose value has actually been read
Definition: Parametrizable.h:205
PointMatcherSupport::Parametrizable::get
S get(const std::string &paramName)
Return the value of paramName, lexically-casted to S.
Definition: Parametrizable.h:215
PointMatcherSupport::operator<<
std::ostream & operator<<(std::ostream &o, const Parametrizable::ParameterDoc &p)
Dump the documentation of this parameter to a stream.
Definition: Parametrizable.cpp:51
testing::internal::string
::std::string string
Definition: gtest.h:1979
PointMatcherSupport::Parametrizable::ParameterDoc::operator<<
friend std::ostream & operator<<(std::ostream &o, const ParameterDoc &p)
Dump the documentation of this parameter to a stream.
Definition: Parametrizable.cpp:51
PointMatcherSupport::Parametrizable::ParametersUsed
std::set< std::string > ParametersUsed
Parameters whose value has been read.
Definition: Parametrizable.h:200
PointMatcherSupport::Parametrizable::LexicalComparison
bool(* LexicalComparison)(std::string a, std::string b)
A function that returns whether a is smaller than b.
Definition: Parametrizable.h:150
PointMatcherSupport::lexical_cast_scalar_to_string
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",...
Definition: Parametrizable.h:55
PointMatcherSupport::Parametrizable::ParametersDoc
std::vector< ParameterDoc > ParametersDoc
The documentation of all parameters.
Definition: Parametrizable.h:187
align_sequence.params
params
Definition: align_sequence.py:13
PointMatcherSupport::Parametrizable::ParameterDoc::defaultValue
std::string defaultValue
default value
Definition: Parametrizable.h:164
PointMatcherSupport::Parametrizable::parameters
Parameters parameters
parameters with their values encoded in string
Definition: Parametrizable.h:204
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:166
PointMatcherSupport::Parametrizable::Comp
static bool Comp(std::string a, std::string b)
Return whether a < b, lexically casted to S.
Definition: Parametrizable.h:154
PointMatcherSupport::Parametrizable::ParameterDoc::name
std::string name
name
Definition: Parametrizable.h:162
PointMatcherSupport::Parametrizable::getVector
std::vector< T > getVector(const std::string &paramName)
Return the value of paramName, lexically-casted std::vector<S>
Definition: Parametrizable.h:219
PointMatcherSupport::Parametrizable::InvalidParameter
An exception thrown when one tries to fetch the value of an unexisting parameter.
Definition: Parametrizable.h:144
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::Parameter
std::string Parameter
alias
Definition: Parametrizable.h:198
PointMatcherSupport::Parametrizable
The superclass of classes that are constructed using generic parameters. This class provides the para...
Definition: Parametrizable.h:141
PointMatcherSupport::Parametrizable::ParameterDoc
The documentation of a parameter.
Definition: Parametrizable.h:160
PointMatcherSupport::lexical_cast
Target lexical_cast(const Source &arg)
General case of lexical cast, use boost.
Definition: Parametrizable.h:77
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:165
PointMatcherSupport::Parametrizable::className
const std::string className
name of the class
Definition: Parametrizable.h:202
PointMatcherSupport::Parametrizable::Parameters
std::map< std::string, Parameter > Parameters
Parameters stored as a map of string->string.
Definition: Parametrizable.h:199
PointMatcherSupport::lexical_cast_vector
std::vector< T > lexical_cast_vector(const std::string &arg)
Special case of lexical cast to std::vector<T>
Definition: Parametrizable.h:84


libpointmatcher
Author(s):
autogenerated on Mon Sep 16 2024 02:24:09