dune-fem  2.4.1-rc
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 
8 #include <dune/common/fmatrix.hh>
9 
10 namespace 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  {
24  std::istringstream in( s );
25  in >> value;
26  if( std::is_same< T, std::string >::value && s.empty() )
27  return true;
28  if( in.fail() )
29  return false;
30  char eof;
31  in >> eof;
32  return in.eof();
33  }
34 
35  static std::string toString ( const T &value )
36  {
37  std::ostringstream out;
38  out << value;
39  return out.str();
40  }
41  };
42 
43  template<>
44  struct ParameterParser< bool >
45  {
46  static bool parse ( const std::string &s, bool &value )
47  {
48  std::string w;
50  {
51  if( (w == std::string( "false" )) || (w == std::string( "no" )) || (w == std::string( "0" )) )
52  {
53  value = false;
54  return true;
55  }
56 
57  if( (w == std::string( "true" )) || (w == std::string( "yes" )) || (w == std::string( "1" )) )
58  {
59  value = true;
60  return true;
61  }
62  }
63  return false;
64  }
65 
66  static std::string toString ( const bool &value )
67  {
68  return std::string( value ? "true" : "false" );
69  }
70  };
71 
72  template< class F, int m, int n >
73  struct ParameterParser< FieldMatrix< F, m, n > >
74  {
75  static bool parse ( const std::string &s, FieldMatrix< F, m, n > &value )
76  {
77  std::istringstream in( s );
78  char c;
79  for( int i = 0; i < m; ++i )
80  {
81  if( i > 0 )
82  {
83  in >> c;
84  if( c != ',' )
85  return false;
86  }
87 
88  for( int j = 0; j < n; ++j )
89  in >> value[ i ][ j ];
90  }
91  in >> c; // read eof
92  return in.eof();
93  }
94 
95  static std::string toString ( const FieldMatrix< F, m, n > &value )
96  {
97  std::ostringstream out;
98  for( int i = 0; i < m; ++i )
99  {
100  out << (i > 0 ? "," : "");
101  for( int j = 0; j< n; ++j )
102  out << " " << value[ i ][ j ];
103  }
104  return out.str();
105  }
106  };
107 
108  } // namespace Fem
109 
110 } // namespace Dune
111 
112 #endif // #ifndef DUNE_FEM_IO_PARAMETER_PARSER_HH
static bool parse(const std::string &s, FieldMatrix< F, m, n > &value)
Definition: parser.hh:75
static bool parse(const std::string &s, bool &value)
Definition: parser.hh:46
static bool parse(const std::string &s, T &value)
Definition: parser.hh:22
Definition: coordinate.hh:4
Definition: parser.hh:20
static std::string toString(const FieldMatrix< F, m, n > &value)
Definition: parser.hh:95
static std::string toString(const bool &value)
Definition: parser.hh:66
static std::string toString(const T &value)
Definition: parser.hh:35