C++ Utilities  4.17.0
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 <cstring>
8 #include <ostream>
9 #include <string>
10 #include <vector>
11 
12 namespace IoUtilities {
13 
15 public:
16  BinaryWriter(std::ostream *stream);
17  BinaryWriter(const BinaryWriter &other);
18  BinaryWriter &operator=(const BinaryWriter &rhs) = delete;
19  ~BinaryWriter();
20 
21  const std::ostream *stream() const;
22  std::ostream *stream();
23  void setStream(std::ostream *stream, bool giveOwnership = false);
24  bool hasOwnership() const;
25  void giveOwnership();
26  void detatchOwnership();
27  void flush();
28  bool fail() const;
29  void write(const char *buffer, std::streamsize length);
30  void write(const std::vector<char> &buffer, std::streamsize length);
31  void writeChar(char value);
32  void writeByte(byte value);
33  void writeInt16BE(int16 value);
34  void writeUInt16BE(uint16 value);
35  void writeInt24BE(int32 value);
36  void writeUInt24BE(uint32 value);
37  void writeInt32BE(int32 value);
38  void writeUInt32BE(uint32 value);
39  void writeInt40BE(int64 value);
40  void writeUInt40BE(uint64 value);
41  void writeInt56BE(int64 value);
42  void writeUInt56BE(uint64 value);
43  void writeInt64BE(int64 value);
44  void writeUInt64BE(uint64 value);
45  void writeVariableLengthUIntBE(uint64 value);
46  void writeFloat32BE(float32 value);
47  void writeFloat64BE(float64 value);
48  void writeInt16LE(int16 value);
49  void writeUInt16LE(uint16 value);
50  void writeInt24LE(int32 value);
51  void writeUInt24LE(uint32 value);
52  void writeInt32LE(int32 value);
53  void writeUInt32LE(uint32 value);
54  void writeInt40LE(int64 value);
55  void writeUInt40LE(uint64 value);
56  void writeInt56LE(int64 value);
57  void writeUInt56LE(uint64 value);
58  void writeInt64LE(int64 value);
59  void writeUInt64LE(uint64 value);
60  void writeVariableLengthUIntLE(uint64 value);
61  void writeFloat32LE(float32 value);
62  void writeFloat64LE(float64 value);
63  void writeString(const std::string &value);
64  void writeTerminatedString(const std::string &value);
65  void writeLengthPrefixedString(const std::string &value);
66  void writeLengthPrefixedCString(const char *value, std::size_t size);
67  void writeBool(bool value);
68  void writeSynchsafeUInt32BE(uint32 valueToConvertAndWrite);
69  void writeFixed8BE(float32 valueToConvertAndWrite);
70  void writeFixed16BE(float32 valueToConvertAndWrite);
71  void writeSynchsafeUInt32LE(uint32 valueToConvertAndWrite);
72  void writeFixed8LE(float32 valueToConvertAndWrite);
73  void writeFixed16LE(float32 valueToConvertAndWrite);
74 
75  // declare further overloads for write() to ease use of BinaryWriter in templates
76  void write(char oneChar);
77  void write(byte oneByte);
78  void write(bool oneBool);
79  void write(const std::string &lengthPrefixedString);
80  void write(const char *lengthPrefixedString);
81  void write(int16 one16BitInt);
82  void write(uint16 one16BitUint);
83  void write(int32 one32BitInt);
84  void write(uint32 one32BitUint);
85  void write(int64 one64BitInt);
86  void write(uint64 one64BitUint);
87  void write(float32 one32BitFloat);
88  void write(float64 one64BitFloat);
89 
90 private:
91  void writeVariableLengthInteger(uint64 size, void (*getBytes)(uint64, char *));
92 
93  std::ostream *m_stream;
94  bool m_ownership;
95  char m_buffer[8];
96 };
97 
103 inline std::ostream *BinaryWriter::stream()
104 {
105  return m_stream;
106 }
107 
113 inline const std::ostream *BinaryWriter::stream() const
114 {
115  return m_stream;
116 }
117 
125 inline bool BinaryWriter::hasOwnership() const
126 {
127  return m_ownership;
128 }
129 
138 {
139  if (m_stream) {
140  m_ownership = true;
141  }
142 }
143 
152 {
153  m_ownership = false;
154 }
155 
159 inline void BinaryWriter::flush()
160 {
161  m_stream->flush();
162 }
163 
167 inline bool BinaryWriter::fail() const
168 {
169  return m_stream ? m_stream->fail() : false;
170 }
171 
175 inline void BinaryWriter::write(const char *buffer, std::streamsize length)
176 {
177  m_stream->write(buffer, length);
178 }
179 
184 inline void BinaryWriter::write(const std::vector<char> &buffer, std::streamsize length)
185 {
186  m_stream->write(buffer.data(), length);
187 }
188 
192 inline void BinaryWriter::writeChar(char value)
193 {
194  m_buffer[0] = value;
195  m_stream->write(m_buffer, 1);
196 }
197 
201 inline void BinaryWriter::writeByte(byte value)
202 {
203  m_buffer[0] = *reinterpret_cast<char *>(&value);
204  m_stream->write(m_buffer, 1);
205 }
206 
210 inline void BinaryWriter::writeBool(bool value)
211 {
212  writeByte(value ? 1 : 0);
213 }
214 
219 {
220  ConversionUtilities::BE::getBytes(value, m_buffer);
221  m_stream->write(m_buffer, sizeof(int16));
222 }
223 
228 {
229  ConversionUtilities::BE::getBytes(value, m_buffer);
230  m_stream->write(m_buffer, sizeof(uint16));
231 }
232 
238 {
239  ConversionUtilities::BE::getBytes(value, m_buffer);
240  m_stream->write(m_buffer + 1, 3);
241 }
242 
248 {
249  // discard most significant byte
250  ConversionUtilities::BE::getBytes(value, m_buffer);
251  m_stream->write(m_buffer + 1, 3);
252 }
253 
258 {
259  ConversionUtilities::BE::getBytes(value, m_buffer);
260  m_stream->write(m_buffer, sizeof(int32));
261 }
262 
267 {
268  ConversionUtilities::BE::getBytes(value, m_buffer);
269  m_stream->write(m_buffer, sizeof(uint32));
270 }
271 
277 {
278  ConversionUtilities::BE::getBytes(value, m_buffer);
279  m_stream->write(m_buffer + 3, 5);
280 }
281 
287 {
288  ConversionUtilities::BE::getBytes(value, m_buffer);
289  m_stream->write(m_buffer + 3, 5);
290 }
291 
297 {
298  ConversionUtilities::BE::getBytes(value, m_buffer);
299  m_stream->write(m_buffer + 1, 7);
300 }
301 
307 {
308  ConversionUtilities::BE::getBytes(value, m_buffer);
309  m_stream->write(m_buffer + 1, 7);
310 }
311 
316 {
317  ConversionUtilities::BE::getBytes(value, m_buffer);
318  m_stream->write(m_buffer, sizeof(int64));
319 }
320 
325 {
326  ConversionUtilities::BE::getBytes(value, m_buffer);
327  m_stream->write(m_buffer, sizeof(uint64));
328 }
329 
335 {
336  writeVariableLengthInteger(value, static_cast<void (*)(uint64, char *)>(&ConversionUtilities::BE::getBytes));
337 }
338 
342 inline void BinaryWriter::writeFloat32BE(float32 value)
343 {
344  ConversionUtilities::BE::getBytes(value, m_buffer);
345  m_stream->write(m_buffer, sizeof(float32));
346 }
347 
351 inline void BinaryWriter::writeFloat64BE(float64 value)
352 {
353  ConversionUtilities::BE::getBytes(value, m_buffer);
354  m_stream->write(m_buffer, sizeof(float64));
355 }
356 
361 {
362  ConversionUtilities::LE::getBytes(value, m_buffer);
363  m_stream->write(m_buffer, sizeof(int16));
364 }
365 
370 {
371  ConversionUtilities::LE::getBytes(value, m_buffer);
372  m_stream->write(m_buffer, sizeof(uint16));
373 }
374 
380 {
381  // discard most significant byte
382  ConversionUtilities::LE::getBytes(value, m_buffer);
383  m_stream->write(m_buffer, 3);
384 }
385 
391 {
392  // discard most significant byte
393  ConversionUtilities::LE::getBytes(value, m_buffer);
394  m_stream->write(m_buffer, 3);
395 }
396 
401 {
402  ConversionUtilities::LE::getBytes(value, m_buffer);
403  m_stream->write(m_buffer, sizeof(int32));
404 }
405 
410 {
411  ConversionUtilities::LE::getBytes(value, m_buffer);
412  m_stream->write(m_buffer, sizeof(uint32));
413 }
414 
420 {
421  ConversionUtilities::LE::getBytes(value, m_buffer);
422  m_stream->write(m_buffer, 5);
423 }
424 
430 {
431  ConversionUtilities::LE::getBytes(value, m_buffer);
432  m_stream->write(m_buffer, 5);
433 }
434 
440 {
441  ConversionUtilities::LE::getBytes(value, m_buffer);
442  m_stream->write(m_buffer, 7);
443 }
444 
450 {
451  ConversionUtilities::LE::getBytes(value, m_buffer);
452  m_stream->write(m_buffer, 7);
453 }
454 
459 {
460  ConversionUtilities::LE::getBytes(value, m_buffer);
461  m_stream->write(m_buffer, sizeof(int64));
462 }
463 
468 {
469  ConversionUtilities::LE::getBytes(value, m_buffer);
470  m_stream->write(m_buffer, sizeof(uint64));
471 }
472 
478 {
479  writeVariableLengthInteger(value, static_cast<void (*)(uint64, char *)>(&ConversionUtilities::LE::getBytes));
480 }
481 
485 inline void BinaryWriter::writeFloat32LE(float32 value)
486 {
487  ConversionUtilities::LE::getBytes(value, m_buffer);
488  m_stream->write(m_buffer, sizeof(float32));
489 }
490 
494 inline void BinaryWriter::writeFloat64LE(float64 value)
495 {
496  ConversionUtilities::LE::getBytes(value, m_buffer);
497  m_stream->write(m_buffer, sizeof(float64));
498 }
499 
503 inline void BinaryWriter::writeString(const std::string &value)
504 {
505  m_stream->write(value.c_str(), value.length());
506 }
507 
511 inline void BinaryWriter::writeTerminatedString(const std::string &value)
512 {
513  m_stream->write(value.c_str(), value.length() + 1);
514 }
515 
521 inline void BinaryWriter::writeSynchsafeUInt32BE(uint32 valueToConvertAndWrite)
522 {
523  writeUInt32BE(ConversionUtilities::toSynchsafeInt(valueToConvertAndWrite));
524 }
525 
529 inline void BinaryWriter::writeFixed8BE(float32 valueToConvertAndWrite)
530 {
531  writeUInt16BE(ConversionUtilities::toFixed8(valueToConvertAndWrite));
532 }
533 
537 inline void BinaryWriter::writeFixed16BE(float32 valueToConvertAndWrite)
538 {
539  writeUInt32BE(ConversionUtilities::toFixed16(valueToConvertAndWrite));
540 }
541 
547 inline void BinaryWriter::writeSynchsafeUInt32LE(uint32 valueToConvertAndWrite)
548 {
549  writeUInt32LE(ConversionUtilities::toSynchsafeInt(valueToConvertAndWrite));
550 }
551 
555 inline void BinaryWriter::writeFixed8LE(float32 valueToConvertAndWrite)
556 {
557  writeUInt16LE(ConversionUtilities::toFixed8(valueToConvertAndWrite));
558 }
559 
563 inline void BinaryWriter::writeFixed16LE(float32 valueToConvertAndWrite)
564 {
565  writeUInt32LE(ConversionUtilities::toFixed16(valueToConvertAndWrite));
566 }
567 
571 inline void BinaryWriter::write(char oneChar)
572 {
573  writeChar(oneChar);
574 }
575 
579 inline void BinaryWriter::write(byte oneByte)
580 {
581  writeByte(oneByte);
582 }
583 
587 inline void BinaryWriter::write(bool oneBool)
588 {
589  writeBool(oneBool);
590 }
591 
597 inline void BinaryWriter::write(const std::string &lengthPrefixedString)
598 {
599  writeLengthPrefixedCString(lengthPrefixedString.data(), lengthPrefixedString.size());
600 }
601 
607 inline void BinaryWriter::write(const char *lengthPrefixedString)
608 {
609  writeLengthPrefixedCString(lengthPrefixedString, std::strlen(lengthPrefixedString));
610 }
611 
615 inline void BinaryWriter::write(int16 one16BitInt)
616 {
617  writeInt16BE(one16BitInt);
618 }
619 
623 inline void BinaryWriter::write(uint16 one16BitUint)
624 {
625  writeUInt16BE(one16BitUint);
626 }
627 
631 inline void BinaryWriter::write(int32 one32BitInt)
632 {
633  writeInt32BE(one32BitInt);
634 }
635 
639 inline void BinaryWriter::write(uint32 one32BitUint)
640 {
641  writeUInt32BE(one32BitUint);
642 }
643 
647 inline void BinaryWriter::write(int64 one64BitInt)
648 {
649  writeInt64BE(one64BitInt);
650 }
651 
655 inline void BinaryWriter::write(uint64 one64BitUint)
656 {
657  writeUInt64BE(one64BitUint);
658 }
659 
663 inline void BinaryWriter::write(float32 one32BitFloat)
664 {
665  writeFloat32BE(one32BitFloat);
666 }
667 
671 inline void BinaryWriter::write(float64 one64BitFloat)
672 {
673  writeFloat64BE(one64BitFloat);
674 }
675 } // namespace IoUtilities
676 
677 #endif // IO_UTILITIES_BINARYWRITER_H
void writeVariableLengthUIntLE(uint64 value)
Writes an up to 8 byte long little endian unsigned integer to the current stream and advances the cur...
Definition: binarywriter.h:477
void writeSynchsafeUInt32BE(uint32 valueToConvertAndWrite)
Writes a 32-bit big endian synchsafe integer to the current stream and advances the current position ...
Definition: binarywriter.h:521
void writeInt24LE(int32 value)
Writes a 24-bit little endian signed integer to the current stream and advances the current position ...
Definition: binarywriter.h:379
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:494
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:439
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:125
Writes primitive data types to a std::ostream.
Definition: binarywriter.h:14
#define CPP_UTILITIES_EXPORT
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:419
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:247
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:227
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:306
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:286
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:201
void writeUInt16LE(uint16 value)
Writes a 16-bit little endian unsigned integer to the current stream and advances the current positio...
Definition: binarywriter.h:369
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:210
void writeUInt24LE(uint32 value)
Writes a 24-bit little endian unsigned integer to the current stream and advances the current positio...
Definition: binarywriter.h:390
void giveOwnership()
The writer will take ownership over the assigned stream.
Definition: binarywriter.h:137
void writeVariableLengthUIntBE(uint64 value)
Writes an up to 8 byte long big endian unsigned integer to the current stream and advances the curren...
Definition: binarywriter.h:334
void writeInt16LE(int16 value)
Writes a 16-bit little endian signed integer to the current stream and advances the current position ...
Definition: binarywriter.h:360
void flush()
Calls the flush() method of the assigned stream.
Definition: binarywriter.h:159
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:351
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:342
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:151
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:429
void writeSynchsafeUInt32LE(uint32 valueToConvertAndWrite)
Writes a 32-bit little endian synchsafe integer to the current stream and advances the current positi...
Definition: binarywriter.h:547
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:175
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:503
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:296
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:192
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:555
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:449
void writeUInt32LE(uint32 value)
Writes a 32-bit little endian unsigned integer to the current stream and advances the current positio...
Definition: binarywriter.h:409
void writeInt32LE(int32 value)
Writes a 32-bit little endian signed integer to the current stream and advances the current position ...
Definition: binarywriter.h:400
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:218
void writeInt64LE(int64 value)
Writes a 64-bit little endian signed integer to the current stream and advances the current position ...
Definition: binarywriter.h:458
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:529
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:113
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:266
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:237
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:485
void writeLengthPrefixedCString(const char *value, std::size_t size)
Writes the length of a string and the string itself to the current stream.
void writeFixed16BE(float32 valueToConvertAndWrite)
Writes the 16.16 fixed point big endian representation for the specified 32-bit floating point value ...
Definition: binarywriter.h:537
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:315
CPP_UTILITIES_EXPORT void getBytes(int16 value, char *outputbuffer)
Stores the specified 16-bit signed integer value at a specified position in a char array.
void writeUInt64LE(uint64 value)
Writes a 64-bit little endian unsigned integer to the current stream and advances the current positio...
Definition: binarywriter.h:467
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:257
CPP_UTILITIES_EXPORT constexpr uint32 toFixed16(float32 float32value)
Returns the 16.16 fixed point representation converted from the specified 32-bit floating point numbe...
void writeFixed16LE(float32 valueToConvertAndWrite)
Writes the 16.16 fixed point little endian representation for the specified 32-bit floating point val...
Definition: binarywriter.h:563
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:324
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:511
bool fail() const
Returns an indication whether the fail bit of the assigned stream is set.
Definition: binarywriter.h:167
std::uint16_t uint16
unsigned 16-bit integer
Definition: types.h:39
CPP_UTILITIES_EXPORT void getBytes(int16 value, char *outputbuffer)
Stores the specified 16-bit signed integer value at a specified position in a char array.
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:276