DGtal  1.1.0
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)
34 
35 #define Parameters_RECURSES
36 
37 #if !defined Parameters_h
38 
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& value )
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::ParameterValue::Self
ParameterValue Self
Definition: Parameters.h:120
DGtal::ParameterValue
Definition: Parameters.h:119
DGtal::ParameterValue::ParameterValue
ParameterValue(const std::string &v)
DGtal::Parameters::Parameters
Parameters(Self &&other)=default
Default move.
DGtal::detail::ValueConverter< X, std::string >::cast
static std::string cast(const X &value)
Definition: Parameters.h:107
DGtal::detail::ValueConverter< std::string, float >::cast
static float cast(const std::string &value)
Definition: Parameters.h:82
DGtal::Parameters::selfDisplay
void selfDisplay(std::ostream &out) const
DGtal::Parameters::operator[]
ParameterValue operator[](std::string name) const
DGtal::Parameters::Parameters
Parameters(std::string name, ParameterValue pv=ParameterValue())
DGtal::Parameters::Parameters
Parameters()=default
Default constructor.
DGtal::Parameters::operator|
Self operator|(const Self &other) const
DGtal::Parameters::count
bool count(std::string name) const
DGtal::Parameters::isValid
bool isValid() const
DGtal::ParameterValue::operator=
Self & operator=(const Self &v)=default
DGtal::Parameters
Definition: Parameters.h:146
DGtal::Parameters::operator()
Self & operator()(std::string name, ParameterValue pv=ParameterValue())
DGtal::Parameters::~Parameters
~Parameters()=default
Default destructor.
DGtal::detail::ValueConverter
Generic definition of a class for converting type X toward type Y.
Definition: Parameters.h:56
DGtal
DGtal is the top-level namespace which contains all DGtal functions and types.
Definition: ClosedIntegerHalfPlane.h:49
DGtal::ParameterValue::ParameterValue
ParameterValue(Self &&v)=default
DGtal::Parameters::myParameters
std::map< std::string, ParameterValue > myParameters
Definition: Parameters.h:198
DGtal::operator<<
std::ostream & operator<<(std::ostream &out, const ClosedIntegerHalfPlane< TSpace > &object)
DGtal::ParameterValue::selfDisplay
void selfDisplay(std::ostream &out) const
DGtal::ParameterValue::as
T as() const
DGtal::detail::ValueConverter< std::string, int >::cast
static int cast(const std::string &value)
Definition: Parameters.h:95
DGtal::ParameterValue::myValue
std::string myValue
Definition: Parameters.h:132
DGtal::Parameters::Self
Parameters Self
The type of *this.
Definition: Parameters.h:148
DGtal::ParameterValue::ParameterValue
ParameterValue(const Self &v)=default
DGtal::Parameters::Parameters
Parameters(const Self &other)=default
Default copy constructor.
DGtal::ParameterValue::ParameterValue
ParameterValue()=default
DGtal::Parameters::operator()
Self & operator()(const Self &params)
DGtal::Parameters::operator=
Self & operator=(const Self &other)=default
Default assignment operator.
DGtal::ParameterValue::ParameterValue
ParameterValue(const X &v)
DGtal::detail::ValueConverter< std::string, double >::cast
static double cast(const std::string &value)
Definition: Parameters.h:69
DGtal::detail::ValueConverter::cast
static Y cast(const X &value)
By default, it is impossible to do such conversions.
Definition: Parameters.h:58