C12Adapter Opensource C++ Interface
MBufferReader Class Reference

Buffer traverser or reader. More...

Public Member Functions

 MBufferReader ()
 Create an empty reader. More...
 
 MBufferReader (MBuffer *buffer)
 Create a reader for a given buffer. More...
 
 MBufferReader (MBuffer *buffer, unsigned readPosition, unsigned readEnd)
 Create a reader for a given buffer at a given position and size. More...
 
 MBufferReader (const MBufferReader &other)
 Create a reader from a given copy. More...
 
 ~MBufferReader ()
 Destructor.
 
void AssignBuffer (MBuffer *buffer)
 Assign the buffer that is to be read. More...
 
void AssignBuffer (MBuffer *buffer, unsigned readPosition, unsigned readEnd)
 Assign a reader for a given buffer at a given position and end. More...
 
const char * GetTotalPtr () const
 Access constant pointer of the whole buffer, regardless of the reader position.
 
unsigned GetTotalSize () const
 Access the size of the whole buffer, regardless of the reader position.
 
char * GetReadPtr ()
 Access the pointer to data to which the current position of the reader points.
 
const char * GetReadPtr () const
 Access the constant pointer to data to which the current position of the reader points.
 
unsigned GetRemainingReadSize () const
 The number of bytes left to read to reach the end of the reader.
 
void IgnoreBytes (unsigned pos)
 Traverse the reader current position by ignoring the given number of bytes. More...
 
void ReadBuffer (Muint8 *data, unsigned size)
 Read characters from the buffer into the given pointer and size. More...
 
template<typename T >
void Read (T &result)
 Read a typed value from the buffer, return the value. More...
 
template<typename T >
Read ()
 Read a typed value from the buffer, return the value. More...
 
void ReadBuffer (char *data, unsigned size)
 Read bytes from the buffer into the given pointer and size. More...
 
Muint8 ReadByte ()
 Read a byte from the buffer. More...
 
void ReadBytes (unsigned size, MByteString &result)
 Read bytes from the buffer into the given result. More...
 
void ReadRemainingBytes (MByteString &result)
 Read all remaining bytes from the buffer into the given result. More...
 
unsigned ReadIsoLength ()
 Read an ISO 8825 length BER representation from this buffer. More...
 
unsigned GetReadPosition () const
 
void SetReadPosition (unsigned pos)
 
unsigned GetEndPosition () const
 
void SetEndPosition (unsigned pos)
 

Protected Attributes

MBufferm_buff
 Client buffer object, not owned by this class.
 
unsigned m_readPosition
 Current read position within the buffer.
 
unsigned m_readEnd
 End position within the buffer, possibly smaller than the buffer size.
 

Detailed Description

Buffer traverser or reader.

Uses buffer object to walk through it sequentially. The buffer reader has

  • Buffer object pointer, the one from which it reads bytes
  • Read position, current offset for the next bytes to be read. Read position should fit within the buffer object.
  • End position, offset where the read should stop. End position should not be smaller than read position, but can be smaller than the buffer size.

One buffer can be used by multiple readers, each having its own possibly overlapping read position at the end.

Constructor & Destructor Documentation

MBufferReader::MBufferReader ( )
inline

Create an empty reader.

Before use, the buffer has to be assigned to this object with AssignBuffer.

MBufferReader::MBufferReader ( MBuffer buffer)
inline

Create a reader for a given buffer.

The read position is zero, the start of the given buffer. The end position is the end of the buffer, buffer size.

Parameters
bufferBuffer object that will be read from the start.
MBufferReader::MBufferReader ( MBuffer buffer,
unsigned  readPosition,
unsigned  readEnd 
)
inline

Create a reader for a given buffer at a given position and size.

Parameters
bufferBuffer object that will be read from the given position.
readPositionStart position, should be within the span of the buffer, as checked with the debug level assert.
readEndEnd position, should be within the span of the buffer, as checked with the debug level assert.
MBufferReader::MBufferReader ( const MBufferReader other)
inline

