dune-fem  2.4.1-rc
streams_inline.hh
Go to the documentation of this file.
1 #ifndef DUNE_FEM_STREAMS_INLINE_HH
2 #define DUNE_FEM_STREAMS_INLINE_HH
3 
4 #include <vector>
5 #include <array>
6 
7 #include <dune/common/array.hh>
8 #include <dune/common/fvector.hh>
9 
10 #include "streams.hh"
11 
12 namespace Dune
13 {
14 
15  namespace Fem
16  {
17 
18  template< class Traits >
19  inline OutStreamInterface< Traits > &
20  operator<< ( OutStreamInterface< Traits > &out,
21  const double value )
22  {
23  out.writeDouble( value );
24  return out;
25  }
26 
27  template< class Traits >
29  operator<< ( OutStreamInterface< Traits > &out,
30  const float value )
31  {
32  out.writeFloat( value );
33  return out;
34  }
35 
36  template< class Traits >
38  operator<< ( OutStreamInterface< Traits > &out,
39  const int value )
40  {
41  out.writeInt( value );
42  return out;
43  }
44 
45  template< class Traits >
47  operator<< ( OutStreamInterface< Traits > &out,
48  const char value )
49  {
50  out.writeChar( value );
51  return out;
52  }
53 
54  template< class Traits >
56  operator<< ( OutStreamInterface< Traits > &out,
57  const bool value )
58  {
59  out.writeBool( value );
60  return out;
61  }
62 
63  template< class Traits >
65  operator<< ( OutStreamInterface< Traits > &out,
66  const std :: string &s )
67  {
68  out.writeString( s );
69  return out;
70  }
71 
72  template< class Traits >
74  operator<< ( OutStreamInterface< Traits > &out,
75  const unsigned int value )
76  {
77  out.writeUnsignedInt( value );
78  return out;
79  }
80 
81  template< class Traits, class T >
83  operator<< ( OutStreamInterface< Traits > &out,
84  const std::complex<T> value )
85  {
86  out.writeDouble( std::real(value) );
87  out.writeDouble( std::imag(value) );
88  return out;
89  }
90 
91  template <class ulongint, class uint64>
93  {
94  // select uint64_t int
95  typedef ulongint UnsignedLongIntType;
96 
97  template < class Traits >
99  const UnsignedLongIntType& value )
100  {
101  // in case uint64_t int and uint64_t are not the same
102  // convert long to uint64_t, there will be no information loss
103  assert( sizeof(ulongint) <= sizeof(uint64) );
104  uint64 value64 = value ;
105  out.writeUnsignedInt64( value64 );
106  }
107 
108  template < class Traits >
110  UnsignedLongIntType& value )
111  {
112  assert( sizeof(ulongint) <= sizeof(uint64) );
113  // always read uint64_t int as uin64_t, since it is always written this way
114  uint64 value64;
115  in.readUnsignedInt64( value64 );
116  value = value64;
117  }
118  };
119 
120  //- in case uint64_t int and uint64_t are the same, do nothing
121  template <class ulongint>
122  struct SelectUnsignedLongInteger< ulongint, ulongint >
123  {
125  template < class Traits >
127  const UnsignedLongIntType value )
128  {
129  DUNE_THROW(NotImplemented,"method not implemented");
130  }
131 
132  template < class Traits >
134  UnsignedLongIntType& value )
135  {
136  DUNE_THROW(NotImplemented,"method not implemented");
137  }
138  };
139 
140  template< class Traits >
142  operator<< ( OutStreamInterface< Traits > &out,
143  const uint64_t value )
144  {
145  out.writeUnsignedInt64( value );
146  return out;
147  }
148 
149  template< class Traits >
151  operator<< ( OutStreamInterface< Traits > &out,
153  {
155  return out;
156  }
157 
158  template< class Traits, class T, std::size_t N >
160  operator<< ( OutStreamInterface< Traits > &out, const std::array< T, N > &value )
161  {
162  for( std::size_t i = 0; i < N; ++i )
163  out << value[ i ];
164  return out;
165  }
166 
167  template< class Traits, class T, int N >
169  operator<< ( OutStreamInterface< Traits > &out, const Dune::FieldVector< T, N > &value )
170  {
171  for( int i = 0; i < N; ++i )
172  out << value[ i ];
173  return out;
174  }
175 
176  template< class Traits, class T, class A >
178  operator<< ( OutStreamInterface< Traits > &out,
179  const std::vector< T, A > & value )
180  {
181  const size_t size = value.size();
182  out << size;
183  for( size_t i = 0; i < size; ++i )
184  out << value[ i ];
185  return out;
186  }
187 
188  template< class Traits >
191  double &value )
192  {
193  in.readDouble( value );
194  return in;
195  }
196 
197  template< class Traits >
200  float &value )
201  {
202  in.readFloat( value );
203  return in;
204  }
205 
206  template< class Traits >
209  int &value )
210  {
211  in.readInt( value );
212  return in;
213  }
214 
215  template< class Traits >
218  char &value )
219  {
220  in.readChar( value );
221  return in;
222  }
223 
224  template< class Traits >
227  bool &value )
228  {
229  in.readBool( value );
230  return in;
231  }
232 
233  template< class Traits >
236  std :: string &s )
237  {
238  in.readString( s );
239  return in;
240  }
241 
242  template< class Traits >
245  unsigned int &value )
246  {
247  in.readUnsignedInt( value );
248  return in;
249  }
250 
251  template< class Traits >
254  uint64_t &value )
255  {
256  in.readUnsignedInt64( value );
257  return in;
258  }
259 
260  template< class Traits >
264  {
266  return in;
267  }
268 
269  template< class Traits, class T, std::size_t N >
271  operator>> ( InStreamInterface< Traits > &in, std::array< T, N > &value )
272  {
273  for( std::size_t i = 0; i < N; ++i )
274  in >> value[ i ];
275  return in;
276  }
277 
278  template< class Traits, class T, int N >
280  operator>> ( InStreamInterface< Traits > &in, Dune::FieldVector< T, N > &value )
281  {
282  for( int i = 0; i < N; ++i )
283  in >> value[ i ];
284  return in;
285  }
286 
287  template< class Traits, class T >
290  std::complex<T> &value )
291  {
292  T r,i;
293  in.readDouble( r );
294  in.readDouble( i );
295  value = std::complex<T>(r,i);
296  return in;
297  }
298 
299  template< class Traits, class T, class A >
302  std::vector< T, A > & value )
303  {
304  size_t size = 0;
305  in >> size;
306  value.resize( size );
307  for( size_t i = 0; i < size; ++i )
308  in >> value[ i ];
309  return in;
310  }
311 
312  } // namespace Fem
313 
314 } // namespace Dune
315 
316 #endif // #ifndef DUNE_FEM_STREAMS_INLINE_HH
void readBool(bool &value)
read a bool from the stream
Definition: streams.hh:282
double imag(const std::complex< Double > &x)
Definition: double.hh:900
void writeDouble(const double value)
write a double to the stream
Definition: streams.hh:78
static void read(InStreamInterface< Traits > &in, UnsignedLongIntType &value)
Definition: streams_inline.hh:133
static void write(OutStreamInterface< Traits > &out, const UnsignedLongIntType &value)
Definition: streams_inline.hh:98
InStreamInterface< StreamTraits > & operator>>(InStreamInterface< StreamTraits > &in, DiscreteFunctionInterface< Impl > &df)
read a discrete function from an input stream
Definition: discretefunction_inline.hh:395
ulongint UnsignedLongIntType
Definition: streams_inline.hh:95
void writeUnsignedInt64(uint64_t value)
write an uint64_t to the stream
Definition: streams.hh:141
void writeBool(const bool value)
write a bool to the stream
Definition: streams.hh:114
void writeChar(const char value)
write a char to the stream
Definition: streams.hh:105
void readUnsignedInt64(uint64_t &value)
read an uint64_t from the stream
Definition: streams.hh:331
void writeInt(const int value)
write an int to the stream
Definition: streams.hh:96
Definition: coordinate.hh:4
abstract interface for an input stream
Definition: streams.hh:177
void readString(std::string &s)
read a string from the stream
Definition: streams.hh:302
void readUnsignedInt(unsigned int &value)
read an unsigned int from the stream
Definition: streams.hh:311
Definition: streams_inline.hh:92
void readChar(char &value)
read a char from the stream
Definition: streams.hh:261
void readFloat(float &value)
read a float from the stream
Definition: streams.hh:221
double real(const std::complex< Double > &x)
Definition: double.hh:890
void readDouble(double &value)
read a double from the stream
Definition: streams.hh:201
static void write(OutStreamInterface< Traits > &out, const UnsignedLongIntType value)
Definition: streams_inline.hh:126
void writeString(const std::string &s)
write a string to the stream
Definition: streams.hh:123
void writeUnsignedInt(unsigned int value)
write an unsigned int to the stream
Definition: streams.hh:132
static void read(InStreamInterface< Traits > &in, UnsignedLongIntType &value)
Definition: streams_inline.hh:109
void readInt(int &value)
read an int from the stream
Definition: streams.hh:241
abstract interface for an output stream
Definition: streams.hh:44
void writeFloat(const float value)
write a float to the stream
Definition: streams.hh:87