The Micro C++ Library
micxx::Buffer Class Reference

A simple generic Buffer that could dynamically grow or shrink. More...

#include <micxx.hxx>

Inherits micxx::traits::ToString.

Inherited by micxx::Datagram.

List of all members.

Public Member Functions

 Buffer (std::size_t size=0)
 Create a new Buffer with initial size and initial capacity set to size.
 Buffer (const Buffer &right)
 Create a copy of Buffer right.
virtual ~Buffer ()
 Delete the internal pointer to the dynamically allocated storage.
virtual void append (const void *address, std::size_t size)
 Appends the address range starting at address to the end of this buffer.
virtual void * getAddress ()
 The address of (or pointer to) the dynamically allocated storage.
virtual const void * getAddress () const
virtual std::size_t getCapacity () const
 The actual size of the dynamically allocated storage.
virtual std::size_t getSize () const
 The currently used size of the available capacity.
virtual unsigned char * operator+ (std::size_t offset)
 The Address of the offset offset into the dynamically allocated storage.
virtual const unsigned char * operator+ (std::size_t offset) const
virtual unsigned char & operator[] (std::size_t offset)
 The byte at offset offset from the dynamically allocated storage.
virtual const unsigned char & operator[] (std::size_t offset) const
virtual Bufferoperator= (const Buffer &right)
 Make this a copy of Buffer right.
virtual std::size_t operator== (const Buffer &right)
 Compare the content from this buffer with right.
template<typename Writer >
Writer & operator>> (Writer &writer) const
 Write the current buffer data (const void*)(*this) with size getSize() / sizeof(Writer::char_type) to the output Writer writer.
template<typename Reader >
Bufferoperator<< (Reader &reader)
 Reads up to getCapacity() / sizeof(Reader::char_type) into this buffer.
virtual operator void * ()
virtual operator const void * () const
virtual void setCapacity (std::size_t capacity)
 Sets the new capacity to capacity.
virtual void setSize (std::size_t size)
 Sets the currently used size to size.
virtual std::string toString () const
 A string representation describing this Buffer.

Protected Attributes

void * address
 The pointer (or address) to the dynamically allocated storage.
std::size_t size
 The current used size of the available capacity.
std::size_t capacity
 The actually available size of the dynamically allocated storage.

Detailed Description

A simple generic Buffer that could dynamically grow or shrink.

A Buffer has

  1. a pointer (or address) to the dynamically allocated storage,
  2. a capacity which is the actual available size of the allocated storage and
  3. a size, which is the current size that is used and which is always less or equal to the capacity.
Author:
Norbert Klose
Date:
May, 2007

Constructor & Destructor Documentation

micxx::Buffer::Buffer ( std::size_t  size = 0) [explicit]

Create a new Buffer with initial size and initial capacity set to size.

Parameters:
sizeThe initial size and capacity.
micxx::Buffer::Buffer ( const Buffer right)

Create a copy of Buffer right.

Parameters:
rightThe Buffer from which to copy.
virtual micxx::Buffer::~Buffer ( ) [virtual]

Delete the internal pointer to the dynamically allocated storage.


Member Function Documentation

virtual void micxx::Buffer::append ( const void *  address,
std::size_t  size 
) [virtual]

Appends the address range starting at address to the end of this buffer.

The size of this buffer is incremented by size before.

virtual void* micxx::Buffer::getAddress ( ) [inline, virtual]

The address of (or pointer to) the dynamically allocated storage.

Returns:
The address to the dynamically allocated storage.
    {
        return address;
    }
virtual const void* micxx::Buffer::getAddress ( ) const [inline, virtual]

The const version of getAddress().

    {
        return address;
    }
virtual std::size_t micxx::Buffer::getCapacity ( ) const [inline, virtual]

The actual size of the dynamically allocated storage.

The capacity will always be greater or equal to the size of a Buffer.

Returns:
The Buffer's capacity.
    {
        return capacity;
    }
