MCS  0.3.3-alpha7
All Classes Namespaces Files Functions Variables Enumerations Enumerator Macros Pages
mcs::Buffer Class Reference

High level buffer. More...

#include <mcs.hh>

Public Member Functions

 Buffer (enum BufferFreeOnDestroy freeBuffer=AUTO_FREE)
 Constructor, buffer allocation is handled internally. More...
 
 Buffer (void *extbuf, unsigned int size, enum BufferFreeOnDestroy freeBuffer=DONT_FREE)
 Constructor, buffer allocation handled by user. More...
 
 Buffer (const Buffer &)
 Declared to avoid using of default copy constructor. More...
 
void free ()
 Free allocated buffer. More...
 
 operator void * () const
 
Bufferoperator() (unsigned int start, unsigned int len)
 Select a "window" in the buffer. More...
 
Bufferoperator() (unsigned int len)
 Select a "window" in the buffer. More...
 
Bufferoperator<< (const void *extbuf)
 Copy data from an external buffer. More...
 
Bufferoperator<< (istream &stream)
 
Bufferoperator= (const Buffer &)
 Declared to avoid using of default assignment operator. More...
 
Bufferoperator>> (void *extbuf)
 Copy data into an external buffer. More...
 
Bufferoperator>> (ostream &stream)
 
char * operator[] (unsigned int pos)
 Use the Buffer class an an array of pointers to char. More...
 
void resize (unsigned int size)
 Check size, and eventually enlarge allocated memory. More...
 
void set (void *extbuf, unsigned int size, enum BufferFreeOnDestroy freeBuffer)
 
unsigned int size ()
 Return size of the buffer. More...
 
 ~Buffer ()
 Destructor, will free the buffer if "freeBuffer" is true. More...
 

Private Attributes

char * buf
 Pointer to internal buffer. More...
 
unsigned int bufsize
 Size of the buffer. More...
 
bool extbuffer
 Tell if we are handling a user allocated buffer. More...
 
enum BufferFreeOnDestroy freebuffer
 Tell if the buffer should be freed once the object is destroyed. More...
 
bool select
 If a window has been selected. More...
 
unsigned int wlen
 Length of the window. More...
 
unsigned int wstart
 Beginning of the window (0-based). More...
 

Detailed Description

High level buffer.

This class provide a buffer with several facilities:

  • dynamical allocation and resizing;
  • Read/write using ">>" and "<<" operators.
  • Availability of the "[ ]" operator to use the object as a simple array of pointers to char.
  • Append to the end of the buffer.

This class can be used in two different way, each corresponding to a different constructor:

  • with a internal dynamically allocated buffer;
  • with a buffer provided by user;

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 1095 of file mcs.hh.

Constructor & Destructor Documentation

◆ Buffer() [1/3]

mcs::Buffer::Buffer ( enum BufferFreeOnDestroy  freeBuffer = AUTO_FREE)

Constructor, buffer allocation is handled internally.

Parameters
freeBufferIf true the buffer will be freed once the object is destroyed, otherwise the buffer must be freed by the user.

Definition at line 28 of file Record.cc.

◆ Buffer() [2/3]

mcs::Buffer::Buffer ( void *  extbuf,
unsigned int  size,
enum BufferFreeOnDestroy  freeBuffer = DONT_FREE 
)

Constructor, buffer allocation handled by user.

Parameters
extbufPointer to the buffer;
sizeSize of the buffer being pointed by "extbuf".

Definition at line 38 of file Record.cc.

◆ Buffer() [3/3]

mcs::Buffer::Buffer ( const Buffer )

Declared to avoid using of default copy constructor.

Warning
This constructor is declared but not implemented. If you try to use it you will get a compilation error.

◆ ~Buffer()

mcs::Buffer::~Buffer ( )

Destructor, will free the buffer if "freeBuffer" is true.

Definition at line 64 of file Record.cc.

Member Function Documentation

◆ free()

void mcs::Buffer::free ( )

Free allocated buffer.

If we are using an internal buffer (i.e. the Buffer(num BufferFreeOnDestroy) constructor has been used) then the internally allocated buffer will be freed.

Definition at line 68 of file Record.cc.

◆ operator()() [1/2]

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.

Parameters
startBeginning of the window (0-based);
lenLength of the window.

Definition at line 109 of file Record.cc.

◆ operator()() [2/2]

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.

Definition at line 101 of file Record.cc.

◆ operator<<()

Buffer & mcs::Buffer::operator<< ( const void *  extbuf)

Copy data from an external buffer.

Data are copied into the selected window on internal buffer.

Parameters
extbufThe pointer to the external buffer from which data are to be copied.

Definition at line 118 of file Record.cc.

◆ operator=()

Buffer& mcs::Buffer::operator= ( const Buffer )

Declared to avoid using of default assignment operator.

Warning
This operator is declared but not implemented. If you try to use it you will get a compilation error.

◆ operator>>()

Buffer & mcs::Buffer::operator>> ( void *  extbuf)

Copy data into an external buffer.

Data are copied into the external buffer from the selected window on internal buffer.

Parameters
extbufThe pointer to the external buffer to which data are to be copied.

Definition at line 165 of file Record.cc.

◆ operator[]()

char * mcs::Buffer::operator[] ( unsigned int  pos)

Use the Buffer class an an array of pointers to char.

Definition at line 199 of file Record.cc.

◆ resize()

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.

Parameters
Sizeof the buffer.

Definition at line 81 of file Record.cc.

◆ size()

unsigned int mcs::Buffer::size ( )

Return size of the buffer.

Definition at line 208 of file Record.cc.

Member Data Documentation

◆ buf

char* mcs::Buffer::buf
private

Pointer to internal buffer.

Definition at line 1098 of file mcs.hh.

◆ bufsize

unsigned int mcs::Buffer::bufsize
private

Size of the buffer.

Definition at line 1101 of file mcs.hh.

◆ extbuffer

bool mcs::Buffer::extbuffer
private

Tell if we are handling a user allocated buffer.

Definition at line 1107 of file mcs.hh.

◆ freebuffer

enum BufferFreeOnDestroy mcs::Buffer::freebuffer
private

Tell if the buffer should be freed once the object is destroyed.

Definition at line 1104 of file mcs.hh.

◆ select

bool mcs::Buffer::select
private

If a window has been selected.

Definition at line 1110 of file mcs.hh.

◆ wlen

unsigned int mcs::Buffer::wlen
private

Length of the window.

Definition at line 1116 of file mcs.hh.

◆ wstart

unsigned int mcs::Buffer::wstart
private

Beginning of the window (0-based).

Definition at line 1113 of file mcs.hh.


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

mcslogo

MCS (My Customizable Server) ver. 0.3.3-alpha7
Documentation generated on Mon May 28 07:39:41 UTC 2018