C++ Utilities  4.6.1
Common C++ classes and routines used by my applications 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/types.h"
5 #include "../conversion/binaryconversion.h"
6 
7 #include <vector>
8 #include <string>
9 #include <ostream>
10 
11 namespace IoUtilities
12 {
13 
15 {
16 public:
17  BinaryWriter(std::ostream *stream);
18  BinaryWriter(const BinaryWriter &other);
19  BinaryWriter & operator=(const BinaryWriter & rhs) = delete;
20  ~BinaryWriter();
21 
22  const std::ostream *stream() const;
23  std::ostream *stream();
24  void setStream(std::ostream *stream, bool giveOwnership = false);
25  bool hasOwnership() const;
26  void giveOwnership();
27  void detatchOwnership();
28  void flush();
29  bool fail() const;
30  void write(const char *buffer, std::streamsize length);
31  void write(const std::vector<char> &buffer, std::streamsize length);
32  void writeChar(char value);
33  void writeByte(byte value);
34  void writeInt16BE(int16 value);
35  void writeUInt16BE(uint16 value);
36  void writeInt24BE(int32 value);
37  void writeUInt24BE(uint32 value);
38  void writeInt32BE(int32 value);
39  void writeUInt32BE(uint32 value);
40  void writeInt40BE(int64 value);
41  void writeUInt40BE(uint64 value);
42  void writeInt56BE(int64 value);
43  void writeUInt56BE(uint64 value);
44  void writeInt64BE(int64 value);
45  void writeUInt64BE(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 writeFloat32LE(float32 value);
61  void writeFloat64LE(float64 value);
62  void writeString(const std::string &value);
63  void writeTerminatedString(const std::string &value);
64  void writeLengthPrefixedString(const std::string &value);
65  void writeBool(bool value);
66  void writeSynchsafeUInt32BE(uint32 valueToConvertAndWrite);
67  void writeFixed8BE(float32 valueToConvertAndWrite);
68  void writeFixed16BE(float32 valueToConvertAndWrite);
69  void writeSynchsafeUInt32LE(uint32 valueToConvertAndWrite);
70  void writeFixed8LE(float32 valueToConvertAndWrite);
71  void writeFixed16LE(float32 valueToConvertAndWrite);
72 
73 private:
74  std::ostream *m_stream;
75  bool m_ownership;
76  char m_buffer[8];
77 };
78 
84 inline std::ostream *BinaryWriter::stream()
85 {
86  return m_stream;
87 }
88 
94 inline const std::ostream *BinaryWriter::stream() const
95 {
96  return m_stream;
97 }
98 
106 inline bool BinaryWriter::hasOwnership() const
107 {
108  return m_ownership;
109 }
110 
119 {
120  if(m_stream) {
121  m_ownership = true;
122  }
123 }
124 
133 {
134  m_ownership = false;
135 }
136 
140 inline void BinaryWriter::flush()
141 {
142  m_stream->flush();
143 }
144 
148 inline bool BinaryWriter::fail() const
149 {
150  return m_stream ? m_stream->fail() : false;
151 }
152 
156 inline void BinaryWriter::write(const char *buffer, std::streamsize length)
157 {
158  m_stream->write(buffer, length);
159 }
160 
165 inline void BinaryWriter::write(const std::vector<char> &buffer, std::streamsize length)
166 {
167  m_stream->write(buffer.data(), length);
168 }
169 
173 inline void BinaryWriter::writeChar(char value)
174 {
175  m_buffer[0] = value;
176  m_stream->write(m_buffer, 1);
177 }
178 
182 inline void BinaryWriter::writeByte(byte value)
183 {
184  m_buffer[0] = *reinterpret_cast<char *>(&value);
185  m_stream->write(m_buffer, 1);
186 }
187 
191 inline void BinaryWriter::writeBool(bool value)
192 {
193  writeByte(value ? 1 : 0);
194 }
195 
200 {
201  ConversionUtilities::BE::getBytes(value, m_buffer);
202  m_stream->write(m_buffer, sizeof(int16));
203 }
204 
209 {
210  ConversionUtilities::BE::getBytes(value, m_buffer);
211  m_stream->write(m_buffer, sizeof(uint16));
212 }
213 
219 {
220  ConversionUtilities::BE::getBytes(value, m_buffer);
221  m_stream->write(m_buffer + 1, 3);
222 }
223 
229 {
230  // discard most significant byte
231  ConversionUtilities::BE::getBytes(value, m_buffer);
232  m_stream->write(m_buffer + 1, 3);
233 }
234 
239 {
240  ConversionUtilities::BE::getBytes(value, m_buffer);
241  m_stream->write(m_buffer, sizeof(int32));
242 }
243 
248 {
249  ConversionUtilities::BE::getBytes(value, m_buffer);
250  m_stream->write(m_buffer, sizeof(uint32));
251 }
252 
258 {
259  ConversionUtilities::BE::getBytes(value, m_buffer);
260  m_stream->write(m_buffer + 3, 5);
261 }
262 
268 {
269  ConversionUtilities::BE::getBytes(value, m_buffer);
270  m_stream->write(m_buffer + 3, 5);
271 }
272 
278 {
279  ConversionUtilities::BE::getBytes(value, m_buffer);
280  m_stream->write(m_buffer + 1, 7);
281 }
282 
288 {
289  ConversionUtilities::BE::getBytes(value, m_buffer);
290  m_stream->write(m_buffer + 1, 7);
291 }
292 
297 {
298  ConversionUtilities::BE::getBytes(value, m_buffer);
299  m_stream->write(m_buffer, sizeof(int64));
300 }
301 
306 {
307  ConversionUtilities::BE::getBytes(value, m_buffer);
308  m_stream->write(m_buffer, sizeof(uint64));
309 }
310 
314 inline void BinaryWriter::writeFloat32BE(float32 value)
315 {
316  ConversionUtilities::BE::getBytes(value, m_buffer);
317  m_stream->write(m_buffer, sizeof(float32));
318 }
319 
323 inline void BinaryWriter::writeFloat64BE(float64 value)
324 {
325  ConversionUtilities::BE::getBytes(value, m_buffer);
326  m_stream->write(m_buffer, sizeof(float64));
327 }
328 
333 {
334  ConversionUtilities::LE::getBytes(value, m_buffer);
335  m_stream->write(m_buffer, sizeof(int16));
336 }
337 
342 {
343  ConversionUtilities::LE::getBytes(value, m_buffer);
344  m_stream->write(m_buffer, sizeof(uint16));
345 }
346 
352 {
353  // discard most significant byte
354  ConversionUtilities::LE::getBytes(value, m_buffer);
355  m_stream->write(m_buffer, 3);
356 }
357 
363 {
364  // discard most significant byte
365  ConversionUtilities::LE::getBytes(value, m_buffer);
366  m_stream->write(m_buffer, 3);
367 }
368 
373 {
374  ConversionUtilities::LE::getBytes(value, m_buffer);
375  m_stream->write(m_buffer, sizeof(int32));
376 }
377 
382 {
383  ConversionUtilities::LE::getBytes(value, m_buffer);
384  m_stream->write(m_buffer, sizeof(uint32));
385 }
386 
392 {
393  ConversionUtilities::LE::getBytes(value, m_buffer);
394  m_stream->write(m_buffer, 5);
395 }
396 
402 {
403  ConversionUtilities::LE::getBytes(value, m_buffer);
404  m_stream->write(m_buffer, 5);
405 }
406 
412 {
413  ConversionUtilities::LE::getBytes(value, m_buffer);
414  m_stream->write(m_buffer, 7);
415 }
416 
422 {
423  ConversionUtilities::LE::getBytes(value, m_buffer);
424  m_stream->write(m_buffer, 7);
425 }
426 
431 {
432  ConversionUtilities::LE::getBytes(value, m_buffer);
433  m_stream->write(m_buffer, sizeof(int64));
434 }
435 
440 {
441  ConversionUtilities::LE::getBytes(value, m_buffer);
442  m_stream->write(m_buffer, sizeof(uint64));
443 }
444 
448 inline void BinaryWriter::writeFloat32LE(float32 value)
449 {
450  ConversionUtilities::LE::getBytes(value, m_buffer);
451  m_stream->write(m_buffer, sizeof(float32));
452 }
453 
457 inline void BinaryWriter::writeFloat64LE(float64 value)
458 {
459  ConversionUtilities::LE::getBytes(value, m_buffer);
460  m_stream->write(m_buffer, sizeof(float64));
461 }
462 
466 inline void BinaryWriter::writeString(const std::string &value)
467 {
468  m_stream->write(value.c_str(), value.length());
469 }
470 
474 inline void BinaryWriter::writeTerminatedString(const std::string &value)
475 {
476  m_stream->write(value.c_str(), value.length() + 1);
477 }
478 
484 inline void BinaryWriter::writeSynchsafeUInt32BE(uint32 valueToConvertAndWrite)
485 {
486  writeUInt32BE(ConversionUtilities::toSynchsafeInt(valueToConvertAndWrite));
487 }
488 
492 inline void BinaryWriter::writeFixed8BE(float32 valueToConvertAndWrite)
493 {
494  writeUInt16BE(ConversionUtilities::toFixed8(valueToConvertAndWrite));
495 }
496 
500 inline void BinaryWriter::writeFixed16BE(float32 valueToConvertAndWrite)
501 {
502  writeUInt32BE(ConversionUtilities::toFixed16(valueToConvertAndWrite));
503 }
504 
510 inline void BinaryWriter::writeSynchsafeUInt32LE(uint32 valueToConvertAndWrite)
511 {
512  writeUInt32LE(ConversionUtilities::toSynchsafeInt(valueToConvertAndWrite));
513 }
514 
518 inline void BinaryWriter::writeFixed8LE(float32 valueToConvertAndWrite)
519 {
520  writeUInt16LE(ConversionUtilities::toFixed8(valueToConvertAndWrite));
521 }
522 
526 inline void BinaryWriter::writeFixed16LE(float32 valueToConvertAndWrite)
527 {
528  writeUInt32LE(ConversionUtilities::toFixed16(valueToConvertAndWrite));
529 }
530 
531 }
532 
533 #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:484
void writeInt24LE(int32 value)
Writes a 24-bit little endian signed integer to the current stream and advances the current position ...
Definition: binarywriter.h:351
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:457
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:411
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:106
Writes primitive data types to a std::ostream.
Definition: binarywriter.h:14
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:391
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:228
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:208
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:287
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:267
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:182
void writeUInt16LE(uint16 value)
Writes a 16-bit little endian unsigned integer to the current stream and advances the current positio...
Definition: binarywriter.h:341
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:191
void writeUInt24LE(uint32 value)
Writes a 24-bit little endian unsigned integer to the current stream and advances the current positio...
Definition: binarywriter.h:362
void giveOwnership()
The writer will take ownership over the assigned stream.
Definition: binarywriter.h:118
void writeInt16LE(int16 value)
Writes a 16-bit little endian signed integer to the current stream and advances the current position ...
Definition: binarywriter.h:332
void flush()
Calls the flush() method of the assigned stream.
Definition: binarywriter.h:140
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:323
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:314
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:132
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:401
void writeSynchsafeUInt32LE(uint32 valueToConvertAndWrite)
Writes a 32-bit little endian synchsafe integer to the current stream and advances the current positi...
Definition: binarywriter.h:510
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:156
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:466
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:277
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:173
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:518
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:421
void writeUInt32LE(uint32 value)
Writes a 32-bit little endian unsigned integer to the current stream and advances the current positio...
Definition: binarywriter.h:381
void writeInt32LE(int32 value)
Writes a 32-bit little endian signed integer to the current stream and advances the current position ...
Definition: binarywriter.h:372
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:199
void writeInt64LE(int64 value)
Writes a 64-bit little endian signed integer to the current stream and advances the current position ...
Definition: binarywriter.h:430
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:492
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:94
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:247
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:218
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:448
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:500
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:296
void writeUInt64LE(uint64 value)
Writes a 64-bit little endian unsigned integer to the current stream and advances the current positio...
Definition: binarywriter.h:439
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:238
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:526
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:305
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:474
bool fail() const
Returns an indication whether the fail bit of the assigned stream is set.
Definition: binarywriter.h:148
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:257