Public Member Functions | Private Types | Private Attributes

claw::bit_ostream< Stream > Class Template Reference

This class is made to help writing datas of custom bit length. More...

#include <bit_ostream.hpp>

List of all members.

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_typem_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.

Detailed Description

template<typename Stream>
class claw::bit_ostream< Stream >

This class is made to help writing datas of custom bit length.

Author:
Julien Jorge

Definition at line 40 of file bit_ostream.hpp.


Member Typedef Documentation

template<typename Stream >
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.


Constructor & Destructor Documentation

template<typename Stream >
claw::bit_ostream< Stream >::bit_ostream ( stream_type f )

Constructor.

Parameters:
fThe 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()
template<typename Stream >
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()

Member Function Documentation

template<typename Stream >
void claw::bit_ostream< Stream >::write ( const char *  buf,
unsigned int  n 
)

Write some bits.

Parameters:
bufA buffer from which we read the bits.
nThe 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()

Member Data Documentation

template<typename Stream >
unsigned char claw::bit_ostream< Stream >::m_pending [private]

Some bits available for writing.

Definition at line 57 of file bit_ostream.hpp.

template<typename Stream >
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.

template<typename Stream >
stream_type& claw::bit_ostream< Stream >::m_stream [private]

The stream we're reading.

Definition at line 54 of file bit_ostream.hpp.


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