C++ Utilities  4.12.1
Useful C++ classes and routines such as argument parser, IO and conversion utilities
binarywriter.h
Go to the documentation of this file.
1 #ifndef IOUTILITIES_BINARYWRITER_H
2 #define IOUTILITIES_BINARYWRITER_H
3 
4 #include "../conversion/binaryconversion.h"
5 #include "../conversion/types.h"
6 
7 #include <ostream>
8 #include <string>
9 #include <vector>
10 
11 namespace IoUtilities {
12 
14 public:
15  BinaryWriter(std::ostream *stream);
16  BinaryWriter(const BinaryWriter &other);
17  BinaryWriter &operator=(const BinaryWriter &rhs) = delete;
18  ~BinaryWriter();
19 
20  const std::ostream *stream() const;
21  std::ostream *stream();
22  void setStream(std::ostream *stream, bool giveOwnership = false);
23  bool hasOwnership() const;
24  void giveOwnership();
25  void detatchOwnership();
26  void flush();
27  bool fail() const;
28  void write(const char *buffer, std::streamsize length);
29  void write(const std::vector<char> &buffer, std::streamsize length);
30  void writeChar(char value);
31  void writeByte(byte value);
32  void writeInt16BE(int16 value);
33  void writeUInt16BE(uint16 value);
34  void writeInt24BE(int32 value);
35  void writeUInt24BE(uint32 value);
36  void writeInt32BE(int32 value);
37  void writeUInt32BE(uint32 value);
38  void writeInt40BE(int64 value);
39  void writeUInt40BE(uint64 value);
40  void writeInt56BE(int64 value);
41  void writeUInt56BE(uint64 value);
42  void writeInt64BE(int64 value);
43  void writeUInt64BE(uint64 value);
44  void writeFloat32BE(float32 value);
45  void writeFloat64BE(float64 value);
46  void writeInt16LE(int16 value);
47  void writeUInt16LE(uint16 value);
48  void writeInt24LE(int32 value);
49  void writeUInt24LE(uint32 value);
50  void writeInt32LE(int32 value);
51  void writeUInt32LE(uint32 value);
52  void writeInt40LE(int64 value);
53  void writeUInt40LE(uint64 value);
54  void writeInt56LE(int64 value);
55  void writeUInt56LE(uint64 value);
56  void writeInt64LE(int64 value);
57  void writeUInt64LE(uint64 value);
58  void writeFloat32LE(float32 value);
59  void writeFloat64LE(float64 value);
60  void writeString(const std::string &value);
61  void writeTerminatedString(const std::string &value);
62  void writeLengthPrefixedString(const std::string &value);
63  void writeBool(bool value);
64  void writeSynchsafeUInt32BE(uint32 valueToConvertAndWrite);
65  void writeFixed8BE(float32 valueToConvertAndWrite);
66  void writeFixed16BE(float32 valueToConvertAndWrite);
67  void writeSynchsafeUInt32LE(uint32 valueToConvertAndWrite);
68  void writeFixed8LE(float32 valueToConvertAndWrite);
69  void writeFixed16LE(float32 valueToConvertAndWrite);
70 
71 private:
72  std::ostream *m_stream;
73  bool m_ownership;
74  char m_buffer[8];
75 };
76 
82 inline std::ostream *BinaryWriter::stream()
83 {
84  return m_stream;
85 }
86 
92 inline const std::ostream *BinaryWriter::stream() const
93 {
94  return m_stream;
95 }
96 
104 inline bool BinaryWriter::hasOwnership() const
105 {
106  return m_ownership;
107 }
108 
117 {
118  if (m_stream) {
119  m_ownership = true;
120  }
121 }
122 
131 {
132  m_ownership = false;
133 }
134 
138 inline void BinaryWriter::flush()
139 {
140  m_stream->flush();
141 }
142 
146 inline bool BinaryWriter::fail() const
147 {
148  return m_stream ? m_stream->fail() : false;
149 }
150 
154 inline void BinaryWriter::write(const char *buffer, std::streamsize length)
155 {
156  m_stream->write(buffer, length);
157 }
158 
163 inline void BinaryWriter::write(const std::vector<char> &buffer, std::streamsize length)
164 {
165  m_stream->write(buffer.data(), length);
166 }
167 
171 inline void BinaryWriter::writeChar(char value)
172 {
173  m_buffer[0] = value;
174  m_stream->write(m_buffer, 1);
175 }
176 
180 inline void BinaryWriter::writeByte(byte value)
181 {
182  m_buffer[0] = *reinterpret_cast<char *>(&value);
183  m_stream->write(m_buffer, 1);
184 }
185 
189 inline void BinaryWriter::writeBool(bool value)
190 {
191  writeByte(value ? 1 : 0);
192 }
193 
198 {
199  ConversionUtilities::BE::getBytes(value, m_buffer);
200  m_stream->write(m_buffer, sizeof(int16));
201 }
202 
207 {
208  ConversionUtilities::BE::getBytes(value, m_buffer);
209  m_stream->write(m_buffer, sizeof(uint16));
210 }
211 
217 {
218  ConversionUtilities::BE::getBytes(value, m_buffer);
219  m_stream->write(m_buffer + 1, 3);
220 }
221 
227 {
228  // discard most significant byte
229  ConversionUtilities::BE::getBytes(value, m_buffer);
230  m_stream->write(m_buffer + 1, 3);
231 }
232 
237 {
238  ConversionUtilities::BE::getBytes(value, m_buffer);
239  m_stream->write(m_buffer, sizeof(int32));
240 }
241 
246 {
247  ConversionUtilities::BE::getBytes(value, m_buffer);
248  m_stream->write(m_buffer, sizeof(uint32));
249 }
250 
256 {
257  ConversionUtilities::BE::getBytes(value, m_buffer);
258  m_stream->write(m_buffer + 3, 5);
259 }
260 
266 {
267  ConversionUtilities::BE::getBytes(value, m_buffer);
268  m_stream->write(m_buffer + 3, 5);
269 }
270 
276 {
277  ConversionUtilities::BE::getBytes(value, m_buffer);
278  m_stream->write(m_buffer + 1, 7);
279 }
280 
286 {
287  ConversionUtilities::BE::getBytes(value, m_buffer);
288  m_stream->write(m_buffer + 1, 7);
289 }
290 
295 {
296  ConversionUtilities::BE::getBytes(value, m_buffer);
297  m_stream->write(m_buffer, sizeof(int64));
298 }
299 
304 {
305  ConversionUtilities::BE::getBytes(value, m_buffer);
306  m_stream->write(m_buffer, sizeof(uint64));
307 }
308 
312 inline void BinaryWriter::writeFloat32BE(float32 value)
313 {
314  ConversionUtilities::BE::getBytes(value, m_buffer);
315  m_stream->write(m_buffer, sizeof(float32));
316 }
317 
321 inline void BinaryWriter::writeFloat64BE(float64 value)
322 {
323  ConversionUtilities::BE::getBytes(value, m_buffer);
324  m_stream->write(m_buffer, sizeof(float64));
325 }
326 
331 {
332  ConversionUtilities::LE::getBytes(value, m_buffer);
333  m_stream->write(m_buffer, sizeof(int16));
334 }
335 
340 {
341  ConversionUtilities::LE::getBytes(value, m_buffer);
342  m_stream->write(m_buffer, sizeof(uint16));
343 }
344 
350 {
351  // discard most significant byte
352  ConversionUtilities::LE::getBytes(value, m_buffer);
353  m_stream->write(m_buffer, 3);
354 }
355 
361 {
362  // discard most significant byte
363  ConversionUtilities::LE::getBytes(value, m_buffer);
364  m_stream->write(m_buffer, 3);
365 }
366 
371 {
372  ConversionUtilities::LE::getBytes(value, m_buffer);
373  m_stream->write(m_buffer, sizeof(int32));
374 }
375 
380 {
381  ConversionUtilities::LE::getBytes(value, m_buffer);
382  m_stream->write(m_buffer, sizeof(uint32));
383 }
384 
390 {
391  ConversionUtilities::LE::getBytes(value, m_buffer);
392  m_stream->write(m_buffer, 5);
393 }
394 
400 {
401  ConversionUtilities::LE::getBytes(value, m_buffer);
402  m_stream->write(m_buffer, 5);
403 }
404 
410 {
411  ConversionUtilities::LE::getBytes(value, m_buffer);
412  m_stream->write(m_buffer, 7);
413 }
414 
420 {
421  ConversionUtilities::LE::getBytes(value, m_buffer);
422  m_stream->write(m_buffer, 7);
423 }
424 
429 {
430  ConversionUtilities::LE::getBytes(value, m_buffer);
431  m_stream->write(m_buffer, sizeof(int64));
432 }
433 
438 {
439  ConversionUtilities::LE::getBytes(value, m_buffer);
440  m_stream->write(m_buffer, sizeof(uint64));
441 }
442 
446 inline void BinaryWriter::writeFloat32LE(float32 value)
447 {
448  ConversionUtilities::LE::getBytes(value, m_buffer);
449  m_stream->write(m_buffer, sizeof(float32));
450 }
451 
455 inline void BinaryWriter::writeFloat64LE(float64 value)
456 {
457  ConversionUtilities::LE::getBytes(value, m_buffer);
458  m_stream->write(m_buffer, sizeof(float64));
459 }
460 
464 inline void BinaryWriter::writeString(const std::string &value)
465 {
466  m_stream->write(value.c_str(), value.length());
467 }
468 
472 inline void BinaryWriter::writeTerminatedString(const std::string &value)
473 {
474  m_stream->write(value.c_str(), value.length() + 1);
475 }
476 
482 inline void BinaryWriter::writeSynchsafeUInt32BE(uint32 valueToConvertAndWrite)
483 {
484  writeUInt32BE(ConversionUtilities::toSynchsafeInt(valueToConvertAndWrite));
485 }
486 
490 inline void BinaryWriter::writeFixed8BE(float32 valueToConvertAndWrite)
491 {
492  writeUInt16BE(ConversionUtilities::toFixed8(valueToConvertAndWrite));
493 }
494 
498 inline void BinaryWriter::writeFixed16BE(float32 valueToConvertAndWrite)
499 {
500  writeUInt32BE(ConversionUtilities::toFixed16(valueToConvertAndWrite));
501 }
502 
508 inline void BinaryWriter::writeSynchsafeUInt32LE(uint32 valueToConvertAndWrite)
509 {
510  writeUInt32LE(ConversionUtilities::toSynchsafeInt(valueToConvertAndWrite));
511 }
512 
516 inline void BinaryWriter::writeFixed8LE(float32 valueToConvertAndWrite)
517 {
518  writeUInt16LE(ConversionUtilities::toFixed8(valueToConvertAndWrite));
519 }
520 
524 inline void BinaryWriter::writeFixed16LE(float32 valueToConvertAndWrite)
525 {
526  writeUInt32LE(ConversionUtilities::toFixed16(valueToConvertAndWrite));
527 }
528 } // namespace IoUtilities
529 
530 #endif // IO_UTILITIES_BINARYWRITER_H
void writeSynchsafeUInt32BE(uint32 valueToConvertAndWrite)
Writes a 32-bit big endian synchsafe integer to the current stream and advances the current position ...
Definition: binarywriter.h:482
void writeInt24LE(int32 value)
Writes a 24-bit little endian signed integer to the current stream and advances the current position ...
Definition: binarywriter.h:349
std::int64_t int64
signed 64-bit integer
Definition: types.h:29
void writeFloat64LE(float64 value)
Writes a 64-bit little endian floating point value to the current stream and advances the current pos...
Definition: binarywriter.h:455
void writeInt56LE(int64 value)
Writes a 56-bit big endian signed integer to the current stream and advances the current position of ...
Definition: binarywriter.h:409
CPP_UTILITIES_EXPORT constexpr uint16 toFixed8(float32 float32value)
Returns the 8.8 fixed point representation converted from the specified 32-bit floating point number...
bool hasOwnership() const
Returns whether the writer takes ownership over the assigned stream.
Definition: binarywriter.h:104
Writes primitive data types to a std::ostream.
Definition: binarywriter.h:13
void writeInt40LE(int64 value)
Writes a 40-bit big endian signed integer to the current stream and advances the current position of ...
Definition: binarywriter.h:389
void writeUInt24BE(uint32 value)
Writes a 24-bit big endian unsigned integer to the current stream and advances the current position o...
Definition: binarywriter.h:226
void writeUInt16BE(uint16 value)
Writes a 16-bit big endian unsigned integer to the current stream and advances the current position o...
Definition: binarywriter.h:206
void writeUInt56BE(uint64 value)
Writes a 56-bit big endian unsigned integer to the current stream and advances the current position o...
Definition: binarywriter.h:285
std::uint64_t uint64
unsigned 64-bit integer
Definition: types.h:49
void writeUInt40BE(uint64 value)
Writes a 40-bit big endian unsigned integer to the current stream and advances the current position o...
Definition: binarywriter.h:265
void writeByte(byte value)
Writes a single byte to the current stream and advances the current position of the stream by one byt...
Definition: binarywriter.h:180
void writeUInt16LE(uint16 value)
Writes a 16-bit little endian unsigned integer to the current stream and advances the current positio...
Definition: binarywriter.h:339
void writeBool(bool value)
Writes a boolean value to the current stream and advances the current position of the stream by one b...
Definition: binarywriter.h:189
void writeUInt24LE(uint32 value)
Writes a 24-bit little endian unsigned integer to the current stream and advances the current positio...
Definition: binarywriter.h:360
void giveOwnership()
The writer will take ownership over the assigned stream.
Definition: binarywriter.h:116
void writeInt16LE(int16 value)
Writes a 16-bit little endian signed integer to the current stream and advances the current position ...
Definition: binarywriter.h:330
void flush()
Calls the flush() method of the assigned stream.
Definition: binarywriter.h:138
void writeFloat64BE(float64 value)
Writes a 64-bit big endian floating point value to the current stream and advances the current positi...
Definition: binarywriter.h:321
void writeFloat32BE(float32 value)
Writes a 32-bit big endian floating point value to the current stream and advances the current positi...
Definition: binarywriter.h:312
Contains utility classes helping to read and write streams.
Definition: binaryreader.h:10
void detatchOwnership()
The writer will not take ownership over the assigned stream.
Definition: binarywriter.h:130
void writeUInt40LE(uint64 value)
Writes a 40-bit big endian unsigned integer to the current stream and advances the current position o...
Definition: binarywriter.h:399
void writeSynchsafeUInt32LE(uint32 valueToConvertAndWrite)
Writes a 32-bit little endian synchsafe integer to the current stream and advances the current positi...
Definition: binarywriter.h:508
void write(const char *buffer, std::streamsize length)
Writes a character array to the current stream and advances the current position of the stream by the...
Definition: binarywriter.h:154
void writeString(const std::string &value)
Writes a string to the current stream and advances the current position of the stream by the length o...
Definition: binarywriter.h:464
void writeInt56BE(int64 value)
Writes a 56-bit big endian signed integer to the current stream and advances the current position of ...
Definition: binarywriter.h:275
void writeChar(char value)
Writes a single character to the current stream and advances the current position of the stream by on...
Definition: binarywriter.h:171
std::uint32_t uint32
unsigned 32-bit integer
Definition: types.h:44
void writeFixed8LE(float32 valueToConvertAndWrite)
Writes the 8.8 fixed point little endian representation for the specified 32-bit floating point value...
Definition: binarywriter.h:516
void writeUInt56LE(uint64 value)
Writes a 56-bit big endian unsigned integer to the current stream and advances the current position o...
Definition: binarywriter.h:419
void writeUInt32LE(uint32 value)
Writes a 32-bit little endian unsigned integer to the current stream and advances the current positio...
Definition: binarywriter.h:379
void writeInt32LE(int32 value)
Writes a 32-bit little endian signed integer to the current stream and advances the current position ...
Definition: binarywriter.h:370
void writeInt16BE(int16 value)
Writes a 16-bit big endian signed integer to the current stream and advances the current position of ...
Definition: binarywriter.h:197
void writeInt64LE(int64 value)
Writes a 64-bit little endian signed integer to the current stream and advances the current position ...
Definition: binarywriter.h:428
std::int32_t int32
signed 32-bit integer
Definition: types.h:24
CPP_UTILITIES_EXPORT constexpr uint32 toSynchsafeInt(uint32 normalInt)
Returns a 32-bit synchsafe integer converted from a normal 32-bit integer.
void writeFixed8BE(float32 valueToConvertAndWrite)
Writes the 8.8 fixed point big endian representation for the specified 32-bit floating point value to...
Definition: binarywriter.h:490
const std::ostream * stream() const
Returns a pointer to the stream the writer will write to when calling one of the write-methods.
Definition: binarywriter.h:92
void writeUInt32BE(uint32 value)
Writes a 32-bit big endian unsigned integer to the current stream and advances the current position o...
Definition: binarywriter.h:245
void writeInt24BE(int32 value)
Writes a 24-bit big endian signed integer to the current stream and advances the current position of ...
Definition: binarywriter.h:216
void writeFloat32LE(float32 value)
Writes a 32-bit little endian floating point value to the current stream and advances the current pos...
Definition: binarywriter.h:446
std::uint8_t byte
unsigned byte
Definition: types.h:14
void writeFixed16BE(float32 valueToConvertAndWrite)
Writes the 16.16 fixed point big endian representation for the specified 32-bit floating point value ...
Definition: binarywriter.h:498
void writeInt64BE(int64 value)
Writes a 64-bit big endian signed integer to the current stream and advances the current position of ...
Definition: binarywriter.h:294
void writeUInt64LE(uint64 value)
Writes a 64-bit little endian unsigned integer to the current stream and advances the current positio...
Definition: binarywriter.h:437
std::int16_t int16
signed 16-bit integer
Definition: types.h:19
void writeInt32BE(int32 value)
Writes a 32-bit big endian signed integer to the current stream and advances the current position of ...
Definition: binarywriter.h:236
CPP_UTILITIES_EXPORT constexpr uint32 toFixed16(float32 float32value)
Returns the 16.16 fixed point representation converted from the specified 32-bit floating point numbe...
#define CPP_UTILITIES_EXPORT
Marks the symbol to be exported by the c++utilities library.
void writeFixed16LE(float32 valueToConvertAndWrite)
Writes the 16.16 fixed point little endian representation for the specified 32-bit floating point val...
Definition: binarywriter.h:524
void writeUInt64BE(uint64 value)
Writes a 64-bit big endian unsigned integer to the current stream and advances the current position o...
Definition: binarywriter.h:303
void writeTerminatedString(const std::string &value)
Writes a terminated string to the current stream and advances the current position of the stream by t...
Definition: binarywriter.h:472
bool fail() const
Returns an indication whether the fail bit of the assigned stream is set.
Definition: binarywriter.h:146
std::uint16_t uint16
unsigned 16-bit integer
Definition: types.h:39
void writeInt40BE(int64 value)
Writes a 40-bit big endian signed integer to the current stream and advances the current position of ...
Definition: binarywriter.h:255