Jpp  18.2.1
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JStreamIO.hh
Go to the documentation of this file.
1 #ifndef __JIO__JSTREAMIO__
2 #define __JIO__JSTREAMIO__
3 
4 #include <istream>
5 #include <ostream>
6 
7 #include "JIO/JSerialisable.hh"
8 
9 
10 /**
11  * \author mdejong
12  */
13 
14 namespace JIO {}
15 namespace JPP { using namespace JIO; }
16 
17 namespace JIO {
18 
19 
20  /**
21  * Binary input based on std::istream.
22  * This class implements the JReader interface.
23  */
24  class JStreamReader :
25  public JReader
26  {
27  public:
28  /**
29  * Constructor.
30  *
31  * \param __in input stream
32  */
33  JStreamReader(std::istream& __in) :
34  in(__in)
35  {}
36 
37 
38  /**
39  * Status of reader.
40  *
41  * \return status of this reader
42  */
43  virtual bool getStatus() const override
44  {
45  return (bool) in;
46  }
47 
48 
49  /**
50  * Clear status of reader.
51  */
52  virtual void clear() override
53  {
54  in.clear();
55  }
56 
57 
58  /**
59  * Read byte array.
60  *
61  * \param buffer pointer to byte array
62  * \param length number of bytes
63  * \return number of bytes read
64  */
65  virtual int read(char* buffer, const int length) override
66  {
67  in.read(buffer, length);
68 
69  return in.gcount();
70  }
71 
72  protected:
73  std::istream& in;
74  };
75 
76 
77  /**
78  * Binary output based on std::ostream.
79  * This class implements the JWriter interface.
80  */
81  class JStreamWriter :
82  public JWriter
83  {
84  public:
85  /**
86  * Constructor.
87  *
88  * \param __out output stream
89  */
90  JStreamWriter(std::ostream& __out) :
91  out(__out)
92  {}
93 
94 
95  /**
96  * Status of writer.
97  *
98  * \return status of this writer
99  */
100  virtual bool getStatus() const override
101  {
102  return (bool) out;
103  }
104 
105 
106  /**
107  * Write byte array.
108  *
109  * \param buffer pointer to byte array
110  * \param length number of bytes
111  * \return number of bytes written
112  */
113  virtual int write(const char* buffer, const int length) override
114  {
115  out.write(buffer, length);
116 
117  return length;
118  }
119 
120  protected:
121  std::ostream& out;
122  };
123 }
124 
125 #endif
std::istream & in
Definition: JStreamIO.hh:73
Interface for binary output.
virtual int read(char *buffer, const int length) override
Read byte array.
Definition: JStreamIO.hh:65
virtual void clear() override
Clear status of reader.
Definition: JStreamIO.hh:52
virtual int write(const char *buffer, const int length) override
Write byte array.
Definition: JStreamIO.hh:113
Binary input based on std::istream.
Definition: JStreamIO.hh:24
Interface for binary input.
std::ostream & out
Definition: JStreamIO.hh:121
virtual bool getStatus() const override
Status of writer.
Definition: JStreamIO.hh:100
JStreamWriter(std::ostream &__out)
Constructor.
Definition: JStreamIO.hh:90
virtual bool getStatus() const override
Status of reader.
Definition: JStreamIO.hh:43
Binary output based on std::ostream.
Definition: JStreamIO.hh:81
JStreamReader(std::istream &__in)
Constructor.
Definition: JStreamIO.hh:33