DGtal  1.5.beta
Parameters.h
1 
17 #pragma once
18 
31 #if defined(Parameters_RECURSES)
32 #error Recursive header files inclusion detected in Parameters.h
33 #else // defined(Parameters_RECURSES)
35 #define Parameters_RECURSES
36 
37 #if !defined Parameters_h
39 #define Parameters_h
40 
42 // Inclusions
43 #include <iostream>
44 #include <map>
45 #include <string>
46 #include <sstream>
47 #include "DGtal/base/Common.h"
49 
50 namespace DGtal
51 {
52  namespace detail
53  {
55  template <typename X, typename Y>
56  struct ValueConverter {
58  static Y cast( const X& )
59  {
60  ASSERT( false
61  && "[ValueConverter<X,Y>::cast] there is no such generic type converter." );
62  return Y();
63  }
64  };
65 
67  template <>
68  struct ValueConverter< std::string, double >{
69  static double cast( const std::string& value )
70  {
71  // note (JOL): cannot use atof (C) since it uses a different locale as program_options (C++).
72  double val;
73  std::istringstream iss( value );
74  iss >> val;
75  return val;
76  }
77  };
78 
80  template <>
81  struct ValueConverter< std::string, float >{
82  static float cast( const std::string& value )
83  {
84  // note (JOL): cannot use atof (C) since it uses a different locale as program_options (C++).
85  float val;
86  std::istringstream iss( value );
87  iss >> val;
88  return val;
89  }
90  };
91 
93  template <>
94  struct ValueConverter< std::string, int >{
95  static int cast( const std::string& value )
96  {
97  // note (JOL): cannot use atoi (C) since it uses a different locale as program_options (C++).
98  int val;
99  std::istringstream iss( value );
100  iss >> val;
101  return val;
102  }
103  };
105  template < typename X >
106  struct ValueConverter< X, std::string >{
107  static std::string cast( const X& value )
108  {
109  std::ostringstream ss;
110  ss << value;
111  return ss.str();
112  }
113  };
114 
115  } // namespace detail
116 
119  struct ParameterValue {
121  ParameterValue () = default;
122  ParameterValue ( const Self& v ) = default;
123  ParameterValue ( Self&& v ) = default;
124  Self& operator=( const Self& v ) = default;
125  ParameterValue ( const std::string& v );
126  template <typename X>
127  ParameterValue ( const X& v );
128  template <typename T>
129  T as() const;
130  void selfDisplay ( std::ostream & out ) const;
131  protected:
132  std::string myValue;
133  };
134 
141  std::ostream&
142  operator<< ( std::ostream & out, const ParameterValue & object );
143 
146  struct Parameters {
148  typedef Parameters Self;
149 
151  Parameters() = default;
153  ~Parameters() = default;
155  Parameters( const Self& other ) = default;
157  Parameters( Self&& other ) = default;
159  Self& operator=( const Self& other ) = default;
163  Parameters( std::string name, ParameterValue pv = ParameterValue() );
167  Self& operator()( std::string name, ParameterValue pv = ParameterValue() );
170  Self& operator()( const Self& params );
173  ParameterValue operator[]( std::string name ) const;
176  bool count( std::string name ) const;
177 
180  Self operator|( const Self& other ) const;
181 
182  // ----------------------- Interface --------------------------------------
183  public:
184 
189  void selfDisplay ( std::ostream & out ) const;
190 
195  bool isValid() const;
196 
197  protected:
198  std::map< std::string, ParameterValue > myParameters;
199  };
200 
207  std::ostream&
208  operator<< ( std::ostream & out, const Parameters & object );
209 
210 } // namespace DGtal
211 
212 
214 // Includes inline functions.
215 #include "DGtal/helpers/Parameters.ih"
216 
217 // //
219 
220 #endif // !defined Parameters_h
221 
222 #undef Parameters_RECURSES
223 #endif // else defined(Parameters_RECURSES)
DGtal is the top-level namespace which contains all DGtal functions and types.
std::ostream & operator<<(std::ostream &out, const ATu0v1< TKSpace, TLinearAlgebra > &object)
ParameterValue(const Self &v)=default
std::string myValue
Definition: Parameters.h:132
void selfDisplay(std::ostream &out) const
ParameterValue(Self &&v)=default
Self & operator=(const Self &v)=default
ParameterValue(const std::string &v)
ParameterValue(const X &v)
ParameterValue Self
Definition: Parameters.h:120
Parameters(std::string name, ParameterValue pv=ParameterValue())
ParameterValue operator[](std::string name) const
Parameters Self
The type of *this.
Definition: Parameters.h:148
Parameters(const Self &other)=default
Default copy constructor.
std::map< std::string, ParameterValue > myParameters
Definition: Parameters.h:198
Self & operator()(std::string name, ParameterValue pv=ParameterValue())
bool isValid() const
Parameters(Self &&other)=default
Default move.
void selfDisplay(std::ostream &out) const
~Parameters()=default
Default destructor.
Parameters()=default
Default constructor.
bool count(std::string name) const
Self & operator()(const Self &params)
Self & operator=(const Self &other)=default
Default assignment operator.
Self operator|(const Self &other) const
static std::string cast(const X &value)
Definition: Parameters.h:107
static double cast(const std::string &value)
Definition: Parameters.h:69
static float cast(const std::string &value)
Definition: Parameters.h:82
static int cast(const std::string &value)
Definition: Parameters.h:95
Generic definition of a class for converting type X toward type Y.
Definition: Parameters.h:56
static Y cast(const X &)
By default, it is impossible to do such conversions.
Definition: Parameters.h:58