Create a reader from a given copy.

The buffer object, as well as the read position and end, are copied and the new object can read the buffer independently.

Parameters
otherBuffer reader from which this buffer is copied.

Member Function Documentation

void MBufferReader::AssignBuffer ( MBuffer buffer)
inline

Assign the buffer that is to be read.

The read position is zero, the start of the given buffer. The end position is the end of the buffer, buffer size. It is okay if this reader was pointing to some other buffer prior to this call.

Parameters
bufferBuffer object that will be read from the start.
void MBufferReader::AssignBuffer ( MBuffer buffer,
unsigned  readPosition,
unsigned  readEnd 
)
inline

Assign a reader for a given buffer at a given position and end.

It is okay if this reader was pointing to some other buffer prior to this call.

Parameters
bufferBuffer object that will be read from the given position.
readPositionStart position, should be within the span of the buffer, as checked with the debug level assert.
readEndEnd position, should be within the span of the buffer, as checked with the debug level assert.
unsigned MBufferReader::GetEndPosition ( ) const
inline

Current end position of the buffer reader.

unsigned MBufferReader::GetReadPosition ( ) const
inline

Current read position of the buffer reader.

void MBufferReader::IgnoreBytes ( unsigned  pos)
inline

Traverse the reader current position by ignoring the given number of bytes.

Only debug level checks are present.

template<typename T >
void MBufferReader::Read ( T &  result)
inline

Read a typed value from the buffer, return the value.

If the type of such size does not fit in the remainder of the buffer, an exception is thrown.

Returns
Value of the given type as read from the buffer.
template<typename T >
T MBufferReader::Read ( )
inline

Read a typed value from the buffer, return the value.

If the type of such size does not fit in the remainder of the buffer, an exception is thrown.

Returns
Value of the given type as read from the buffer.
void MBufferReader::ReadBuffer ( Muint8 *  data,
unsigned  size 
)

Read characters from the buffer into the given pointer and size.

Parameters
dataPointer into which to read bytes.
sizeHow many bytes to read, should not be less than what is available in the data pointer. If the requested size is smaller than what is available to reader, an exception is thrown.
void MBufferReader::ReadBuffer ( char *  data,
unsigned  size 
)
inline

Read bytes from the buffer into the given pointer and size.

Parameters
dataPointer into which to read bytes
sizeHow many bytes to read, should not be less than what is available in the data pointer. If the requested size is smaller than what is available to reader, an exception is thrown.
Muint8 MBufferReader::ReadByte ( )

Read a byte from the buffer.

If the read position is at the end already, an exception is thrown.

Returns
Byte as read from the current position.
void MBufferReader::ReadBytes ( unsigned  size,
MByteString result 
)

Read bytes from the buffer into the given result.

The reason this method takes an 'out' variable is that it is typically kept with some preallocated memory for efficiency of multiple calls.

Parameters
sizeHow many bytes to read, should not be less than what is available in the data pointer. If the requested size is smaller than what is available to reader, an exception is thrown.
resultWrite-only parameter into which the data will be read.
unsigned MBufferReader::ReadIsoLength ( )

Read an ISO 8825 length BER representation from this buffer.

Exception will be thrown if there is not enough bytes in the buffer reader, or if the bytes do not comprise a proper ISO length.

void MBufferReader::ReadRemainingBytes ( MByteString result)

Read all remaining bytes from the buffer into the given result.

The reason this method takes an 'out' variable is that it is typically kept with some preallocated memory for efficiency of multiple calls. After this method is called, the read position will be equal to the end position.

Parameters
resultWrite-only parameter into which the data will be read.
void MBufferReader::SetEndPosition ( unsigned  pos)

Current end position of the buffer reader.

void MBufferReader::SetReadPosition ( unsigned  pos)
inline

Current read position of the buffer reader.