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