dune-fem 2.12-git
Loading...
Searching...
No Matches
parser.hh
Go to the documentation of this file.
1#ifndef DUNE_FEM_IO_PARAMETER_PARSER_HH
2#define DUNE_FEM_IO_PARAMETER_PARSER_HH
3
4#include <sstream>
5#include <string>
6#include <type_traits>
7
9
10namespace Dune
11{
12
13 namespace Fem
14 {
15
16 // ParameterParser
17 // ---------------
18
19 template< class T >
21 {
22 static bool parse ( const std::string &s, T &value )
23 {
25 {
26 // if string is non-empty just copy completely
27 if( ! s.empty() )
28 value = s;
29 return true;
30 }
31
33 in >> value;
34
35 if( in.fail() )
36 return false;
37
38 char eof;
39 in >> eof;
40 return in.eof();
41 }
42
43 static std::string toString ( const T &value )
44 {
46 out << value;
47 return out.str();
48 }
49 };
50
51 template<>
52 struct ParameterParser< bool >
53 {
54 static bool parse ( const std::string &s, bool &value )
55 {
58 {
59 std::transform(w.begin(), w.end(), w.begin(), ::tolower);
60 if( (w == std::string( "false" )) || (w == std::string( "no" )) || (w == std::string( "0" )) )
61 {
62 value = false;
63 return true;
64 }
65
66 if( (w == std::string( "true" )) || (w == std::string( "yes" )) || (w == std::string( "1" )) )
67 {
68 value = true;
69 return true;
70 }
71 }
72 return false;
73 }
74
75 static std::string toString ( const bool &value )
76 {
77 return std::string( value ? "true" : "false" );
78 }
79 };
80
81 template< class F, int m, int n >
82 struct ParameterParser< FieldMatrix< F, m, n > >
83 {
84 static bool parse ( const std::string &s, FieldMatrix< F, m, n > &value )
85 {
87 char c;
88 for( int i = 0; i < m; ++i )
89 {
90 if( i > 0 )
91 {
92 in >> c;
93 if( c != ',' )
94 return false;
95 }
96
97 for( int j = 0; j < n; ++j )
98 in >> value[ i ][ j ];
99 }
100 in >> c; // read eof
101 return in.eof();
102 }
103
105 {
107 for( int i = 0; i < m; ++i )
108 {
109 out << (i > 0 ? "," : "");
110 for( int j = 0; j< n; ++j )
111 out << " " << value[ i ][ j ];
112 }
113 return out.str();
114 }
115 };
116
117 } // namespace Fem
118
119} // namespace Dune
120
121#endif // #ifndef DUNE_FEM_IO_PARAMETER_PARSER_HH
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
static bool parse(const std::string &s, bool &value)
Definition parser.hh:54
static std::string toString(const bool &value)
Definition parser.hh:75
static std::string toString(const FieldMatrix< F, m, n > &value)
Definition parser.hh:104
static bool parse(const std::string &s, FieldMatrix< F, m, n > &value)
Definition parser.hh:84
T begin(T... args)
T empty(T... args)
T end(T... args)
T transform(T... args)