framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Public Member Functions | List of all members
framework::serializable::bit_stream< Stream, Block > Class Template Reference

Bit stream. More...

#include <bit_stream.hpp>

Inherits reference_wrapper< Stream >.

Public Member Functions

 bit_stream (Stream &s)
 Wrapper constructor. More...
 
template<std::size_t N>
bool read (char *s)
 Read bytes.
 
bool read (char *s, std::size_t n)
 Read bytes.
 
template<std::size_t N>
bool write (char const *s)
 Read bytes.
 
bool write (char const *s, std::size_t n)
 Read bytes.
 
template<std::size_t N, typename T >
bool read_bits (T &t)
 Read bits. More...
 
template<typename T >
bool read_bits (T &t, std::size_t n)
 Read bits. More...
 
template<std::size_t N, typename T >
bool write_bits (T const &t)
 Write bits. More...
 
template<typename T >
bool write_bits (T const &t, std::size_t n)
 Write bits. More...
 
bool flush ()
 Flush output stream. More...
 

Detailed Description

template<typename Stream, typename Block>
class framework::serializable::bit_stream< Stream, Block >

Bit stream.

Provides bit-level access to the underlying stream. Blocks of data are serialized according to the provided Block specification - each block's bits are consumed from the most to the least significant bit. This wrapper provides traditional byte-level read/write methods with each byte consuming the next CHAR_BIT bits in the stream, and type-sensitive read_bits/write_bits methods which read a fixed number of bits into the provided container.

Note that the interpretation of a byte-level method is dependent on the definition of the Block type - streams should generally use big endian byte ordering here to avoid altering the implicit definition of underlying types. For example, consider the following field definitions:

using field = value <NAME("Field 1"), big_endian <uint32_t>>;
using ex1 = alias <bit_field <big_endian <uint16_t>, field>>;
using ex2 = alias <bit_field <little_endian <uint16_t>, field>>;

In the first example, ex1, the definition of field is unaltered - a byte-sequence (a,b,c,d) is read into Field 1 as 0xabcd, as expected. In the second example, on the other hand, a byte sequence (a,b,c,d) is effectively interpreted as 0xbadc - that is, using PDP-11 byte ordering.

Warning
The final operation following a series of writes to a bit stream should be a call to flush to ensure any remaining bits in the buffer are written to the underlying stream. Any unused bits in the data block are set to zero.
Template Parameters
Streamstream type
Blockblock type

Constructor & Destructor Documentation

template<typename Stream, typename Block>
framework::serializable::bit_stream< Stream, Block >::bit_stream ( Stream &  s)
inline

Wrapper constructor.

Warning
The lifetime of stream must exceed this class; behaviour when this condition is not met is undefined.

Member Function Documentation

template<typename Stream, typename Block>
bool framework::serializable::bit_stream< Stream, Block >::flush ( )
inline

Flush output stream.

Writes any remaining bits in the output buffer to the underlying stream.

Returns
true on success, false on failure
template<typename Stream, typename Block>
template<std::size_t N, typename T >
bool framework::serializable::bit_stream< Stream, Block >::read_bits ( T &  t)
inline

Read bits.

Reads a sequence of bits into the low-order N bits of the provided value.

Parameters
toutput container
Template Parameters
Nnumber of bits
Returns
true on success, false on failure
template<typename Stream, typename Block>
template<typename T >
bool framework::serializable::bit_stream< Stream, Block >::read_bits ( T &  t,
std::size_t  n 
)
inline

Read bits.

Reads a sequence of bits into the low-order n bits of the provided value.

Parameters
toutput container
nnumber of bits
Returns
true on success, false on failure
template<typename Stream, typename Block>
template<std::size_t N, typename T >
bool framework::serializable::bit_stream< Stream, Block >::write_bits ( T const &  t)
inline

Write bits.

Writes the low-order N bits of the provided value to the bit stream.

Parameters
tinput container
Template Parameters
Nnumber of bits
Returns
true on success, false on failure
template<typename Stream, typename Block>
template<typename T >
bool framework::serializable::bit_stream< Stream, Block >::write_bits ( T const &  t,
std::size_t  n 
)
inline

Write bits.

Writes the low-order n bits of the provided value to the bit stream.

Parameters
tinput container
nnumber of bits
Returns
true on success, false on failure

The documentation for this class was generated from the following file: