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.
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.
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.
Method to get a pointer to the buffer.
Method to get the size of the buffer.
unsigned long int
fill (char *src, unsigned long int size)
Private Member Functions
Private Attributes
Current size of the buffer.
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
(
unsigned long int
size )
explicit
Constructor. It checks size and allocates memory.
- Parameters
-
size size of the buffer
- Exceptions
-
invalid_argument in case of wrong size
Definition at line 34 of file Buffer.cpp.
{
if (size == 0)
throw std::invalid_argument("Buffer with size 0");
else
}
Destructor. It deallocates memory.
Definition at line 46 of file Buffer.cpp.
Member Function Documentation
Method to compare two buffers' content.
It compares the content of this buffer with the content of another buffer
- Parameters
-
b the buffer against whose content it must be compared
size size of bytes to be compared
- Returns
- true if the contents match, false otherwise
- Exceptions
-
out_of_range in 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
-
s pointer to the memory address against whose content it must be compared
size size of bytes to be compared
- Returns
- true if the contents match, false otherwise
- Exceptions
-
out_of_range in case the given size is greater than the buffer buffers
Definition at line 142 of file Buffer.cpp.
{
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
-
src source of the content used to fill the buffer
size number of bytes to be copied
- Returns
- number of bytes copied
- Exceptions
-
out_of_range in case the size is greater than the size of the buffer
invalid_argument in case the source points to NULL
Definition at line 79 of file Buffer.cpp.
{
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
-
b pointer to the buffer whose data must be used to fill this buffer
size size 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;
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.
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
-
p position in the buffer
- Returns
- the byte at the specified position
- Exceptions
-
out_of_range in case the position is out of boundary
Definition at line 60 of file Buffer.cpp.
{
throw std::out_of_range("Operation on buffer out of boundary");
else
}
Member Data Documentation
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: