ONPOSIX  2.0
 All Classes Namespaces Files Functions Variables Enumerator Friends Macros Pages
Buffer Class Reference

Very simple buffer with control on overflow. More...

#include <Buffer.hpp>

Public Member Functions

 Buffer (unsigned long int size)
 Constructor. It checks size and allocates memory.
virtual ~Buffer ()
char & operator[] (unsigned long int p)
 Method to access a specific byte of the buffer.
unsigned long int fill (const char *src, unsigned long int size)
 Method to fill the buffer.
unsigned long int fill (Buffer *b, unsigned long int size)
 Method to fill the buffer with the content of another buffer.
bool compare (Buffer *b, unsigned long int size)
 Method to compare two buffers' content.
bool compare (const char *s, unsigned long int size)
 Method to compare the content of the buffer against a buffer in memory.
char * getBuffer ()
 Method to get a pointer to the buffer.
unsigned long int getSize () const
 Method to get the size of the buffer.
unsigned long int fill (char *src, unsigned long int size)

Private Member Functions

 Buffer (const Buffer &)

Private Attributes

unsigned long int size_
 Current size of the buffer.
char * data_
 Pointer to the allocated memory.

Detailed Description

Very simple buffer with control on overflow.

This is a simple buffer, internally allocated as a char buffer with new and delete. With respect to hand-made buffers, it adds the check on boundaries.

Definition at line 35 of file Buffer.hpp.

Constructor & Destructor Documentation

Buffer ( const Buffer )
private
Buffer ( unsigned long int  size)
explicit

Constructor. It checks size and allocates memory.

Parameters
sizesize of the buffer
Exceptions
invalid_argumentin case of wrong size

Definition at line 34 of file Buffer.cpp.

: size_(size)
{
if (size == 0)
throw std::invalid_argument("Buffer with size 0");
else
data_ = new char[size_];
}
~Buffer ( )
virtual

Destructor. It deallocates memory.

Definition at line 46 of file Buffer.cpp.

{
if (size_ != 0)
delete[] data_;
}

Member Function Documentation

bool compare ( Buffer b,
unsigned long int  size 
)

Method to compare two buffers' content.

It compares the content of this buffer with the content of another buffer

Parameters
bthe buffer against whose content it must be compared
sizesize of bytes to be compared
Returns
true if the contents match, false otherwise
Exceptions
out_of_rangein case the given size is greater than the size of one of the two buffers

Definition at line 123 of file Buffer.cpp.

{
if (size > size_ || size > b->getSize())
throw std::out_of_range("Operation on buffer out of boundary");
return !std::memcmp(data_, b->getBuffer(), size);
}

Here is the call graph for this function:

bool compare ( const char *  s,
unsigned long int  size 
)

Method to compare the content of the buffer against a buffer in memory.

It compares the content of this buffer with the content at a specified memory address

Parameters
spointer to the memory address against whose content it must be compared
sizesize of bytes to be compared
Returns
true if the contents match, false otherwise
Exceptions
out_of_rangein case the given size is greater than the buffer buffers

Definition at line 142 of file Buffer.cpp.

{
if (size > size_)
throw std::out_of_range("Operation on buffer out of boundary");
return !memcmp(data_, s, size);
}
unsigned long int fill ( const char *  src,
unsigned long int  size 
)

Method to fill the buffer.

It takes the content from a specified address.

Parameters
srcsource of the content used to fill the buffer
sizenumber of bytes to be copied
Returns
number of bytes copied
Exceptions
out_of_rangein case the size is greater than the size of the buffer
invalid_argumentin case the source points to NULL

Definition at line 79 of file Buffer.cpp.

{
if (size > size_)
throw std::out_of_range("Operation on buffer out of boundary");
else if (src == 0)
throw std::invalid_argument("Attempt to copy from NULL pointer");
else if (size == 0)
return 0;
std::memcpy (data_, src, size);
return size;
}

Here is the caller graph for this function:

unsigned long int fill ( Buffer b,
unsigned long int  size 
)

Method to fill the buffer with the content of another buffer.

The number of bytes copied is the minimum between the size of the buffer and the size provided as argument.

Parameters
bpointer to the buffer whose data must be used to fill this buffer
sizesize of bytes to be copied
Returns
number of bytes copied
See Also
Buffer::fill(const char* src, unsigned long int size)

Definition at line 101 of file Buffer.cpp.

{
unsigned long int min;
if (size < b->getSize())
min = size;
else
min = b->getSize();
return fill (b->getBuffer(), min);
}

Here is the call graph for this function:

unsigned long int fill ( char *  src,
unsigned long int  size 
)
inline

Definition at line 76 of file Buffer.hpp.

{
return fill (reinterpret_cast<const char*> (src), size);
}

Here is the call graph for this function:

char* getBuffer ( )
inline

Method to get a pointer to the buffer.

Returns
position of the first byte

Definition at line 63 of file Buffer.hpp.

{
return reinterpret_cast<char*> (data_);
}

Here is the caller graph for this function:

unsigned long int getSize ( ) const
inline

Method to get the size of the buffer.

Returns
Size of the buffer

Definition at line 72 of file Buffer.hpp.

{
return size_;
}

Here is the caller graph for this function:

char & operator[] ( unsigned long int  p)

Method to access a specific byte of the buffer.

It checks the argument and throws an exception in case of out of range.

Parameters
pposition in the buffer
Returns
the byte at the specified position
Exceptions
out_of_rangein case the position is out of boundary

Definition at line 60 of file Buffer.cpp.

{
if (p > (size_ - 1))
throw std::out_of_range("Operation on buffer out of boundary");
else
return data_[p];
}

Member Data Documentation

char* data_
private

Pointer to the allocated memory.

Definition at line 44 of file Buffer.hpp.

unsigned long int size_
private

Current size of the buffer.

Definition at line 39 of file Buffer.hpp.


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