![]() |
nalchi
|
Helper stream to write bits to your buffer. More...
#include <nalchi/bit_stream.hpp>
Public Member Functions | |
bit_stream_writer (const bit_stream_writer &)=delete | |
Deleted copy constructor. | |
auto | operator= (const bit_stream_writer &) -> bit_stream_writer &=delete |
Deleted copy assignment operator. | |
bit_stream_writer () | |
Constructs a bit_stream_writer instance without a buffer. | |
bit_stream_writer (shared_payload buffer, size_type logical_bytes_length) | |
Constructs a bit_stream_writer instance with a shared_payload buffer. | |
bit_stream_writer (std::span< word_type > buffer, size_type logical_bytes_length) | |
Constructs a bit_stream_writer instance with a std::span<word_type> buffer. | |
bit_stream_writer (word_type *begin, word_type *end, size_type logical_bytes_length) | |
Constructs a bit_stream_writer instance with a word range. | |
bit_stream_writer (word_type *begin, size_type words_length, size_type logical_bytes_length) | |
Constructs a bit_stream_writer instance with a word begin pointer and the word length. | |
void | set_fail () |
Force set the fail flag. | |
bool | fail () const noexcept |
Check if writing to your buffer has been failed or not. | |
bool | operator! () const noexcept |
Check if there was an error in the writing to your buffer. This is effectively same as fail() . | |
operator bool () const noexcept | |
Check if there was no error in the writing to your buffer. This is effectively same as !fail() . | |
auto | total_bytes () const -> size_type |
Gets the number of total bytes in the stream. | |
auto | total_bits () const -> size_type |
Gets the number of total bits in the stream. | |
auto | used_bytes () const -> size_type |
Gets the number of used bytes in the stream. | |
auto | used_bits () const -> size_type |
Gets the number of used bits in the stream. | |
auto | unused_bytes () const -> size_type |
Gets the number of unused bytes in the stream. | |
auto | unused_bits () const -> size_type |
Gets the number of unused bits in the stream. | |
void | restart () |
Restarts the stream so that it can write from the beginning again. | |
void | reset () |
Resets the stream so that it no longer holds your buffer anymore. | |
void | reset_with (shared_payload buffer, size_type logical_bytes_length) |
Resets the stream with a shared_payload buffer. | |
void | reset_with (std::span< word_type > buffer, size_type logical_bytes_length) |
Resets the stream with a std::span<word_type> buffer. | |
void | reset_with (word_type *begin, word_type *end, size_type logical_bytes_length) |
Resets the stream with a word range. | |
void | reset_with (word_type *begin, size_type words_length, size_type logical_bytes_length) |
Resets the stream with a word begin pointer and the word length. | |
auto | flush_final () -> bit_stream_writer & |
Flushes the last remaining bytes on the internal scratch buffer to your buffer. | |
bool | flushed () const |
Checks if flush_final() has been called or not. | |
auto | write (const void *data, size_type size) -> bit_stream_writer & |
Writes some arbitrary data to the bit stream. | |
template<std::integral SInt> requires (sizeof(SInt) <= sizeof(word_type)) | |
auto | write (SInt data, SInt min=std::numeric_limits< SInt >::min(), SInt max=std::numeric_limits< SInt >::max()) -> bit_stream_writer & |
Writes an integral value to the bit stream. | |
template<std::integral BInt> requires (sizeof(BInt) > sizeof(word_type)) | |
auto | write (BInt data, BInt min=std::numeric_limits< BInt >::min(), BInt max=std::numeric_limits< BInt >::max()) -> bit_stream_writer & |
Writes an integral value to the bit stream. | |
auto | write (float data) -> bit_stream_writer & |
Writes a float value to the bit stream. | |
auto | write (double data) -> bit_stream_writer & |
Writes a double value to the bit stream. | |
template<character CharT, typename CharTraits > | |
auto | write (std::basic_string_view< CharT, CharTraits > str) -> bit_stream_writer & |
Writes a string view to the bit stream. | |
template<character CharT, typename CharTraits , typename Allocator > | |
auto | write (const std::basic_string< CharT, CharTraits, Allocator > &str) -> bit_stream_writer & |
Writes a string to the bit stream. | |
template<character CharT> | |
auto | write (const CharT *str) -> bit_stream_writer & |
Writes a null-terminated string to the bit stream. | |
Static Public Attributes | |
static constexpr size_type | STR_LEN_PREFIX_PREFIX_BITS = 2u |
static constexpr size_type | MIN_STR_LEN_PREFIX_PREFIX = 0u |
static constexpr size_type | MAX_STR_LEN_PREFIX_PREFIX = 3u |
Helper stream to write bits to your buffer.
Its design is based on the articles by Glenn Fiedler, see:
bit_stream_writer
uses an internal scratch buffer, so the final few bytes might not be flushed to your buffer yet when you're done writing. flush_final()
to flush the remaining bytes to your buffer. bit_stream_writer
instance won't flush them, either.) nalchi::bit_stream_writer::bit_stream_writer | ( | ) |
Constructs a bit_stream_writer
instance without a buffer.
This constructor can be useful if you want to set the buffer afterwards.
To set the buffer, call reset_with()
.
nalchi::bit_stream_writer::bit_stream_writer | ( | shared_payload | buffer, |
size_type | logical_bytes_length ) |
Constructs a bit_stream_writer
instance with a shared_payload
buffer.
buffer | Buffer to write bits to. |
logical_bytes_length | Number of bytes logically. This is useful if you want to only allow partial write to the final word. |
nalchi::bit_stream_writer::bit_stream_writer | ( | std::span< word_type > | buffer, |
size_type | logical_bytes_length ) |
Constructs a bit_stream_writer
instance with a std::span<word_type>
buffer.
buffer | Buffer to write bits to. |
logical_bytes_length | Number of bytes logically. This is useful if you want to only allow partial write to the final word. |
nalchi::bit_stream_writer::bit_stream_writer | ( | word_type * | begin, |
word_type * | end, | ||
size_type | logical_bytes_length ) |
Constructs a bit_stream_writer
instance with a word range.
begin | Pointer to the beginning of a buffer. |
end | Pointer to the end of a buffer. |
logical_bytes_length | Number of bytes logically. This is useful if you want to only allow partial write to the final word. |
nalchi::bit_stream_writer::bit_stream_writer | ( | word_type * | begin, |
size_type | words_length, | ||
size_type | logical_bytes_length ) |
Constructs a bit_stream_writer
instance with a word begin pointer and the word length.
begin | Pointer to the beginning of a buffer. |
words_length | Number of words in the buffer. |
logical_bytes_length | Number of bytes logically. This is useful if you want to only allow partial write to the final word. |
|
inlinenoexcept |
Check if writing to your buffer has been failed or not.
If this is true
, all the operations for this bit_stream_writer
is no-op.
true
if writing has been failed, otherwise false
. auto nalchi::bit_stream_writer::flush_final | ( | ) | -> bit_stream_writer & |
Flushes the last remaining bytes on the internal scratch buffer to your buffer.
|
inline |
Checks if flush_final()
has been called or not.
flush_final()
has been called or not.
|
inlinenoexcept |
Check if there was no error in the writing to your buffer.
This is effectively same as !fail()
.
If this is false
, all the operations for this bit_stream_writer
is no-op.
|
inlinenoexcept |
Check if there was an error in the writing to your buffer.
This is effectively same as fail()
.
If this is true
, all the operations for this bit_stream_writer
is no-op.
void nalchi::bit_stream_writer::reset | ( | ) |
Resets the stream so that it no longer holds your buffer anymore.
flush_final()
beforehand. void nalchi::bit_stream_writer::reset_with | ( | shared_payload | buffer, |
size_type | logical_bytes_length ) |
Resets the stream with a shared_payload
buffer.
flush_final()
beforehand. buffer | Buffer to write bits to. |
logical_bytes_length | Number of bytes logically. This is useful if you want to only allow partial write to the final word. |
void nalchi::bit_stream_writer::reset_with | ( | std::span< word_type > | buffer, |
size_type | logical_bytes_length ) |
Resets the stream with a std::span<word_type>
buffer.
flush_final()
beforehand. buffer | Buffer to write bits to. |
logical_bytes_length | Number of bytes logically. This is useful if you want to only allow partial write to the final word. |
void nalchi::bit_stream_writer::reset_with | ( | word_type * | begin, |
size_type | words_length, | ||
size_type | logical_bytes_length ) |
Resets the stream with a word begin pointer and the word length.
flush_final()
beforehand. begin | Pointer to the beginning of a buffer. |
words_length | Number of words in the buffer. |
logical_bytes_length | Number of bytes logically. This is useful if you want to only allow partial write to the final word. |
void nalchi::bit_stream_writer::reset_with | ( | word_type * | begin, |
word_type * | end, | ||
size_type | logical_bytes_length ) |
Resets the stream with a word range.
flush_final()
beforehand. begin | Pointer to the beginning of a buffer. |
end | Pointer to the end of a buffer. |
logical_bytes_length | Number of bytes logically. This is useful if you want to only allow partial write to the final word. |
void nalchi::bit_stream_writer::restart | ( | ) |
Restarts the stream so that it can write from the beginning again.
flush_final()
beforehand.
|
inline |
Gets the number of total bits in the stream.
|
inline |
Gets the number of total bytes in the stream.
|
inline |
Gets the number of unused bits in the stream.
|
inline |
Gets the number of unused bytes in the stream.
|
inline |
Gets the number of used bits in the stream.
auto nalchi::bit_stream_writer::used_bytes | ( | ) | const -> size_type |
Gets the number of used bytes in the stream.
|
inline |
Writes an integral value to the bit stream.
BInt | Big integer type that exceeds the size of word_type . |
data | Data to write. |
min | Minimum value allowed for data . |
max | Maximum value allowed for data . |
|
inline |
Writes a null-terminated string to the bit stream.
CharT | Character type of the null-terminated string. |
str | String to write. |
|
inline |
Writes a string to the bit stream.
CharT | Underlying character type of std::basic_string . |
CharTraits | Char traits for CharT . |
Allocator | Underlying allocator for std::basic_string . |
str | String to write. |
auto nalchi::bit_stream_writer::write | ( | const void * | data, |
size_type | size ) -> bit_stream_writer & |
Writes some arbitrary data to the bit stream.
data | Pointer to the arbitrary data. |
size | Size in bytes of the data. |
auto nalchi::bit_stream_writer::write | ( | double | data | ) | -> bit_stream_writer & |
Writes a double value to the bit stream.
data | Data to write. |
auto nalchi::bit_stream_writer::write | ( | float | data | ) | -> bit_stream_writer & |
Writes a float value to the bit stream.
data | Data to write. |
|
inline |
Writes an integral value to the bit stream.
SInt | Small integer type that doesn't exceed the size of word_type . |
data | Data to write. |
min | Minimum value allowed for data . |
max | Maximum value allowed for data . |
|
inline |
Writes a string view to the bit stream.
CharT | Underlying character type of std::basic_string_view . |
CharTraits | Char traits for CharT . |
str | String to write. |