virtual std::size_t micxx::Buffer::getSize ( ) const [inline, virtual]

The currently used size of the available capacity.

The size will always be less or equal to the capacity of a Buffer.

Returns:
The Buffer's size.
    {
        return size;
    }
virtual micxx::Buffer::operator const void * ( ) const [inline, virtual]

Same as getAddress() const.

    {
        return address;
    }
virtual micxx::Buffer::operator void * ( ) [inline, virtual]

Same as getAddress().

    {
        return address;
    }
virtual unsigned char* micxx::Buffer::operator+ ( std::size_t  offset) [virtual]

The Address of the offset offset into the dynamically allocated storage.

Parameters:
offsetThe offset into the dynamically allocated storage
Returns:
((unsigned char *)getAddress()) + offset.
virtual const unsigned char* micxx::Buffer::operator+ ( std::size_t  offset) const [virtual]

The const version of operator+(std::size_t offset).

template<typename Reader >
Buffer & micxx::Buffer::operator<< ( Reader &  reader)

Reads up to getCapacity() / sizeof(Reader::char_type) into this buffer.

Note:
setCapacity() should be called before calling this operator. Use getSize() afterwards to get the number of bytes read from the Reader reader.
{
    __MICXXTRY__
    setSize(0);
    std::streamsize num = getCapacity() / sizeof(Reader::char_type);
    if (num)
    {
        std::size_t bytesRead = reader.read(address, num);
        setSize(bytesRead);
    }
    return *this;
    __MICXXEND__
}
virtual Buffer& micxx::Buffer::operator= ( const Buffer right) [virtual]

Make this a copy of Buffer right.

Parameters:
rightThe Buffer from which to copy.
Returns:
*this
virtual std::size_t micxx::Buffer::operator== ( const Buffer right) [virtual]

Compare the content from this buffer with right.

Returns:
0, if both buffers are equal, or
the position of the first byte, where both buffers differ.
template<typename Writer >
Writer & micxx::Buffer::operator>> ( Writer &  writer) const

Write the current buffer data (const void*)(*this) with size getSize() / sizeof(Writer::char_type) to the output Writer writer.

{
    __MICXXTRY__
    std::streamsize num = getSize() / sizeof(typename Writer::char_type);
    if (num)
    {
        writer.write(static_cast<const typename Writer::char_type*>(address), num);
    }
    return writer;
    __MICXXEND__
}
virtual unsigned char& micxx::Buffer::operator[] ( std::size_t  offset) [virtual]

The byte at offset offset from the dynamically allocated storage.

Parameters:
offsetThe offset of the desired byte from the dynamically allocated storage
Returns:
((unsigned char *)getAddress())[ offset ].
virtual const unsigned char& micxx::Buffer::operator[] ( std::size_t  offset) const [virtual]

The const version of operator[](std::size_t offset).

virtual void micxx::Buffer::setCapacity ( std::size_t  capacity) [virtual]

Sets the new capacity to capacity.

If capacity < getSize(), the size is automatically decreased to capacity. The dynamically allocated storage is reallocated (and possibly moved) with the new capacity, causing the Buffer to grow or shrink dynamically.

virtual void micxx::Buffer::setSize ( std::size_t  size) [virtual]

Sets the currently used size to size.

If size > getCapacity(), then setCapacity(std::size_t) is called with size before, causing the Buffer to grow dynamically.

Parameters:
sizeThe new currently used size.
See also:
setCapacity(std::size_t)
virtual std::string micxx::Buffer::toString ( ) const [virtual]

A string representation describing this Buffer.

Reimplemented from micxx::traits::ToString.

Reimplemented in micxx::Datagram.


Member Data Documentation

void* micxx::Buffer::address [protected]

The pointer (or address) to the dynamically allocated storage.

std::size_t micxx::Buffer::capacity [protected]

The actually available size of the dynamically allocated storage.

std::size_t micxx::Buffer::size [protected]

The current used size of the available capacity.


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