This class is made to help writing datas of custom bit length. More...
#include <bit_ostream.hpp>
Public Member Functions | |
bit_ostream (stream_type &f) | |
Constructor. | |
~bit_ostream () | |
Destructor. | |
void | write (const char *buf, unsigned int n) |
Write some bits. | |
Private Types | |
typedef Stream | stream_type |
The type of the stream we will write. | |
Private Attributes | |
stream_type & | m_stream |
The stream we're reading. | |
unsigned char | m_pending |
Some bits available for writing. | |
unsigned char | m_pending_length |
The number of valid bits in m_pending. |
This class is made to help writing datas of custom bit length.
Definition at line 40 of file bit_ostream.hpp.
typedef Stream claw::bit_ostream< Stream >::stream_type [private] |
The type of the stream we will write.
Definition at line 44 of file bit_ostream.hpp.
claw::bit_ostream< Stream >::bit_ostream | ( | stream_type & | f ) |
Constructor.
f | The stream in which we write. |
Definition at line 38 of file bit_ostream.tpp.
: m_stream(f), m_pending(0), m_pending_length(0) { } // bit_ostream::bit_ostream()
claw::bit_ostream< Stream >::~bit_ostream | ( | ) |
Destructor.
Definition at line 49 of file bit_ostream.tpp.
References claw::bit_ostream< Stream >::write().
{ if (m_pending_length != 0) m_stream.write( (char*)&m_pending, sizeof(m_pending) ); } // bit_ostream::~bit_ostream()
void claw::bit_ostream< Stream >::write | ( | const char * | buf, |
unsigned int | n | ||
) |
Write some bits.
buf | A buffer from which we read the bits. |
n | The number of bits to write. |
Definition at line 62 of file bit_ostream.tpp.
Referenced by claw::bit_ostream< Stream >::~bit_ostream().
{ if ( n == 0 ) return; unsigned int cur_size = 0; unsigned char data = *buf; while ( n != 0 ) { while( (m_pending_length != CHAR_BIT) && (n!=0) ) { unsigned int bits = std::min(CHAR_BIT - (unsigned int)m_pending_length, n); if ( CHAR_BIT - cur_size < bits ) bits = CHAR_BIT - cur_size; unsigned int mask = (1 << bits) - 1; m_pending |= (data & mask) << m_pending_length; cur_size += bits; m_pending_length += bits; data >>= bits; n -= bits; if ( (cur_size == CHAR_BIT) && (n!=0) ) { ++buf; cur_size = 0; data = *buf; } } if ( m_pending_length == CHAR_BIT ) { m_stream.write( (char*)&m_pending, sizeof(m_pending) ); m_pending = 0; m_pending_length = 0; } } } // bit_ostream::write()
unsigned char claw::bit_ostream< Stream >::m_pending [private] |
Some bits available for writing.
Definition at line 57 of file bit_ostream.hpp.
unsigned char claw::bit_ostream< Stream >::m_pending_length [private] |
The number of valid bits in m_pending.
Definition at line 60 of file bit_ostream.hpp.
stream_type& claw::bit_ostream< Stream >::m_stream [private] |
The stream we're reading.
Definition at line 54 of file bit_ostream.hpp.