1#ifndef IOUTILITIES_BINARYWRITER_H
2#define IOUTILITIES_BINARYWRITER_H
16 BinaryWriter(std::ostream *stream,
bool giveOwnership =
false);
21 const std::ostream *stream()
const;
22 std::ostream *stream();
23 void setStream(std::ostream *stream,
bool giveOwnership =
false);
24 bool hasOwnership()
const;
26 void detatchOwnership();
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(std::uint8_t value);
33 void writeInt16BE(std::int16_t value);
34 void writeUInt16BE(std::uint16_t value);
35 void writeInt24BE(std::int32_t value);
36 void writeUInt24BE(std::uint32_t value);
37 void writeInt32BE(std::int32_t value);
38 void writeUInt32BE(std::uint32_t value);
39 void writeInt40BE(std::int64_t value);
40 void writeUInt40BE(std::uint64_t value);
41 void writeInt56BE(std::int64_t value);
42 void writeUInt56BE(std::uint64_t value);
43 void writeInt64BE(std::int64_t value);
44 void writeUInt64BE(std::uint64_t value);
45 void writeVariableLengthUIntBE(std::uint64_t value);
46 void writeFloat32BE(
float value);
47 void writeFloat64BE(
double value);
48 void writeInt16LE(std::int16_t value);
49 void writeUInt16LE(std::uint16_t value);
50 void writeInt24LE(std::int32_t value);
51 void writeUInt24LE(std::uint32_t value);
52 void writeInt32LE(std::int32_t value);
53 void writeUInt32LE(std::uint32_t value);
54 void writeInt40LE(std::int64_t value);
55 void writeUInt40LE(std::uint64_t value);
56 void writeInt56LE(std::int64_t value);
57 void writeUInt56LE(std::uint64_t value);
58 void writeInt64LE(std::int64_t value);
59 void writeUInt64LE(std::uint64_t value);
60 void writeVariableLengthUIntLE(std::uint64_t value);
61 void writeFloat32LE(
float value);
62 void writeFloat64LE(
double 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(std::uint32_t valueToConvertAndWrite);
69 void writeFixed8BE(
float valueToConvertAndWrite);
70 void writeFixed16BE(
float valueToConvertAndWrite);
71 void writeSynchsafeUInt32LE(std::uint32_t valueToConvertAndWrite);
72 void writeFixed8LE(
float valueToConvertAndWrite);
73 void writeFixed16LE(
float valueToConvertAndWrite);
76 void write(
char oneChar);
77 void write(std::uint8_t oneByte);
78 void write(
bool oneBool);
79 void write(
const std::string &lengthPrefixedString);
80 void write(std::string_view lengthPrefixedString);
81 void write(
const char *lengthPrefixedString);
82 void write(std::int16_t one16BitInt);
83 void write(std::uint16_t one16BitUint);
84 void write(std::int32_t one32BitInt);
85 void write(std::uint32_t one32BitUint);
86 void write(std::int64_t one64BitInt);
87 void write(std::uint64_t one64BitUint);
88 void write(
float one32BitFloat);
89 void write(
double one64BitFloat);
92 void writeVariableLengthInteger(std::uint64_t size,
void (*getBytes)(std::uint64_t,
char *));
94 std::ostream *m_stream;
106 , m_ownership(giveOwnership)
115 : m_stream(other.m_stream)
201 return m_stream ? m_stream->fail() :
false;
209 m_stream->write(buffer, length);
218 m_stream->write(buffer.data(), length);
227 m_stream->write(m_buffer, 1);
235 m_buffer[0] = *
reinterpret_cast<char *
>(&value);
236 m_stream->write(m_buffer, 1);
253 m_stream->write(m_buffer,
sizeof(std::int16_t));
262 m_stream->write(m_buffer,
sizeof(std::uint16_t));
272 m_stream->write(m_buffer + 1, 3);
283 m_stream->write(m_buffer + 1, 3);
292 m_stream->write(m_buffer,
sizeof(std::int32_t));
301 m_stream->write(m_buffer,
sizeof(std::uint32_t));
311 m_stream->write(m_buffer + 3, 5);
321 m_stream->write(m_buffer + 3, 5);
331 m_stream->write(m_buffer + 1, 7);
341 m_stream->write(m_buffer + 1, 7);
350 m_stream->write(m_buffer,
sizeof(std::int64_t));
359 m_stream->write(m_buffer,
sizeof(std::uint64_t));
368 writeVariableLengthInteger(value,
static_cast<void (*)(std::uint64_t,
char *)
>(&
BE::getBytes));
377 m_stream->write(m_buffer,
sizeof(
float));
386 m_stream->write(m_buffer,
sizeof(
double));
395 m_stream->write(m_buffer,
sizeof(std::int16_t));
404 m_stream->write(m_buffer,
sizeof(std::uint16_t));
415 m_stream->write(m_buffer, 3);
426 m_stream->write(m_buffer, 3);
435 m_stream->write(m_buffer,
sizeof(std::int32_t));
444 m_stream->write(m_buffer,
sizeof(std::uint32_t));
454 m_stream->write(m_buffer, 5);
464 m_stream->write(m_buffer, 5);
474 m_stream->write(m_buffer, 7);
484 m_stream->write(m_buffer, 7);
493 m_stream->write(m_buffer,
sizeof(std::int64_t));
502 m_stream->write(m_buffer,
sizeof(std::uint64_t));
511 writeVariableLengthInteger(value,
static_cast<void (*)(std::uint64_t,
char *)
>(&
LE::getBytes));
520 m_stream->write(m_buffer,
sizeof(
float));
529 m_stream->write(m_buffer,
sizeof(
double));
537 m_stream->write(value.data(),
static_cast<std::streamsize
>(value.size()));
545 m_stream->write(value.data(),
static_cast<std::streamsize
>(value.size() + 1));
558 m_stream->write(value.data(),
static_cast<std::streamsize
>(value.size()));
571 m_stream->write(value,
static_cast<std::streamsize
>(size));
Writes primitive data types to a std::ostream.
BinaryWriter & operator=(const BinaryWriter &rhs)=delete
void writeVariableLengthUIntBE(std::uint64_t value)
Writes an up to 8 byte long big endian unsigned integer to the current stream and advances the curren...
void writeFloat32LE(float value)
Writes a 32-bit little endian floating point value to the current stream and advances the current pos...
void writeUInt40BE(std::uint64_t value)
Writes a 40-bit big endian unsigned integer to the current stream and advances the current position o...
void writeUInt64LE(std::uint64_t value)
Writes a 64-bit little endian unsigned integer to the current stream and advances the current positio...
void writeFixed16BE(float valueToConvertAndWrite)
Writes the 16.16 fixed point big endian representation for the specified 32-bit floating point value ...
void writeInt40LE(std::int64_t value)
Writes a 40-bit big endian signed integer to the current stream and advances the current position of ...
void writeUInt40LE(std::uint64_t value)
Writes a 40-bit big endian unsigned integer to the current stream and advances the current position o...
void writeInt64BE(std::int64_t value)
Writes a 64-bit big endian signed integer to the current stream and advances the current position of ...
void writeInt32LE(std::int32_t value)
Writes a 32-bit little endian signed integer to the current stream and advances the current position ...
bool hasOwnership() const
Returns whether the writer takes ownership over the assigned stream.
void writeUInt24LE(std::uint32_t value)
Writes a 24-bit little endian unsigned integer to the current stream and advances the current positio...
void writeFloat32BE(float value)
Writes a 32-bit big endian floating point value to the current stream and advances the current positi...
void writeFixed16LE(float valueToConvertAndWrite)
Writes the 16.16 fixed point little endian representation for the specified 32-bit floating point val...
void giveOwnership()
The writer will take ownership over the assigned stream.
bool fail() const
Returns an indication whether the fail bit of the assigned stream is set.
void writeInt24BE(std::int32_t value)
Writes a 24-bit big endian signed integer to the current stream and advances the current position of ...
void writeFixed8LE(float valueToConvertAndWrite)
Writes the 8.8 fixed point little endian representation for the specified 32-bit floating point value...
void writeUInt56BE(std::uint64_t value)
Writes a 56-bit big endian unsigned integer to the current stream and advances the current position o...
void writeUInt32BE(std::uint32_t value)
Writes a 32-bit big endian unsigned integer to the current stream and advances the current position o...
void writeFloat64BE(double value)
Writes a 64-bit big endian floating point value to the current stream and advances the current positi...
void writeFixed8BE(float valueToConvertAndWrite)
Writes the 8.8 fixed point big endian representation for the specified 32-bit floating point value to...
void writeFloat64LE(double value)
Writes a 64-bit little endian floating point value to the current stream and advances the current pos...
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...
void detatchOwnership()
The writer will not take ownership over the assigned stream.
void writeUInt56LE(std::uint64_t value)
Writes a 56-bit big endian unsigned integer to the current stream and advances the current position o...
void writeInt56LE(std::int64_t value)
Writes a 56-bit big endian signed integer to the current stream and advances the current position of ...
const std::ostream * stream() const
Returns a pointer to the stream the writer will write to when calling one of the write-methods.
void writeUInt16BE(std::uint16_t value)
Writes a 16-bit big endian unsigned integer to the current stream and advances the current position o...
void writeUInt64BE(std::uint64_t value)
Writes a 64-bit big endian unsigned integer to the current stream and advances the current position o...
void flush()
Calls the flush() method of the assigned stream.
void writeVariableLengthUIntLE(std::uint64_t value)
Writes an up to 8 byte long little endian unsigned integer to the current stream and advances the cur...
void writeInt56BE(std::int64_t value)
Writes a 56-bit big endian signed integer to the current stream and advances the current position of ...
void writeInt16LE(std::int16_t value)
Writes a 16-bit little endian signed integer to the current stream and advances the current position ...
void writeUInt32LE(std::uint32_t value)
Writes a 32-bit little endian unsigned integer to the current stream and advances the current positio...
BinaryWriter(std::ostream *stream, bool giveOwnership=false)
Constructs a new BinaryWriter.
void writeUInt16LE(std::uint16_t value)
Writes a 16-bit little endian unsigned integer to the current stream and advances the current positio...
void writeInt16BE(std::int16_t value)
Writes a 16-bit big endian signed integer to the current stream and advances the current position of ...
void writeChar(char value)
Writes a single character to the current stream and advances the current position of the stream by on...
void writeSynchsafeUInt32LE(std::uint32_t valueToConvertAndWrite)
Writes a 32-bit little endian synchsafe integer to the current stream and advances the current positi...
void writeByte(std::uint8_t value)
Writes a single byte to the current stream and advances the current position of the stream by one byt...
void writeLengthPrefixedString(const std::string &value)
Writes the length of a string and the string itself to the current stream.
void writeInt24LE(std::int32_t value)
Writes a 24-bit little endian signed integer to the current stream and advances the current position ...
void writeUInt24BE(std::uint32_t value)
Writes a 24-bit big endian unsigned integer to the current stream and advances the current position o...
~BinaryWriter()
Destroys the BinaryWriter.
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...
void writeLengthPrefixedCString(const char *value, std::size_t size)
Writes the length of a string and the string itself to the current stream.
void writeTerminatedString(const std::string &value)
Writes a terminated string to the current stream and advances the current position of the stream by t...
void writeInt64LE(std::int64_t value)
Writes a 64-bit little endian signed integer to the current stream and advances the current position ...
void writeSynchsafeUInt32BE(std::uint32_t valueToConvertAndWrite)
Writes a 32-bit big endian synchsafe integer to the current stream and advances the current position ...
void writeInt32BE(std::int32_t value)
Writes a 32-bit big endian signed integer to the current stream and advances the current position of ...
void writeInt40BE(std::int64_t value)
Writes a 40-bit big endian signed integer to the current stream and advances the current position of ...
void writeBool(bool value)
Writes a boolean value to the current stream and advances the current position of the stream by one b...
#define CPP_UTILITIES_EXPORT
Marks the symbol to be exported by the c++utilities library.
CPP_UTILITIES_EXPORT void getBytes(T value, char *outputbuffer)
Stores the specified (unsigned) integer value in a char array.
CPP_UTILITIES_EXPORT void getBytes(T value, char *outputbuffer)
Stores the specified (unsigned) integer value in a char array.
Contains all utilities provides by the c++utilities library.
CPP_UTILITIES_EXPORT constexpr std::uint32_t toFixed16(float float32value)
Returns the 16.16 fixed point representation converted from the specified 32-bit floating point numbe...
CPP_UTILITIES_EXPORT constexpr std::uint16_t toFixed8(float float32value)
Returns the 8.8 fixed point representation converted from the specified 32-bit floating point number.
CPP_UTILITIES_EXPORT constexpr std::uint32_t toSynchsafeInt(std::uint32_t normalInt)
Returns a 32-bit synchsafe integer converted from a normal 32-bit integer.