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  )  [inline]

Constructor.

Parameters:
f The stream in which we read.

Definition at line 38 of file bit_istream.tpp.

00039   : m_stream(f), m_pending(0), m_pending_length(0)
00040 {
00041 
00042 } // bit_istream::bit_istream()


Member Function Documentation

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

Tell if the input stream is still valid.

Definition at line 93 of file bit_istream.tpp.

References claw::bit_istream< Stream >::m_pending_length, and claw::bit_istream< Stream >::m_stream.

00094 {
00095   return m_stream || (m_pending_length > 0);
00096 } // bit_istream::operator bool()

template<typename Stream >
void claw::bit_istream< Stream >::read ( char *  buf,
unsigned int  n 
) [inline]

Read some bits.

Parameters:
buf A buffer in which we write the bits.
n The number of bits to read.

Definition at line 51 of file bit_istream.tpp.

References claw::bit_istream< Stream >::m_pending, claw::bit_istream< Stream >::m_pending_length, and claw::bit_istream< Stream >::m_stream.

00052 {
00053   if ( n == 0 )
00054     return;
00055 
00056   unsigned int cur_size = 0;
00057 
00058   while ( (n != 0) && !!(*this) )
00059     {
00060       while( (m_pending_length != 0) && (n!=0) && !!(*this) )
00061         {
00062           unsigned int bits = std::min((unsigned int)m_pending_length, n);
00063 
00064           if ( CHAR_BIT - cur_size < bits )
00065             bits = CHAR_BIT - cur_size;
00066 
00067           unsigned int mask = (1 << bits) - 1;
00068 
00069           *buf |= (m_pending & mask) << cur_size;
00070           cur_size += bits;
00071           m_pending_length -= bits;
00072           m_pending >>= bits;
00073           n -= bits;
00074 
00075           if ( cur_size == CHAR_BIT )
00076             {
00077               ++buf;
00078               cur_size = 0;
00079             }
00080         }
00081 
00082       if ( m_pending_length == 0 )
00083         if ( m_stream.read( (char*)&m_pending, sizeof(m_pending) ) )
00084           m_pending_length = CHAR_BIT;
00085     }
00086 } // 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.

Referenced by claw::bit_istream< Stream >::read().

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.

Referenced by claw::bit_istream< Stream >::operator bool(), and claw::bit_istream< Stream >::read().

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.

Referenced by claw::bit_istream< Stream >::operator bool(), and claw::bit_istream< Stream >::read().


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

Generated on 9 Nov 2009 for CLAW Library (a C++ Library Absolutely Wonderful) by  doxygen 1.6.1