DUNE-FEM (unstable)

reader.hh
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
13#include <dune/fem/io/parameter/exceptions.hh>
14#include <dune/fem/io/parameter/parser.hh>
15
16namespace Dune
17{
18
19 namespace Fem
20 {
21
22 static inline const std::string& checkParameterExistsString()
23 {
24 static const std::string defaultKeyForExistCheck("__ParameterReader::check-exists__");
25 return defaultKeyForExistCheck;
26 }
27
28 // BasicParameterReader
29 // --------------------
30
31 template< class Parameter >
32 struct BasicParameterReader
33 {
34 typedef BasicParameterReader<Parameter> ThisType;
35 explicit BasicParameterReader ( Parameter parameter = Parameter() )
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_); }
257 Parameter parameter() { return 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 {
288 std::stringstream sstr;
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:
308 Parameter parameter_;
309 };
310
311
312
313 // ParameterReader
314 // ---------------
315
316 typedef BasicParameterReader< std::function< const std::string *( const std::string &, const std::string * ) > > ParameterReader;
317
318 } // namespace Fem
319
320} // namespace Dune
321
322#endif // #ifndef DUNE_FEM_IO_PARAMETER_READER_HH
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
Dune namespace.
Definition: alignedallocator.hh:13
constexpr auto get(std::integer_sequence< T, II... >, std::integral_constant< std::size_t, pos >={})
Return the entry at position pos of the given sequence.
Definition: integersequence.hh:22
STL namespace.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 13, 23:29, 2024)