MyraMath
Streams.h
Go to the documentation of this file.
1 // ========================================================================= //
2 // This file is part of MyraMath, copyright (c) 2014-2019 by Ryan A Chilton //
3 // and distributed by MyraCore, LLC. See LICENSE.txt for license terms. //
4 // ========================================================================= //
5 
6 #ifndef MYRAMATH_IO_STREAMS_H
7 #define MYRAMATH_IO_STREAMS_H
8 
14 #include <cstddef>
15 
16 namespace myra {
17 
18 // Forward declarations.
19 class InputStream;
20 class OutputStream;
21 template<class T> class ReaderWriter;
22 
24 template<class T> class ReaderWriter
25  {
26  public:
27 
29  static T read(InputStream& in)
30  { return T(in); }
31 
33  static void write(const T& t, OutputStream& out)
34  { t.write(out); }
35 
36  };
37 
40  {
41  public:
42  virtual void write_binary (const char* p, size_t n) = 0;
43  template<class T> void write(const T& t) { ReaderWriter<T>::write(t,*this); }
44  };
45 
48  {
49  public:
50  virtual void read_binary (char* p, size_t n) = 0;
51  template<class T> T read() { return ReaderWriter<T>::read(*this); }
52  };
53 
55 template<class T> InputStream& operator >> (InputStream& in, T& t)
56  { t = in.read<T>(); return in; }
57 
59 template<class T> OutputStream& operator << (OutputStream& out, const T& t)
60  { out.write(t); return out; }
61 
62 } // namespace
63 
64 #endif
Definition: syntax.dox:1
Abstraction layer, serializable objects write themselves to these.
Definition: Streams.h:39
Abstraction layer, deserializable objects read themselves from these.
Definition: Streams.h:47
static T read(InputStream &in)
.. read() should try to delegate to a stream constructor, return T(in)
Definition: Streams.h:29
static void write(const T &t, OutputStream &out)
.. and write() should try to delegate to a member function, T.write(out)
Definition: Streams.h:33