mcs::Buffer Class Reference

#include <mcs.hh>

List of all members.


Detailed Description

High level buffer.

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
Bufferoperator() (unsigned int len)
 Select a "window" in the buffer.
Bufferoperator() (unsigned int start, unsigned int len)
 Select a "window" in the buffer.
Bufferoperator<< (istream &stream)
Bufferoperator<< (const void *extbuf)
 Copy data from an external buffer.
Bufferoperator= (const Buffer &)
 Declared to avoid using of default assignment operator.
Bufferoperator>> (ostream &stream)
Bufferoperator>> (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).

Constructor & Destructor Documentation

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

Constructor, buffer allocation is handled internally.

Parameters:
freeBuffer If 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.

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

Constructor, buffer allocation handled by user.

Parameters:
extbuf Pointer to the buffer;
size Size of the buffer being pointed by "extbuf".

Definition at line 38 of file Record.cc.

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.

mcs::Buffer::~Buffer (  ) 

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

Definition at line 64 of file Record.cc.


Member Function Documentation

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.

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.

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:
start Beginning of the window (0-based);
len Length of the window.

Definition at line 109 of file Record.cc.

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

Copy data from an external buffer.

Data are copied into the selected window on internal buffer.

Parameters:
extbuf The pointer to the external buffer from which data are to be copied.

Definition at line 118 of file Record.cc.

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.

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:
extbuf The pointer to the external buffer to which data are to be copied.

Definition at line 165 of file Record.cc.

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.

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:
Size of the buffer.

Definition at line 81 of file Record.cc.

unsigned int mcs::Buffer::size (  ) 

Return size of the buffer.

Definition at line 208 of file Record.cc.


Member Data Documentation

char* mcs::Buffer::buf [private]

Pointer to internal buffer.

Definition at line 1139 of file mcs.hh.

unsigned int mcs::Buffer::bufsize [private]

Size of the buffer.

Definition at line 1142 of file mcs.hh.

bool mcs::Buffer::extbuffer [private]

Tell if we are handling a user allocated buffer.

Definition at line 1148 of file mcs.hh.

enum BufferFreeOnDestroy mcs::Buffer::freebuffer [private]

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

Definition at line 1145 of file mcs.hh.

bool mcs::Buffer::select [private]

If a window has been selected.

Definition at line 1151 of file mcs.hh.

unsigned int mcs::Buffer::wlen [private]

Length of the window.

Definition at line 1157 of file mcs.hh.

unsigned int mcs::Buffer::wstart [private]

Beginning of the window (0-based).

Definition at line 1154 of file mcs.hh.


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

MCS (My Customizable Server) ver. 0.3.3-alpha3
Documentation generated on Thu Mar 22 13:22:23 UTC 2012