1#ifndef DUNE_FEM_IO_PARAMETER_READER_HH
2#define DUNE_FEM_IO_PARAMETER_READER_HH
13#include <dune/fem/io/parameter/exceptions.hh>
14#include <dune/fem/io/parameter/parser.hh>
22 static inline const std::string& checkParameterExistsString()
24 static const std::string defaultKeyForExistCheck(
"__ParameterReader::check-exists__");
25 return defaultKeyForExistCheck;
31 template<
class Parameter >
32 struct BasicParameterReader
34 typedef BasicParameterReader<Parameter> ThisType;
35 explicit BasicParameterReader ( Parameter parameter = Parameter() )
36 : parameter_(
std::move( parameter ) )
46 bool exists (
const std::string &key )
const
48 return static_cast< bool >( parameter_( key, &checkParameterExistsString() ) );
61 void get (
const std::string &key, T &value )
const
63 const std::string *
string = parameter_( key,
nullptr );
65 DUNE_THROW( ParameterNotFound,
"Parameter '" << key <<
"' not found." );
66 if( !ParameterParser< T >::parse( *
string, value ) )
67 DUNE_THROW( ParameterInvalid,
"Parameter '" << key <<
"' invalid." );
81 void get (
const std::string &key,
const T &defaultValue, T &value )
const
83 const std::string defaultString = ParameterParser< T >::toString( defaultValue );
84 const std::string *
string = parameter_( key, &defaultString );
86 if( !ParameterParser< T >::parse( *
string, value ) )
87 DUNE_THROW( ParameterInvalid,
"Parameter '" << key <<
"' invalid." );
100 void get (
const std::string &key,
const char* defaultValue, std::string &value )
const
102 const std::string defaultString( defaultValue );
103 const std::string *
string = parameter_( key, &defaultString );
105 if( !ParameterParser< std::string >::parse( *
string, value ) )
106 DUNE_THROW( ParameterInvalid,
"Parameter '" << key <<
"' invalid." );
119 template<
class T,
class Val
idator >
120 void getValid (
const std::string &key,
const Validator &validator, T &value )
const
122 const std::string *
string = parameter_( key,
nullptr );
124 DUNE_THROW( ParameterNotFound,
"Parameter '" << key <<
"' not found." );
125 if( !ParameterParser< T >::parse( *
string, value ) || !validator( value ) )
126 DUNE_THROW( ParameterInvalid,
"Parameter '" << key <<
"' invalid." );
140 template<
class T,
class Val
idator >
141 void getValid (
const std::string &key,
const T &defaultValue,
const Validator &validator, T &value )
const
143 const std::string defaultString = ParameterParser< T >::toString( defaultValue );
144 const std::string *
string = parameter_( key, &defaultString );
146 if( !ParameterParser< T >::parse( *
string, value ) || !validator( value ) )
147 DUNE_THROW( ParameterInvalid,
"Parameter '" << key <<
"' invalid." );
161 T getValue (
const std::string &key )
const
180 T getValue (
const std::string &key,
const T &defaultValue )
const
182 T value = defaultValue;
183 get( key, defaultValue, value );
198 template<
class T,
class Val
idator >
199 T getValidValue (
const std::string &key,
const Validator &validator )
const
202 getValid( key, validator, value );
218 template<
class T,
class Val
idator >
219 T getValidValue (
const std::string &key,
const T &defaultValue,
const Validator &validator )
const
222 getValid( key, defaultValue, validator, value );
227 int getEnum (
const std::string &key,
const std::string (&values)[ n ] )
const
229 const std::string *
string = parameter_( key,
nullptr );
231 DUNE_THROW( ParameterNotFound,
"Parameter '" << key <<
"' not found." );
232 return getEnumeration( key, *
string, values );
236 int getEnum (
const std::string &key,
const std::string (&values)[ n ],
int defaultValue )
const
238 const std::string *
string = parameter_( key, &values[ defaultValue ] );
239 return getEnumeration( key, *
string, values );
242 int getEnum (
const std::string &key,
const std::vector<std::string> &values )
const
244 const std::string *
string = parameter_( key,
nullptr );
246 DUNE_THROW( ParameterNotFound,
"Parameter '" << key <<
"' not found." );
247 return getEnumeration( key, *
string, values );
250 int getEnum (
const std::string &key,
const std::vector<std::string> &values,
int defaultValue )
const
252 const std::string *
string = parameter_( key, &values[ defaultValue ] );
253 return getEnumeration( key, *
string, values );
256 ThisType* clone()
const {
return new ThisType(parameter_); }
257 Parameter parameter() {
return parameter_; }
258 const Parameter parameter()
const {
return parameter_; }
263 static int getEnumeration (
const std::string &key,
const std::string& value,
const std::string (&values)[ n ] )
265 return getEnumeration( key, value, values, n );
268 static int getEnumeration (
const std::string &key,
const std::string& value,
const std::vector<std::string>& values )
270 return getEnumeration( key, value, values, values.size() );
274 template <
class StringVector>
275 static int getEnumeration (
const std::string &key,
const std::string& value,
const StringVector &values,
const int n )
277 for(
int i = 0; i < n; ++i )
279 if( value == values[ i ] )
284 if( !ParameterParser< int >::parse( value, j ) )
286 if( (j < 0) || (j >= n) )
288 std::stringstream sstr;
289 if ( value.find(
"help") == std::string::npos )
290 sstr << std::endl <<
"Parameter '" << key <<
"' invalid." << std::endl;
292 sstr <<
"Help for parameter '" << key <<
"':" << std::endl;
294 sstr <<
"Valid values are: ";
295 for(
int i = 0; i < n; ++i )
296 sstr << values[ i ] << (i < n-1 ?
", " :
"");
299 if ( value.find(
"help") == std::string::npos )
302 throw std::runtime_error(sstr.str());
308 Parameter parameter_;
316 typedef BasicParameterReader< std::function<
const std::string *(
const std::string &,
const std::string * ) > > ParameterReader;
#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