#include <mcs.hh>
This class provide a buffer with several facilities:
This class can be used in two different way, each corresponding to a different constructor:
In the first case the allocation is performed automatically by the object, in the second case user must provide the pointer to the buffer and its size.
An example of its usage follows:
Buffer buffer(true); //The buffer will be freed when the object //is destroyed. //Insert some data into the buffer. buffer(5) << "hello"; //Note that we passed in circle brackets the //length of the data being inserted. //Append data. buffer(20) << ", my name is Giorgio"; //Modify the first 5 bytes. buffer(0, 5) << "HELLO"; //Note that unitl now we didn't wrote a terminating NULL character //in the buffer. This is needed only if we want to use the Buffer //object as a simple array of chars. This can be done in the //following way (note that we passed 2 as data length, one for the //character and one for the NULL): buffer(2) << "!"; //Now simply print out the buffer content starting from the first //character. cout << buffer[0] << endl; //Print starting from the 8-th character. cout << buffer[7] << endl; //Copy from the 19-th character to the 25-th into another buffer. char tmp[100]; buffer(18, 7) >> tmp; cout << tmp << endl; //Copy the entire content. buffer(buffer.size()) >> tmp; cout << tmp << endl;
Definition at line 1136 of file mcs.hh.
Public Member Functions | |
Buffer (const Buffer &) | |
Declared to avoid using of default copy constructor. | |
Buffer (void *extbuf, unsigned int size, enum BufferFreeOnDestroy freeBuffer=DONT_FREE) | |
Constructor, buffer allocation handled by user. | |
Buffer (enum BufferFreeOnDestroy freeBuffer=AUTO_FREE) | |
Constructor, buffer allocation is handled internally. | |
void | free () |
Free allocated buffer. | |
operator void * () const | |
Buffer & | operator() (unsigned int len) |
Select a "window" in the buffer. | |
Buffer & | operator() (unsigned int start, unsigned int len) |
Select a "window" in the buffer. | |
Buffer & | operator<< (istream &stream) |
Buffer & | operator<< (const void *extbuf) |
Copy data from an external buffer. | |
Buffer & | operator= (const Buffer &) |
Declared to avoid using of default assignment operator. | |
Buffer & | operator>> (ostream &stream) |
Buffer & | operator>> (void *extbuf) |
Copy data into an external buffer. | |
char * | operator[] (unsigned int pos) |
Use the Buffer class an an array of pointers to char. | |
void | resize (unsigned int size) |
Check size, and eventually enlarge allocated memory. | |
void | set (void *extbuf, unsigned int size, enum BufferFreeOnDestroy freeBuffer) |
unsigned int | size () |
Return size of the buffer. | |
~Buffer () | |
Destructor, will free the buffer if "freeBuffer" is true. | |
Private Attributes | |
char * | buf |
Pointer to internal buffer. | |
unsigned int | bufsize |
Size of the buffer. | |
bool | extbuffer |
Tell if we are handling a user allocated buffer. | |
enum BufferFreeOnDestroy | freebuffer |
Tell if the buffer should be freed once the object is destroyed. | |
bool | select |
If a window has been selected. | |
unsigned int | wlen |
Length of the window. | |
unsigned int | wstart |
Beginning of the window (0-based). |
mcs::Buffer::Buffer | ( | enum BufferFreeOnDestroy | freeBuffer = AUTO_FREE |
) |
mcs::Buffer::Buffer | ( | void * | extbuf, | |
unsigned int | size, | |||
enum BufferFreeOnDestroy | freeBuffer = DONT_FREE | |||
) |
mcs::Buffer::Buffer | ( | const Buffer & | ) |
Declared to avoid using of default copy constructor.
mcs::Buffer::~Buffer | ( | ) |
void mcs::Buffer::free | ( | ) |
Buffer & mcs::Buffer::operator() | ( | unsigned int | len | ) |
Select a "window" in the buffer.
This operator is similar to operator()(unsigned int, unsigned int) with the exception that in this case the start of the window is assumed to be at the end of the buffer when using the "<<" operator, at the beginning when using the ">>" operator.
Buffer & mcs::Buffer::operator() | ( | unsigned int | start, | |
unsigned int | len | |||
) |
Select a "window" in the buffer.
A "window" is a part of the buffer which is to be read or written using a succesive call to the "<<" or ">>" operators.
This operator is used to define such a window.
start | Beginning of the window (0-based); | |
len | Length of the window. |
Buffer & mcs::Buffer::operator<< | ( | const void * | extbuf | ) |
Declared to avoid using of default assignment operator.
Buffer & mcs::Buffer::operator>> | ( | void * | extbuf | ) |
char * mcs::Buffer::operator[] | ( | unsigned int | pos | ) |
void mcs::Buffer::resize | ( | unsigned int | size | ) |
Check size, and eventually enlarge allocated memory.
If the buffer is handled internally then this method will check if it has enough space to hold "size" bytes, if not it reallocates the buffer to a bigger size.
If the buffer used is handled by the user then it perform the same check but if the size is not enough an exception will be thrown.
You usually don't need to call this method since it is automatically called by operators.
Size | of the buffer. |
unsigned int mcs::Buffer::size | ( | ) |
char* mcs::Buffer::buf [private] |
unsigned int mcs::Buffer::bufsize [private] |
bool mcs::Buffer::extbuffer [private] |
enum BufferFreeOnDestroy mcs::Buffer::freebuffer [private] |
bool mcs::Buffer::select [private] |
unsigned int mcs::Buffer::wlen [private] |
unsigned int mcs::Buffer::wstart [private] |
![]() |
MCS (My Customizable Server) ver. 0.3.3-alpha3
|