DUNE-FEM (unstable)

parser.hh
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 >
20 struct ParameterParser
21 {
22 static bool parse ( const std::string &s, T &value )
23 {
24 if constexpr ( std::is_same< T, std::string >::value )
25 {
26 // if string is non-empty just copy completely
27 if( ! s.empty() )
28 value = s;
29 return true;
30 }
31
32 std::istringstream in( s );
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 {
45 std::ostringstream out;
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 {
56 std::string w;
57 if( ParameterParser< std::string >::parse( s, w ) )
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 {
86 std::istringstream in( s );
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
104 static std::string toString ( const FieldMatrix< F, m, n > &value )
105 {
106 std::ostringstream out;
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
std::string toString(Precision p)
map precision to VTK type name
Definition: common.hh:280
Implements a matrix constructed from a given type representing a field and compile-time given number ...
Dune namespace.
Definition: alignedallocator.hh:13
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 21, 23:30, 2024)