dune-fem 2.12-git
Loading...
Searching...
No Matches
reader.hh
Go to the documentation of this file.
1#ifndef DUNE_FEM_IO_PARAMETER_READER_HH
2#define DUNE_FEM_IO_PARAMETER_READER_HH
3
4#include <cassert>
5
6#include <functional>
7#include <iostream>
8#include <string>
9#include <sstream>
10#include <utility>
11#include <stdexcept>
12
15
16namespace Dune
17{
18
19 namespace Fem
20 {
21
23 {
24 static const std::string defaultKeyForExistCheck("__ParameterReader::check-exists__");
25 return defaultKeyForExistCheck;
26 }
27
28 // BasicParameterReader
29 // --------------------
30
31 template< class Parameter >
33 {
36 : parameter_( std::move( parameter ) )
37 {}
38
46 bool exists ( const std::string &key ) const
47 {
48 return static_cast< bool >( parameter_( key, &checkParameterExistsString() ) );
49 }
50
60 template< class T >
61 void get ( const std::string &key, T &value ) const
62 {
63 const std::string *string = parameter_( key, nullptr );
64 if( !string )
65 DUNE_THROW( ParameterNotFound, "Parameter '" << key << "' not found." );
66 if( !ParameterParser< T >::parse( *string, value ) )
67 DUNE_THROW( ParameterInvalid, "Parameter '" << key << "' invalid." );
68 }
69
80 template< class T >
81 void get ( const std::string &key, const T &defaultValue, T &value ) const
82 {
83 const std::string defaultString = ParameterParser< T >::toString( defaultValue );
84 const std::string *string = parameter_( key, &defaultString );
85 assert( string );
86 if( !ParameterParser< T >::parse( *string, value ) )
87 DUNE_THROW( ParameterInvalid, "Parameter '" << key << "' invalid." );
88 }
89
100 void get ( const std::string &key, const char* defaultValue, std::string &value ) const
101 {
102 const std::string defaultString( defaultValue );
103 const std::string *string = parameter_( key, &defaultString );
104 assert( string );
105 if( !ParameterParser< std::string >::parse( *string, value ) )
106 DUNE_THROW( ParameterInvalid, "Parameter '" << key << "' invalid." );
107 }
108
119 template< class T, class Validator >
120 void getValid ( const std::string &key, const Validator &validator, T &value ) const
121 {
122 const std::string *string = parameter_( key, nullptr );
123 if( !string )
124 DUNE_THROW( ParameterNotFound, "Parameter '" << key << "' not found." );
125 if( !ParameterParser< T >::parse( *string, value ) || !validator( value ) )
126 DUNE_THROW( ParameterInvalid, "Parameter '" << key << "' invalid." );
127 }
128
140 template< class T, class Validator >
141 void getValid ( const std::string &key, const T &defaultValue, const Validator &validator, T &value ) const
142 {
143 const std::string defaultString = ParameterParser< T >::toString( defaultValue );
144 const std::string *string = parameter_( key, &defaultString );
145 assert( string );
146 if( !ParameterParser< T >::parse( *string, value ) || !validator( value ) )
147 DUNE_THROW( ParameterInvalid, "Parameter '" << key << "' invalid." );
148 }
149
160 template< class T >
161 T getValue ( const std::string &key ) const
162 {
163 T value;
164 get( key, value );
165 return value;
166 }
167
179 template< class T >
180 T getValue ( const std::string &key, const T &defaultValue ) const
181 {
182 T value = defaultValue;
183 get( key, defaultValue, value );
184 return value;
185 }
186
198 template< class T, class Validator >
199 T getValidValue ( const std::string &key, const Validator &validator ) const
200 {
201 T value;
202 getValid( key, validator, value );
203 return value;
204 }
205
218 template< class T, class Validator >
219 T getValidValue ( const std::string &key, const T &defaultValue, const Validator &validator ) const
220 {
221 T value;
222 getValid( key, defaultValue, validator, value );
223 return value;
224 }
225
226 template< int n >
227 int getEnum ( const std::string &key, const std::string (&values)[ n ] ) const
228 {
229 const std::string *string = parameter_( key, nullptr );
230 if( !string )
231 DUNE_THROW( ParameterNotFound, "Parameter '" << key << "' not found." );
232 return getEnumeration( key, *string, values );
233 }
234
235 template< int n >
236 int getEnum ( const std::string &key, const std::string (&values)[ n ], int defaultValue ) const
237 {
238 const std::string *string = parameter_( key, &values[ defaultValue ] );
239 return getEnumeration( key, *string, values );
240 }
241
242 int getEnum ( const std::string &key, const std::vector<std::string> &values ) const
243 {
244 const std::string *string = parameter_( key, nullptr );
245 if( !string )
246 DUNE_THROW( ParameterNotFound, "Parameter '" << key << "' not found." );
247 return getEnumeration( key, *string, values );
248 }
249
250 int getEnum ( const std::string &key, const std::vector<std::string> &values, int defaultValue ) const
251 {
252 const std::string *string = parameter_( key, &values[ defaultValue ] );
253 return getEnumeration( key, *string, values );
254 }
255
256 ThisType* clone() const { return new ThisType(parameter_); }
258 const Parameter parameter() const { return parameter_; }
259 void reset() {}
260
261 private:
262 template< int n >
263 static int getEnumeration ( const std::string &key, const std::string& value, const std::string (&values)[ n ] )
264 {
265 return getEnumeration( key, value, values, n );
266 }
267
268 static int getEnumeration ( const std::string &key, const std::string& value, const std::vector<std::string>& values )
269 {
270 return getEnumeration( key, value, values, values.size() );
271 }
272
273 // implementation of getEnumeration
274 template <class StringVector>
275 static int getEnumeration ( const std::string &key, const std::string& value, const StringVector &values, const int n )
276 {
277 for( int i = 0; i < n; ++i )
278 {
279 if( value == values[ i ] )
280 return i;
281 }
282
283 int j = -1;
284 if( !ParameterParser< int >::parse( value, j ) )
285 j = -1;
286 if( (j < 0) || (j >= n) )
287 {
289 if ( value.find("help") == std::string::npos )
290 sstr << std::endl << "Parameter '" << key << "' invalid." << std::endl;
291 else
292 sstr << "Help for parameter '" << key << "':" << std::endl;
293
294 sstr << "Valid values are: ";
295 for( int i = 0; i < n; ++i )
296 sstr << values[ i ] << (i < n-1 ? ", " : "");
297 sstr << std::endl;
298
299 if ( value.find("help") == std::string::npos )
300 DUNE_THROW( ParameterInvalid, sstr.str() );
301 else
302 throw std::runtime_error(sstr.str());
303 }
304 return j;
305 }
306
307 protected:
309 };
310
311
312
313 // ParameterReader
314 // ---------------
315
317
318 } // namespace Fem
319
320} // namespace Dune
321
322#endif // #ifndef DUNE_FEM_IO_PARAMETER_READER_HH
#define DUNE_THROW(E,...)
STL namespace.
static const std::string & checkParameterExistsString()
Definition reader.hh:22
Container for User Specified Parameters.
Definition io/parameter.hh:191
Definition io/parameter/exceptions.hh:17
Definition io/parameter/exceptions.hh:26
Definition parser.hh:21
static bool parse(const std::string &s, T &value)
Definition parser.hh:22
static std::string toString(const T &value)
Definition parser.hh:43
Definition reader.hh:33
void reset()
Definition reader.hh:259
void get(const std::string &key, const char *defaultValue, std::string &value) const
get optional parameter (special case for string)
Definition reader.hh:100
int getEnum(const std::string &key, const std::string(&values)[n]) const
Definition reader.hh:227
Parameter parameter()
Definition reader.hh:257
T getValidValue(const std::string &key, const T &defaultValue, const Validator &validator) const
get optional parameter
Definition reader.hh:219
const Parameter parameter() const
Definition reader.hh:258
ThisType * clone() const
Definition reader.hh:256
void get(const std::string &key, const T &defaultValue, T &value) const
get optional parameter
Definition reader.hh:81
void getValid(const std::string &key, const T &defaultValue, const Validator &validator, T &value) const
get optional parameter
Definition reader.hh:141
int getEnum(const std::string &key, const std::string(&values)[n], int defaultValue) const
Definition reader.hh:236
void get(const std::string &key, T &value) const
get mandatory parameter
Definition reader.hh:61
T getValue(const std::string &key) const
get mandatory parameter
Definition reader.hh:161
T getValue(const std::string &key, const T &defaultValue) const
get optional parameter
Definition reader.hh:180
int getEnum(const std::string &key, const std::vector< std::string > &values) const
Definition reader.hh:242
bool exists(const std::string &key) const
check, whether a parameter is defined
Definition reader.hh:46
Parameter parameter_
Definition reader.hh:308
T getValidValue(const std::string &key, const Validator &validator) const
get optional parameter
Definition reader.hh:199
BasicParameterReader(Parameter parameter=Parameter())
Definition reader.hh:35
int getEnum(const std::string &key, const std::vector< std::string > &values, int defaultValue) const
Definition reader.hh:250
BasicParameterReader< Parameter > ThisType
Definition reader.hh:34
void getValid(const std::string &key, const Validator &validator, T &value) const
get mandatory parameter
Definition reader.hh:120
T endl(T... args)
T size(T... args)
T str(T... args)