PByteStream.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015-2018 Digital Barriers plc. All rights reserved.
3  * Contact: http://www.digitalbarriers.com/
4  *
5  * This file is part of the Papillon SDK.
6  *
7  * You can't use, modify or distribute any part of this file without
8  * the explicit written agreements of Digital Barriers.
9  */
10 
15 #pragma once
16 #include <PObject.h>
17 #include <PResult.h>
18 
19 PAPILLON_BEGIN_NAMESPACE
20 
21 class PByteArray;
22 
69 class PAPILLON_API_CORE PByteStream : public PObject
70 {
71 public:
75  PByteStream();
76 
80  PByteStream(const PByteStream& other);
81 
85  virtual ~PByteStream();
86 
90  PByteStream& operator=(const PByteStream& other);
91 
95  bool operator==(const PByteStream& other) const;
96 
101  PResult IsOpened() const;
102 
107  PResult Flush();
108 
113  PResult Close();
114 
118  bool CanSeek() const;
119 
124  papillon::int64 GetPosition() const;
125 
129  PResult Seek(papillon::uint64 n);
130 
134  PResult Rewind();
135 
139  bool HasSize() const;
140 
144  papillon::int64 Size() const;
145 
150  PResult Resize(papillon::uint64 newSize);
151 
155  bool CanRead() const;
156 
162  PResult ReadByte(papillon::int8& value);
163 
170  PResult ReadBytes(void* destinationBuffer, papillon::uint64 n);
171 
178  PResult ReadBytes(PByteArray& destinationArray, papillon::uint64 position, papillon::uint64 n);
179 
188  template <typename T>
190  {
191  return t.Read(*this);
192  }
193 
202  template <typename T>
204  {
205  if (ReadObject(t).LogErrorIfAny2(PAPILLON_SRC_POS).Failed())
206  throw std::exception();
207  return *this;
208  }
209 
213  bool CanWrite() const;
214 
219  PResult WriteByte(papillon::int8 value);
220 
225  PResult WriteBytes(const void* sourceBuffer, papillon::uint64 n);
226 
233  PResult WriteBytes(const PByteArray& sourceArray, papillon::uint64 sourcePosition, papillon::uint64 numBytes = 0);
234 
243  template <typename T>
244  PResult WriteObject(const T& t)
245  {
246  return t.Write(*this);
247  }
248 
257  template <typename T>
258  PByteStream& operator<<(const T& t)
259  {
260  if (WriteObject(t).LogErrorIfAny2(PAPILLON_SRC_POS).Failed())
261  throw std::exception();
262  return *this;
263  }
264 
265  static PResult ReadClassIdAndCheck(PByteStream& is, classid expectedClassId);
266 
267  private:
268  PResult ReadBytesWithoutChecks(void* destinationBuffer, papillon::uint64 n);
269 };
270 
271 template <> inline PResult PByteStream::ReadObject(bool& value) { return ReadBytesWithoutChecks(&value, sizeof(int8 )); }
272 template <> inline PResult PByteStream::ReadObject(papillon::int8& value) { return ReadBytesWithoutChecks(&value, sizeof(int8 )); }
273 template <> inline PResult PByteStream::ReadObject(papillon::int16& value) { return ReadBytesWithoutChecks(&value, sizeof(int16 )); }
274 template <> inline PResult PByteStream::ReadObject(papillon::int32& value) { return ReadBytesWithoutChecks(&value, sizeof(int32 )); }
275 template <> inline PResult PByteStream::ReadObject(papillon::int64& value) { return ReadBytesWithoutChecks(&value, sizeof(int64 )); }
276 template <> inline PResult PByteStream::ReadObject(papillon::uint8& value) { return ReadBytesWithoutChecks(&value, sizeof(uint8 )); }
277 template <> inline PResult PByteStream::ReadObject(papillon::uint16& value) { return ReadBytesWithoutChecks(&value, sizeof(uint16)); }
278 template <> inline PResult PByteStream::ReadObject(papillon::uint32& value) { return ReadBytesWithoutChecks(&value, sizeof(uint32)); }
279 template <> inline PResult PByteStream::ReadObject(papillon::uint64& value) { return ReadBytesWithoutChecks(&value, sizeof(uint64)); }
280 template <> inline PResult PByteStream::ReadObject(float& value) { return ReadBytesWithoutChecks(&value, sizeof(float )); }
281 template <> inline PResult PByteStream::ReadObject(double& value) { return ReadBytesWithoutChecks(&value, sizeof(double)); }
282 
283 template <> inline PResult PByteStream::WriteObject(const bool& value) { return WriteBytes(&value, sizeof(bool )); }
284 template <> inline PResult PByteStream::WriteObject(const papillon::int8& value) { return WriteBytes(&value, sizeof(int8 )); }
285 template <> inline PResult PByteStream::WriteObject(const papillon::int16& value) { return WriteBytes(&value, sizeof(int16 )); }
286 template <> inline PResult PByteStream::WriteObject(const papillon::int32& value) { return WriteBytes(&value, sizeof(int32 )); }
287 template <> inline PResult PByteStream::WriteObject(const papillon::int64& value) { return WriteBytes(&value, sizeof(int64 )); }
288 template <> inline PResult PByteStream::WriteObject(const papillon::uint8& value) { return WriteBytes(&value, sizeof(uint8 )); }
289 template <> inline PResult PByteStream::WriteObject(const papillon::uint16& value){ return WriteBytes(&value, sizeof(uint16)); }
290 template <> inline PResult PByteStream::WriteObject(const papillon::uint32& value){ return WriteBytes(&value, sizeof(uint32)); }
291 template <> inline PResult PByteStream::WriteObject(const papillon::uint64& value){ return WriteBytes(&value, sizeof(uint64)); }
292 template <> inline PResult PByteStream::WriteObject(const float& value) { return WriteBytes(&value, sizeof(float )); }
293 template <> inline PResult PByteStream::WriteObject(const double& value) { return WriteBytes(&value, sizeof(double)); }
294 
295 PAPILLON_END_NAMESPACE
296 
PResult Read(PByteStream &is, papillon::ESerialisationFormat format=papillon::E_BINARY_FORMAT)
Reads this object from the bytes stream retrieved from the specified input stream (deserialisation)...
PResult Write(PByteStream &os, ESerialisationFormat format=papillon::E_BINARY_FORMAT) const
Writes this object to the specified output stream (serialisation).
The PResult class represents the result returned by most of Papillon functions; this class is used to...
Definition: PResult.h:159
PObject ReadObject(PByteStream &is, PResult &result) const
Builds a PObject from a byte stream (deserialisation).
The PByteArray class represents a resizable array of bytes.
Definition: PByteArray.h:40
PResult ReadObject(T &t)
Deserialises the specified object from this stream.
Definition: PByteStream.h:189
The PObject class is the root class of all Papillon classes; all PObject behaves like shared referenc...
Definition: PObject.h:27
The PByteStream class is a generic byte stream (can be a memory stream or a file stream).
Definition: PByteStream.h:69
PResult WriteObject(const T &t)
Serialises the specified object to this stream.
Definition: PByteStream.h:244
Class PResult: a result returned by most of Papillon functions; this class is used to replace classic...
Class PObject: the root class of all Papillon classes.
PObject & operator=(const PObject &obj)
Performs a shared copy of other to this object.
Definition: PObject.h:45
PAPILLON_BEGIN_NAMESPACE typedef uint16_t classid
Definition: PRtti.h:21
PByteStream & operator<<(const T &t)
Serialises the specified object to this stream; throw an exception if failure.
Definition: PByteStream.h:258
PByteStream & operator>>(T &t)
Deserialises the specified object from this stream.
Definition: PByteStream.h:203