Gorgon Game Engine
Stream.h
Go to the documentation of this file.
1 #pragma once
2 #include <stdint.h>
3 #include <iostream>
4 #include <vector>
5 #include "../Types.h"
6 #include "../SGuid.h"
7 #include "../Geometry/Point.h"
8 #include "../Geometry/Size.h"
9 #include "../Graphics/Color.h"
10 
13 namespace Gorgon { namespace IO {
14 
16  template<class E_>
17  inline E_ ReadEnum32(std::istream &stream) {
18  int32_t r;
19  stream.read(reinterpret_cast<char*>(&r), sizeof(int32_t));
20 
21  return E_(r);
22  }
23 
26  inline long ReadInt32(std::istream &stream) {
27  int32_t r;
28  stream.read(reinterpret_cast<char*>(&r), sizeof(int32_t));
29 
30  return r;
31  }
32 
35  inline unsigned long ReadUInt32(std::istream &stream) {
36  uint32_t r;
37  stream.read(reinterpret_cast<char*>(&r), sizeof(uint32_t));
38 
39  return r;
40  }
41 
44  inline int ReadInt16(std::istream &stream) {
45  int16_t r;
46  stream.read(reinterpret_cast<char*>(&r), sizeof(int16_t));
47 
48  return r;
49  }
50 
53  inline unsigned ReadUInt16(std::istream &stream) {
54  uint16_t r;
55  stream.read(reinterpret_cast<char*>(&r), sizeof(uint16_t));
56 
57  return r;
58  }
59 
62  inline char ReadInt8(std::istream &stream) {
63  int8_t r;
64  stream.read(reinterpret_cast<char*>(&r), sizeof(int8_t));
65 
66  return r;
67  }
68 
71  inline Byte ReadUInt8(std::istream &stream) {
72  uint8_t r;
73  stream.read(reinterpret_cast<char*>(&r), sizeof(uint8_t));
74 
75  return r;
76  }
77 
80  inline float ReadFloat(std::istream &stream) {
81  static_assert(sizeof(float) == 4, "Current implementation only supports 32bit floats");
82 
83  float r;
84  // cppcheck-suppress invalidPointerCast
85  //this is ok due to requirement of standard float and double is necessary for Gorgon library
86  stream.read(reinterpret_cast<char*>(&r), 4);
87 
88  return r;
89  }
90 
93  inline double ReadDouble(std::istream &stream) {
94  static_assert(sizeof(double) == 8, "Current implementation only supports 64bit floats");
95 
96  float r;
97  // cppcheck-suppress invalidPointerCast
98  //this is ok due to requirement of standard float and double is necessary for Gorgon library
99  stream.read(reinterpret_cast<char*>(&r), 4);
100 
101  return r;
102  }
103 
105  inline bool ReadBool(std::istream &stream) {
106  return ReadInt32(stream) != 0;
107  }
108 
110  inline Graphics::RGBA ReadRGBA(std::istream &stream) {
111  Graphics::RGBA color;
112 
113  color.R = ReadUInt8(stream);
114  color.G = ReadUInt8(stream);
115  color.B = ReadUInt8(stream);
116  color.A = ReadUInt8(stream);
117 
118  return color;
119  }
120 
122  inline Graphics::RGBAf ReadRGBAf(std::istream &stream) {
123  Graphics::RGBAf color;
124 
125  color.R = ReadFloat(stream);
126  color.G = ReadFloat(stream);
127  color.B = ReadFloat(stream);
128  color.A = ReadFloat(stream);
129 
130  return color;
131  }
132 
135  inline std::string ReadString(std::istream &stream) {
136  unsigned long size;
137  std::string ret;
138  size = ReadUInt32(stream);
139  ret.resize(size);
140  stream.read(&ret[0], size);
141 
142  return ret;
143  }
144 
146  inline std::string ReadString(std::istream &stream, unsigned long size) {
147  std::string ret;
148  ret.resize(size);
149  stream.read(&ret[0], size);
150  return ret;
151  }
152 
157  template<class T_>
158  inline void ReadArray(std::istream &stream, T_ *data, unsigned long size) {
159  stream.read((char*)data, size*sizeof(T_));
160  }
161 
163  inline SGuid ReadGuid(std::istream &stream) {
164  return SGuid(stream);
165  }
166 
167  inline Geometry::Point ReadPoint(std::istream &stream) {
168  auto x = ReadInt32(stream);
169  auto y = ReadInt32(stream);
170 
171  return {(int)x, (int)y};
172  }
173 
174  inline Geometry::Pointf ReadPointf(std::istream &stream) {
175  auto x = ReadFloat(stream);
176  auto y = ReadFloat(stream);
177 
178  return {x, y};
179  }
180 
181  inline Geometry::Size ReadSize(std::istream &stream) {
182  auto w = ReadInt32(stream);
183  auto h = ReadInt32(stream);
184 
185  return {(int)w, (int)h};
186  }
187 
188 
190  template<class E_>
191  inline void WriteEnum32(std::ostream &stream, E_ v) {
192  int32_t r = (int32_t)(v);
193  stream.write(reinterpret_cast<char*>(&r), sizeof(int32_t));
194  }
195 
197  inline void WriteInt32(std::ostream &stream, long value) {
198  int32_t r = value;
199  stream.write(reinterpret_cast<const char*>(&r), sizeof(int32_t));
200  }
201 
204  inline void WriteUInt32(std::ostream &stream, unsigned long value) {
205  uint32_t r = value;
206  stream.write(reinterpret_cast<const char*>(&r), sizeof(uint32_t));
207  }
208 
211  inline void WriteInt16(std::ostream &stream, int value) {
212  int16_t r = value;
213  stream.write(reinterpret_cast<const char*>(&r), sizeof(int16_t));
214  }
215 
218  inline void WriteUInt16(std::ostream &stream, unsigned value) {
219  uint16_t r = value;
220  stream.write(reinterpret_cast<const char*>(&r), sizeof(uint16_t));
221  }
222 
225  inline void WriteInt8(std::ostream &stream, char value) {
226  stream.write(reinterpret_cast<const char*>(&value), sizeof(int8_t));
227  }
228 
231  inline void WriteUInt8(std::ostream &stream, Byte value) {
232  stream.write(reinterpret_cast<const char*>(&value), sizeof(uint8_t));
233  }
234 
237  inline void WriteFloat(std::ostream &stream, float value) {
238  static_assert(sizeof(float) == 4, "Current implementation only supports 32bit floats");
239 
240  // cppcheck-suppress invalidPointerCast
241  //this is ok due to requirement of standard float and double is necessary for Gorgon library
242  stream.write(reinterpret_cast<const char*>(&value), 4);
243  }
244 
247  inline void WriteDouble(std::ostream &stream, double value) {
248  static_assert(sizeof(double) == 8, "Current implementation only supports 64bit floats");
249 
250  // cppcheck-suppress invalidPointerCast
251  //this is ok due to requirement of standard float and double is necessary for Gorgon library
252  stream.write(reinterpret_cast<const char*>(&value), 4);
253  }
254 
256  inline void WriteBool(std::ostream &stream, bool value) {
257  WriteInt32(stream, value);
258  }
259 
261  inline void WriteRGBA(std::ostream &stream, Graphics::RGBA value) {
262  WriteUInt8(stream, value.R);
263  WriteUInt8(stream, value.G);
264  WriteUInt8(stream, value.B);
265  WriteUInt8(stream, value.A);
266  }
267 
269  inline void WriteRGBAf(std::ostream &stream, Graphics::RGBAf value) {
270  WriteFloat(stream, value.R);
271  WriteFloat(stream, value.G);
272  WriteFloat(stream, value.B);
273  WriteFloat(stream, value.A);
274  }
275 
278  inline void WriteStringWithSize(std::ostream &stream, const std::string &value) {
279  WriteUInt32(stream, (unsigned long)value.size());
280 
281  stream.write(&value[0], value.size());
282  }
283 
285  inline void WriteString(std::ostream &stream, const std::string &value) {
286  stream.write(&value[0], value.size());
287  }
288 
293  template<class T_>
294  inline void WriteArray(std::ostream &stream, const T_ *data, unsigned long size) {
295  stream.write((const char*)data, size*sizeof(T_));
296  }
297 
298 
301  template<class T_>
302  inline void WriteVector(std::ostream &stream, const std::vector<T_> &data) {
303  stream.write((const char*)&data[0], data.size()*sizeof(T_));
304  }
305 
306 
308  inline void WriteGuid(std::ostream &stream, const SGuid &value) {
309  WriteArray(stream, value.Bytes, 8);
310  }
311 
313  inline void WritePoint(std::ostream &stream, Geometry::Point p) {
314  WriteInt32(stream, p.X);
315  WriteInt32(stream, p.Y);
316  }
317 
319  template<class F_ = Float>
320  typename std::enable_if<std::is_same<F_, float>::value, void>::type
321  WritePointf(std::ostream &stream, Geometry::Pointf p) {
322  WriteFloat(stream, p.X);
323  WriteFloat(stream, p.Y);
324  }
325 
327  inline void WriteSize(std::ostream &stream, Geometry::Size s) {
328  WriteInt32(stream, s.Width);
329  WriteInt32(stream, s.Height);
330  }
331 } }
332 
333 
335 inline long operator +(const std::streampos &l, long r) {
336  return (long)l + r;
337 }
338 
340 inline long operator +(const std::streampos &l, int r) {
341  return (long)l + r;
342 }
343 
345 inline unsigned long operator +(const std::streampos &l, unsigned long r) {
346  return (unsigned long)l + r;
347 }
348 
350 inline long operator -(const std::streampos &l, unsigned long r) {
351  return (long)l - r;
352 }
353 
355 inline long operator -(const std::streampos &l, long r) {
356  return (long)l - r;
357 }
358 
360 inline long operator -(const std::streampos &l, int r) {
361  return (unsigned long)l - r;
362 }
Gorgon::IO::WriteInt16
void WriteInt16(std::ostream &stream, int value)
Writes a 16-bit integer from the stream.
Definition: Stream.h:211
Gorgon::Graphics::RGBA
This class represents a color information.
Definition: Color.h:91
Gorgon::IO::ReadRGBA
Graphics::RGBA ReadRGBA(std::istream &stream)
Reads a RGBA color, R will be read first.
Definition: Stream.h:110
Gorgon::IO::WriteInt32
void WriteInt32(std::ostream &stream, long value)
Writes a 32-bit integer to the stream.
Definition: Stream.h:197
Gorgon::IO::ReadInt8
char ReadInt8(std::istream &stream)
Reads an 8-bit integer from the stream.
Definition: Stream.h:62
Gorgon::IO::WriteBool
void WriteBool(std::ostream &stream, bool value)
Writes a boolean value. In resource 1.0, booleans are stored as 32bit integers.
Definition: Stream.h:256
Gorgon::Geometry::basic_Point::X
T_ X
X coordinate.
Definition: Point.h:368
Gorgon::IO::WritePointf
std::enable_if< std::is_same< F_, float >::value, void >::type WritePointf(std::ostream &stream, Geometry::Pointf p)
Saves the point as two consecutive floats. This function will not work if float type is double.
Definition: Stream.h:321
Gorgon::IO::WriteUInt16
void WriteUInt16(std::ostream &stream, unsigned value)
Writes a 16-bit unsigned integer from the stream.
Definition: Stream.h:218
Gorgon::Geometry::basic_Size::Height
T_ Height
Height of this size object.
Definition: Size.h:261
Gorgon::IO::WriteEnum32
void WriteEnum32(std::ostream &stream, E_ v)
Writes an enumeration as a 32-bit integer.
Definition: Stream.h:191
Gorgon::Graphics::RGBAf
Represents a four channel 32 bit float per channel color information.
Definition: Color.h:373
Gorgon::IO::WriteArray
void WriteArray(std::ostream &stream, const T_ *data, unsigned long size)
Writes an array to the stream.
Definition: Stream.h:294
Gorgon::Graphics::RGBA::A
Byte A
Alpha channel.
Definition: Color.h:312
Gorgon::IO::ReadRGBAf
Graphics::RGBAf ReadRGBAf(std::istream &stream)
Reads a RGBAf color, R will be read first.
Definition: Stream.h:122
Gorgon::IO::WriteVector
void WriteVector(std::ostream &stream, const std::vector< T_ > &data)
Writes a vector to the stream.
Definition: Stream.h:302
Gorgon::IO::ReadFloat
float ReadFloat(std::istream &stream)
Reads a 32 bit IEEE floating point number from the stream.
Definition: Stream.h:80
Gorgon::IO::WriteRGBA
void WriteRGBA(std::ostream &stream, Graphics::RGBA value)
Writes a RGBA color, R will be saved first.
Definition: Stream.h:261
Gorgon::IO::ReadInt32
long ReadInt32(std::istream &stream)
Reads a 32-bit integer from the stream.
Definition: Stream.h:26
Gorgon
Root namespace for Gorgon Game Engine.
Definition: Any.h:19
Gorgon::Geometry::basic_Size
This class represents a 2D geometric size.
Definition: Size.h:23
Gorgon::Graphics::RGBAf::R
float R
Red channel.
Definition: Color.h:588
Gorgon::IO::WriteUInt32
void WriteUInt32(std::ostream &stream, unsigned long value)
Writes a 32-bit unsigned integer from the stream.
Definition: Stream.h:204
Gorgon::IO::ReadEnum32
E_ ReadEnum32(std::istream &stream)
Reads an enumeration that is saved as 32bit integer.
Definition: Stream.h:17
Gorgon::IO::ReadBool
bool ReadBool(std::istream &stream)
Reads a boolean value. In resource 1.0, booleans are stored as 32bit integers.
Definition: Stream.h:105
Gorgon::SGuid::Bytes
Byte Bytes[8]
Allows byte-by-byte addressing of the guid.
Definition: SGuid.h:27
Gorgon::IO::ReadInt16
int ReadInt16(std::istream &stream)
Reads a 16-bit integer from the stream.
Definition: Stream.h:44
Gorgon::IO::ReadUInt32
unsigned long ReadUInt32(std::istream &stream)
Reads a 32-bit unsigned integer from the stream.
Definition: Stream.h:35
Gorgon::Resource::GID::SGuid
constexpr Type SGuid
Identifies resources.
Definition: GID.h:115
Gorgon::IO::ReadUInt16
unsigned ReadUInt16(std::istream &stream)
Reads a 16-bit unsigned integer from the stream.
Definition: Stream.h:53
Gorgon::Graphics::RGBAf::G
float G
Green channel.
Definition: Color.h:591
operator+
long operator+(const std::streampos &l, long r)
Adds an integer to streampos.
Definition: Stream.h:335
Gorgon::IO::WriteUInt8
void WriteUInt8(std::ostream &stream, Byte value)
Writes an 8-bit unsigned integer from the stream.
Definition: Stream.h:231
Gorgon::IO::WriteSize
void WriteSize(std::ostream &stream, Geometry::Size s)
Saves the size as two consecutive 32bit integers.
Definition: Stream.h:327
Gorgon::Graphics::RGBA::B
Byte B
Blue channel.
Definition: Color.h:309
Gorgon::Geometry::basic_Point
This class represents a 2D point.
Definition: Point.h:32
Gorgon::Graphics::RGBAf::B
float B
Blue channel.
Definition: Color.h:594
Gorgon::Graphics::RGBA::G
Byte G
Green channel.
Definition: Color.h:306
Gorgon::Byte
unsigned char Byte
Represents smallest cell in memory.
Definition: Types.h:9
Gorgon::IO::WriteInt8
void WriteInt8(std::ostream &stream, char value)
Writes an 8-bit integer from the stream.
Definition: Stream.h:225
Gorgon::IO::ReadPointf
Geometry::Pointf ReadPointf(std::istream &stream)
Definition: Stream.h:174
Gorgon::Geometry::basic_Point::Y
T_ Y
Y coordinate.
Definition: Point.h:371
Gorgon::Geometry::basic_Size::Width
T_ Width
Width of this size object.
Definition: Size.h:258
Gorgon::IO::WriteDouble
void WriteDouble(std::ostream &stream, double value)
Writes a 64 bit IEEE double precision floating point number from the stream.
Definition: Stream.h:247
Gorgon::Graphics::RGBA::R
Byte R
Red channel.
Definition: Color.h:303
Gorgon::SGuid
This class represents a short globally unique identifier.
Definition: SGuid.h:22
Gorgon::IO::ReadSize
Geometry::Size ReadSize(std::istream &stream)
Definition: Stream.h:181
Gorgon::Graphics::RGBAf::A
float A
Alpha channel.
Definition: Color.h:597
Gorgon::IO::WritePoint
void WritePoint(std::ostream &stream, Geometry::Point p)
Saves the point as two consecutive 32bit integers.
Definition: Stream.h:313
Gorgon::IO::ReadGuid
SGuid ReadGuid(std::istream &stream)
Reads a GUID from the given stream.
Definition: Stream.h:163
Gorgon::IO::WriteGuid
void WriteGuid(std::ostream &stream, const SGuid &value)
Writes a GUID from the given stream.
Definition: Stream.h:308
Gorgon::IO::ReadDouble
double ReadDouble(std::istream &stream)
Reads a 64 bit IEEE double precision floating point number from the stream.
Definition: Stream.h:93
Gorgon::IO::ReadPoint
Geometry::Point ReadPoint(std::istream &stream)
Definition: Stream.h:167
Gorgon::IO::WriteFloat
void WriteFloat(std::ostream &stream, float value)
Writes a 32 bit IEEE floating point number from the stream.
Definition: Stream.h:237
Gorgon::IO::ReadArray
void ReadArray(std::istream &stream, T_ *data, unsigned long size)
Reads an array from the stream.
Definition: Stream.h:158
operator-
long operator-(const std::streampos &l, unsigned long r)
Subtracts an integer from streampos.
Definition: Stream.h:350
Gorgon::IO::WriteString
void WriteString(std::ostream &stream, const std::string &value)
Writes a string without its size.
Definition: Stream.h:285
Gorgon::IO::ReadUInt8
Byte ReadUInt8(std::istream &stream)
Reads an 8-bit unsigned integer from the stream.
Definition: Stream.h:71
Gorgon::IO::WriteStringWithSize
void WriteStringWithSize(std::ostream &stream, const std::string &value)
Writes a string from a given stream.
Definition: Stream.h:278
Gorgon::IO::ReadString
std::string ReadString(std::istream &stream)
Reads a string from a given stream.
Definition: Stream.h:135
Gorgon::IO::WriteRGBAf
void WriteRGBAf(std::ostream &stream, Graphics::RGBAf value)
Writes a RGBAf color, R will be saved first.
Definition: Stream.h:269