C++ Utilities
5.36.0
Useful C++ classes and routines such as argument parser, IO and conversion utilities
Toggle main menu visibility
Loading...
Searching...
No Matches
io
binarywriter.cpp
Go to the documentation of this file.
1
#include "
./binarywriter.h
"
2
3
#include "
../conversion/conversionexception.h
"
4
5
using namespace
std
;
6
7
namespace
CppUtilities
{
8
15
27
void
BinaryWriter::setStream
(ostream *
stream
,
bool
giveOwnership
)
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
44
void
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
binarywriter.h
CppUtilities::BinaryWriter::giveOwnership
void giveOwnership()
The writer will take ownership over the assigned stream.
Definition
binarywriter.h:169
CppUtilities::BinaryWriter::setStream
void setStream(std::ostream *stream, bool giveOwnership=false)
Assigns the stream the writer will write to when calling one of the write-methods.
Definition
binarywriter.cpp:27
CppUtilities::BinaryWriter::stream
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:145
CppUtilities::ConversionException::ConversionException
ConversionException() noexcept
Constructs a new ConversionException.
Definition
conversionexception.h:28
conversionexception.h
CppUtilities
Contains all utilities provided by the c++utilities library.
Definition
argumentparser.h:18
std
STL namespace.
Generated on
for C++ Utilities by
1.18.0