Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JSerialisable.hh
Go to the documentation of this file.
1 #ifndef __JIO__JSERIALISABLE__
2 #define __JIO__JSERIALISABLE__
3 
4 #include "JLang/JBinaryIO.hh"
6 
7 
8 /**
9  * \author mdejong
10  */
11 
12 /**
13  * Auxiliary classes and methods for binary I/O.
14  */
15 namespace JIO {}
16 namespace JPP { using namespace JIO; }
17 
18 namespace JIO {
19 
20  using JLANG::JBinaryInput;
23 
24  class JReader; //!< Forward declaration of binary input.
25  class JWriter; //!< Forward declaration of binary output.
26 
27 
28  /**
29  * Interface class for a data structure with binary I/O.
30  */
31  class JSerialisable {
32  public:
33  /**
34  * Virtual destructor.
35  */
36  virtual ~JSerialisable()
37  {}
38 
39 
40  /**
41  * Read from input.
42  *
43  * \param in JReader
44  * \return JReader
45  */
46  virtual JReader& read(JReader& in) = 0;
47 
48 
49  /**
50  * Write to output.
51  *
52  * \param out JWriter
53  * \return JWriter
54  */
55  virtual JWriter& write(JWriter& out) const = 0;
56  };
57 
58 
59  /**
60  * Interface for binary input.
61  */
62  class JReader :
63  public JLANG::JBinaryInput,
65  {
66  public:
67  /**
68  * Clear status of reader.
69  */
70  virtual void clear()
71  {}
72 
73 
74  /**
75  * Read serialisable data object.
76  *
77  * \param object serialisable data object
78  * \return JReader
79  */
81  {
82  return object.read(*this);
83  }
84 
85 
86  JReader& operator>>(bool& value) { read((char*) &value, sizeof(bool)); return *this; }
87  JReader& operator>>(char& value) { read((char*) &value, sizeof(char)); return *this; }
88  JReader& operator>>(unsigned char& value) { read((char*) &value, sizeof(unsigned char)); return *this; }
89  JReader& operator>>(short& value) { read((char*) &value, sizeof(short)); return *this; }
90  JReader& operator>>(unsigned short& value) { read((char*) &value, sizeof(unsigned short)); return *this; }
91  JReader& operator>>(int& value) { read((char*) &value, sizeof(int)); return *this; }
92  JReader& operator>>(unsigned int& value) { read((char*) &value, sizeof(unsigned int)); return *this; }
93  JReader& operator>>(long int& value) { read((char*) &value, sizeof(long int)); return *this; }
94  JReader& operator>>(unsigned long int& value) { read((char*) &value, sizeof(unsigned long int)); return *this; }
95  JReader& operator>>(long long int& value) { read((char*) &value, sizeof(long long int)); return *this; }
96  JReader& operator>>(unsigned long long int& value) { read((char*) &value, sizeof(unsigned long long int)); return *this; }
97  JReader& operator>>(float& value) { read((char*) &value, sizeof(float)); return *this; }
98  JReader& operator>>(double& value) { read((char*) &value, sizeof(double)); return *this; }
99  JReader& operator>>(long double& value) { read((char*) &value, sizeof(long double)); return *this; }
100 
101 
102  /**
103  * Read object.
104  *
105  * \param object object
106  * \return this reader
107  */
108  inline JReader& load(JSerialisable& object)
109  {
110  return object.read(*this);
111  }
112 
113 
114  /**
115  * Read object.
116  *
117  * \param object object
118  * \return this reader
119  */
120  template<class T>
121  inline JReader& load(T& object)
122  {
123  return *this >> object;
124  }
125  };
126 
127 
128  /**
129  * Interface for binary output.
130  */
131  class JWriter :
132  public JLANG::JBinaryOutput,
134  {
135  public:
136  /**
137  * Write serialisable data object.
138  *
139  * \param object serialisable data object
140  * \return JWriter
141  */
143  {
144  return object.write(*this);
145  }
146 
147 
148  JWriter& operator<<(const bool& value) { write((const char*) &value, sizeof(bool)); return *this; }
149  JWriter& operator<<(const char& value) { write((const char*) &value, sizeof(char)); return *this; }
150  JWriter& operator<<(const unsigned char& value) { write((const char*) &value, sizeof(unsigned char)); return *this; }
151  JWriter& operator<<(const short& value) { write((const char*) &value, sizeof(short)); return *this; }
152  JWriter& operator<<(const unsigned short& value) { write((const char*) &value, sizeof(unsigned short)); return *this; }
153  JWriter& operator<<(const int& value) { write((const char*) &value, sizeof(int)); return *this; }
154  JWriter& operator<<(const unsigned int& value) { write((const char*) &value, sizeof(unsigned int)); return *this; }
155  JWriter& operator<<(const long int& value) { write((const char*) &value, sizeof(long int)); return *this; }
156  JWriter& operator<<(const unsigned long int& value) { write((const char*) &value, sizeof(unsigned long int)); return *this; }
157  JWriter& operator<<(const long long int& value) { write((const char*) &value, sizeof(long long int)); return *this; }
158  JWriter& operator<<(const unsigned long long int& value) { write((const char*) &value, sizeof(unsigned long long int)); return *this; }
159  JWriter& operator<<(const float& value) { write((const char*) &value, sizeof(float)); return *this; }
160  JWriter& operator<<(const double& value) { write((const char*) &value, sizeof(double)); return *this; }
161  JWriter& operator<<(const long double& value) { write((const char*) &value, sizeof(long double)); return *this; }
162 
163 
164  /**
165  * Write object.
166  *
167  * \param object object
168  * \return this writer
169  */
170  inline JWriter& store(const JSerialisable& object)
171  {
172  return object.write(*this);
173  }
174 
175 
176  /**
177  * Write object.
178  *
179  * \param object object
180  * \return this writer
181  */
182  template<class T>
183  inline JWriter& store(const T& object)
184  {
185  return *this << object;
186  }
187  };
188 }
189 
190 #endif
Interface for binary input.
Definition: JBinaryIO.hh:18
JReader & operator>>(short &value)
Interface for binary output.
JWriter & store(const JSerialisable &object)
Write object.
JReader & operator>>(long long int &value)
JReader & operator>>(unsigned int &value)
JReader & operator>>(char &value)
JWriter & operator<<(const unsigned long int &value)
JReader & operator>>(float &value)
JWriter & operator<<(const unsigned long long int &value)
virtual JWriter & write(JWriter &out) const =0
Write to output.
esac print_variable DETECTOR INPUT_FILE OUTPUT_FILE CDF for TYPE in
Definition: JSirene.sh:45
virtual int read(char *buffer, const int length)=0
Read byte array.
virtual int write(const char *buffer, const int length)=0
Write byte array.
JWriter & operator<<(const long long int &value)
JReader & operator>>(unsigned long long int &value)
JWriter & operator<<(const bool &value)
JWriter & operator<<(const char &value)
JReader & operator>>(unsigned char &value)
Forward declaration of binary output.
JReader & load(JSerialisable &object)
Read object.
virtual JReader & read(JReader &in)=0
Read from input.
JWriter & operator<<(const double &value)
JWriter & operator<<(const unsigned char &value)
JReader & operator>>(unsigned long int &value)
do set_variable OUTPUT_DIRECTORY $WORKDIR T
JReader & operator>>(long double &value)
Interface for binary input.
JReader & operator>>(long int &value)
JReader & operator>>(JSerialisable &object)
Read serialisable data object.
JWriter & operator<<(const long int &value)
JWriter & operator<<(const long double &value)
JReader & operator>>(unsigned short &value)
JReader & operator>>(int &value)
JReader & load(T &object)
Read object.
JWriter & operator<<(const int &value)
Interface for status of object.
JWriter & operator<<(const unsigned short &value)
JWriter & operator<<(const short &value)
virtual ~JSerialisable()
Virtual destructor.
JWriter & operator<<(const float &value)
JWriter & operator<<(const JSerialisable &object)
Write serialisable data object.
JWriter & operator<<(const unsigned int &value)
JReader & operator>>(double &value)
virtual void clear()
Clear status of reader.
JReader & operator>>(bool &value)
Interface for binary output.
Definition: JBinaryIO.hh:41
JWriter & store(const T &object)
Write object.