MyraMath
ReaderWriter.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_READERWRITER_H
7 #define MYRAMATH_IO_READERWRITER_H
8 
14 #include <cstddef>
15 
16 namespace myra {
17 
18 // Forward declarations.
19 class OutputStream;
20 class InputStream;
21 template<class T> class ReaderWriter;
22 
23 // Helper functions for ReaderWriter<> specializations of "plain old data" types (e.g. char's, int's, float's)
24 template<class POD> void write_pod (const POD& pod, myra::OutputStream& out)
25  { out.write_binary(reinterpret_cast<const char*>(&pod), sizeof(pod)); }
26 template<class POD> POD read_pod (myra::InputStream& in)
27  { POD pod; in.read_binary(reinterpret_cast<char*>(&pod), sizeof(pod)); return pod; }
28 
30 template<class T> class ReaderWriter;
31 
33 template<> class ReaderWriter<size_t>
34  {
35  public:
36  static size_t read(InputStream& in)
37  { return read_pod<size_t>(in); }
38  static void write(const size_t& s, OutputStream& out)
39  { write_pod<size_t>(s,out); }
40  };
41 
43 template<> class ReaderWriter<int>
44  {
45  public:
46  static int read(InputStream& in)
47  { return read_pod<int>(in); }
48  static void write(const int& i, OutputStream& out)
49  { write_pod<int>(i,out); }
50  };
51 
53 template<> class ReaderWriter<float>
54  {
55  public:
56  static float read(InputStream& in)
57  { return read_pod<float>(in); }
58  static void write(const float& f, OutputStream& out)
59  { write_pod<float>(f,out); }
60  };
61 
63 template<> class ReaderWriter<double>
64  {
65  public:
66  static double read(InputStream& in)
67  { return read_pod<double>(in); }
68  static void write(const double& d, OutputStream& out)
69  { write_pod<double>(d,out); }
70  };
71 
73 template<> class ReaderWriter<char>
74  {
75  public:
76  static char read(InputStream& in)
77  { return read_pod<char>(in); }
78  static void write(const char& c, OutputStream& out)
79  { write_pod<char>(c,out); }
80  };
81 
82 } // namespace
83 
84 #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
Specialize this class liberally. See /detail for example specializations, for various std::containers...
Definition: ReaderWriter.h:21
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