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

Constructor.

Parameters:
f The stream in which we write.

Definition at line 38 of file bit_ostream.tpp.

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

template<typename Stream >
claw::bit_ostream< Stream >::~bit_ostream (  )  [inline]

Destructor.

Definition at line 49 of file bit_ostream.tpp.

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

00050 {
00051   if (m_pending_length != 0)
00052     m_stream.write( (char*)&m_pending, sizeof(m_pending) );
00053 } // bit_ostream::~bit_ostream()


Member Function Documentation

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

Write some bits.

Parameters:
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.

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

00063 {
00064   if ( n == 0 )
00065     return;
00066 
00067   unsigned int cur_size = 0;
00068   unsigned char data = *buf;
00069 
00070   while ( n != 0 )
00071     {
00072       while( (m_pending_length != CHAR_BIT) && (n!=0) )
00073         {
00074           unsigned int bits =
00075             std::min(CHAR_BIT - (unsigned int)m_pending_length, n);
00076 
00077           if ( CHAR_BIT - cur_size < bits )
00078             bits = CHAR_BIT - cur_size;
00079 
00080           unsigned int mask = (1 << bits) - 1;
00081 
00082           m_pending |= (data & mask) << m_pending_length;
00083           cur_size += bits;
00084           m_pending_length += bits;
00085           data >>= bits;
00086           n -= bits;
00087 
00088           if ( (cur_size == CHAR_BIT) && (n!=0) )
00089             {
00090               ++buf;
00091               cur_size = 0;
00092               data = *buf;
00093             }
00094         }
00095 
00096       if ( m_pending_length == CHAR_BIT )
00097         {
00098           m_stream.write( (char*)&m_pending, sizeof(m_pending) );
00099           m_pending = 0;
00100           m_pending_length = 0;
00101         }
00102     }
00103 } // 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.

Referenced by claw::bit_ostream< Stream >::write(), and claw::bit_ostream< Stream >::~bit_ostream().

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.

Referenced by claw::bit_ostream< Stream >::write(), and claw::bit_ostream< Stream >::~bit_ostream().

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.

Referenced by claw::bit_ostream< Stream >::write(), and claw::bit_ostream< Stream >::~bit_ostream().


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