CLAW Library (a C++ Library Absolutely Wonderful) 1.5.5
Public Member Functions | Private Types | Private Attributes

claw::bit_istream< Stream > Class Template Reference

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

#include <bit_istream.hpp>

List of all members.

Public Member Functions

 bit_istream (stream_type &f)
 Constructor.
void read (char *buf, unsigned int n)
 Read some bits.
 operator bool () const
 Tell if the input stream is still valid.

Private Types

typedef Stream stream_type
 The type of the stream we will read.

Private Attributes

stream_typem_stream
 The stream we're reading.
unsigned char m_pending
 Some bits available for reading.
unsigned char m_pending_length
 The number of valid bits in m_pending.

Detailed Description

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

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

Author:
Julien Jorge

Definition at line 40 of file bit_istream.hpp.


Member Typedef Documentation

template<typename Stream >
typedef Stream claw::bit_istream< Stream >::stream_type [private]

The type of the stream we will read.

Definition at line 44 of file bit_istream.hpp.


Constructor & Destructor Documentation

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

Constructor.

Parameters:
fThe stream in which we read.

Definition at line 38 of file bit_istream.tpp.

  : m_stream(f), m_pending(0), m_pending_length(0)
{

} // bit_istream::bit_istream()

Member Function Documentation

template<typename Stream >
claw::bit_istream< Stream >::operator bool ( ) const

Tell if the input stream is still valid.

Definition at line 93 of file bit_istream.tpp.

{
  return m_stream || (m_pending_length > 0);
} // bit_istream::operator bool()
template<typename Stream >
void claw::bit_istream< Stream >::read ( char *  buf,
unsigned int  n 
)

Read some bits.

Parameters:
bufA buffer in which we write the bits.
nThe number of bits to read.

Definition at line 51 of file bit_istream.tpp.

{
  if ( n == 0 )
    return;

  unsigned int cur_size = 0;

  while ( (n != 0) && !!(*this) )
    {
      while( (m_pending_length != 0) && (n!=0) && !!(*this) )
        {
          unsigned int bits = std::min((unsigned int)m_pending_length, n);

          if ( CHAR_BIT - cur_size < bits )
            bits = CHAR_BIT - cur_size;

          unsigned int mask = (1 << bits) - 1;

          *buf |= (m_pending & mask) << cur_size;
          cur_size += bits;
          m_pending_length -= bits;
          m_pending >>= bits;
          n -= bits;

          if ( cur_size == CHAR_BIT )
            {
              ++buf;
              cur_size = 0;
            }
        }

      if ( m_pending_length == 0 )
        if ( m_stream.read( (char*)&m_pending, sizeof(m_pending) ) )
          m_pending_length = CHAR_BIT;
    }
} // bit_istream::read()

Member Data Documentation

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

Some bits available for reading.

Definition at line 58 of file bit_istream.hpp.

template<typename Stream >
unsigned char claw::bit_istream< Stream >::m_pending_length [private]

The number of valid bits in m_pending.

Definition at line 61 of file bit_istream.hpp.

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

The stream we're reading.

Definition at line 55 of file bit_istream.hpp.


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