DGtal  1.0.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 <string>
45 #include <sstream>
46 #include "DGtal/base/Common.h"
48 
49 namespace DGtal
50 {
51  namespace detail
52  {
54  template <typename X, typename Y>
55  struct ValueConverter {
57  static Y cast( const X& value )
58  {
59  ASSERT( false
60  && "[ValueConverter<X,Y>::cast] there is no such generic type converter." );
61  return Y();
62  }
63  };
64 
66  template <>
67  struct ValueConverter< std::string, double >{
68  static double cast( const std::string& value )
69  { return atof( value.c_str() ); }
70  };
71 
73  template <>
74  struct ValueConverter< std::string, float >{
75  static float cast( const std::string& value )
76  { return (float) atof( value.c_str() ); }
77  };
78 
80  template <>
81  struct ValueConverter< std::string, int >{
82  static int cast( const std::string& value )
83  { return atoi( value.c_str() ); }
84  };
86  template < typename X >
87  struct ValueConverter< X, std::string >{
88  static std::string cast( const X& value )
89  {
90  std::ostringstream ss;
91  ss << value;
92  return ss.str();
93  }
94  };
95 
96  } // namespace detail
97 
100  struct ParameterValue {
102  ParameterValue () = default;
103  ParameterValue ( const Self& v ) = default;
104  ParameterValue ( Self&& v ) = default;
105  Self& operator=( const Self& v ) = default;
106  ParameterValue ( const std::string& v );
107  template <typename X>
108  ParameterValue ( const X& v );
109  template <typename T>
110  T as() const;
111  void selfDisplay ( std::ostream & out ) const;
112  protected:
113  std::string myValue;
114  };
115 
122  std::ostream&
123  operator<< ( std::ostream & out, const ParameterValue & object );
124 
127  struct Parameters {
129  typedef Parameters Self;
130 
132  Parameters() = default;
134  ~Parameters() = default;
136  Parameters( const Self& other ) = default;
138  Parameters( Self&& other ) = default;
140  Self& operator=( const Self& other ) = default;
144  Parameters( std::string name, ParameterValue pv = ParameterValue() );
148  Self& operator()( std::string name, ParameterValue pv = ParameterValue() );
151  Self& operator()( const Self& params );
154  ParameterValue operator[]( std::string name ) const;
157  bool count( std::string name ) const;
158 
161  Self operator|( const Self& other ) const;
162 
163  // ----------------------- Interface --------------------------------------
164  public:
165 
170  void selfDisplay ( std::ostream & out ) const;
171 
176  bool isValid() const;
177 
178  protected:
179  std::map< std::string, ParameterValue > myParameters;
180  };
181 
188  std::ostream&
189  operator<< ( std::ostream & out, const Parameters & object );
190 
191 } // namespace DGtal
192 
193 
195 // Includes inline functions.
196 #include "DGtal/helpers/Parameters.ih"
197 
198 // //
200 
201 #endif // !defined Parameters_h
202 
203 #undef Parameters_RECURSES
204 #endif // else defined(Parameters_RECURSES)
ParameterValue Self
Definition: Parameters.h:101
~Parameters()=default
Default destructor.
Parameters()=default
Default constructor.
std::string myValue
Definition: Parameters.h:113
static double cast(const std::string &value)
Definition: Parameters.h:68
static Y cast(const X &value)
By default, it is impossible to do such conversions.
Definition: Parameters.h:57
bool isValid() const
std::map< std::string, ParameterValue > myParameters
Definition: Parameters.h:179
Self & operator()(std::string name, ParameterValue pv=ParameterValue())
Parameters Self
The type of *this.
Definition: Parameters.h:129
void selfDisplay(std::ostream &out) const
Self operator|(const Self &other) const
bool count(std::string name) const
static int cast(const std::string &value)
Definition: Parameters.h:82
std::ostream & operator<<(std::ostream &out, const ClosedIntegerHalfPlane< TSpace > &object)
static std::string cast(const X &value)
Definition: Parameters.h:88
Self & operator=(const Self &v)=default
ParameterValue operator[](std::string name) const
void selfDisplay(std::ostream &out) const
DGtal is the top-level namespace which contains all DGtal functions and types.
Generic definition of a class for converting type X toward type Y.
Definition: Parameters.h:55
static float cast(const std::string &value)
Definition: Parameters.h:75
Self & operator=(const Self &other)=default
Default assignment operator.