C++ Utilities 5.31.1
Useful C++ classes and routines such as argument parser, IO and conversion utilities
Loading...
Searching...
No Matches
binarywriter.cpp
Go to the documentation of this file.
1#include "./binarywriter.h"
2
4
5using namespace std;
6
7namespace CppUtilities {
8
15
28{
29 if (m_ownership) {
30 delete m_stream;
31 }
32 if (stream) {
33 m_stream = stream;
34 m_ownership = giveOwnership;
35 } else {
36 m_stream = nullptr;
37 m_ownership = false;
38 }
39}
40
44void BinaryWriter::writeVariableLengthInteger(std::uint64_t value, void (*getBytes)(std::uint64_t, char *))
45{
46 std::uint64_t boundCheck = 0x80;
47 std::uint8_t prefixLength = 1;
48 for (; boundCheck != 0x8000000000000000; boundCheck <<= 7, ++prefixLength) {
49 if (value < boundCheck) {
50 getBytes(value | boundCheck, m_buffer);
51 break;
52 }
53 }
54 if (prefixLength == 9) {
55 throw ConversionException("The variable-length integer to be written exceeds the maximum.");
56 }
57 m_stream->write(m_buffer + 8 - prefixLength, prefixLength);
58}
59
60} // namespace CppUtilities
void giveOwnership()
The writer will take ownership over the assigned stream.
void setStream(std::ostream *stream, bool giveOwnership=false)
Assigns the stream the writer will write to when calling one of the write-methods.
const std::ostream * stream() const
Returns a pointer to the stream the writer will write to when calling one of the write-methods.
Contains all utilities provided by the c++utilities library.
STL namespace.