mcs::Dynamic_Array< BASE > Class Template Reference

#include <mcs.hh>

Inheritance diagram for mcs::Dynamic_Array< BASE >:

Inheritance graph
[legend]

List of all members.


Detailed Description

template<class BASE>
class mcs::Dynamic_Array< BASE >

A thread safe, template class to handle an array of objects.

This class is thread safe in the sense that it can be synchronized betweem different thread, using the parent class Synchro. This class can be used as an array (using the bracket operator []) or like a FIFO structure (using the push(), pop(), peek() methods).

Definition at line 3828 of file mcs.hh.


Public Member Functions

void clear ()
 Empty the array.
int count ()
 Returns number of elements in the array.
 Dynamic_Array (const Dynamic_Array &)
 Declared to avoid using of default copy constructor.
 Dynamic_Array (bool synchro)
 Constructor with optional synchronization.
Dynamic_Arrayoperator= (Dynamic_Array &from)
 Assignment operator.
BASE & operator[] (int pos)
 The operator[] for the array.
BASE peek (int pos)
 Retrieve an element from the array but don't extract it.
BASE pop (int pos=0)
 Extracts an element from the array.
void push (BASE &d)
 Push a copy of an existing element at the end of the array.
void push (BASE *d)
 Push an existing element at the end of the array.
bool ready ()
 Tells if there are objects in the array.
 ~Dynamic_Array ()
 Destructor, this also destroy all objects in the array.

Protected Attributes

Dynamic_Array< BASE > & array

Private Member Functions

void check_allocation (int count)
 Alloc necessary memory to store data in the array.

Private Attributes

BASE ** arr
 Array of pointer to template objects.
int lcount
 How many elements are in the array.
int step_alloc
 How many blocks are allocated.

Constructor & Destructor Documentation

template<class BASE>
mcs::Dynamic_Array< BASE >::Dynamic_Array ( bool  synchro  )  [inline]

Constructor with optional synchronization.

Parameters:
synchro If true the array will use synchronization.

Definition at line 4027 of file mcs.hh.

template<class BASE>
mcs::Dynamic_Array< BASE >::Dynamic_Array ( const Dynamic_Array< BASE > &   ) 

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.

template<class BASE>
mcs::Dynamic_Array< BASE >::~Dynamic_Array (  )  [inline]

Destructor, this also destroy all objects in the array.

Definition at line 4038 of file mcs.hh.


Member Function Documentation

template<class BASE>
void mcs::Dynamic_Array< BASE >::check_allocation ( int  count  )  [inline, private]

Alloc necessary memory to store data in the array.

Check that the current allocated memeory is big enough to contain as many pointer to template objects as passed in parameter. If the memory is not enough it realloc the memory to make it bigger.

Definition at line 4007 of file mcs.hh.

template<class BASE>
void mcs::Dynamic_Array< BASE >::clear (  )  [inline]

Empty the array.

This destroy all objects in the array.

Definition at line 4145 of file mcs.hh.

template<class BASE>
int mcs::Dynamic_Array< BASE >::count (  )  [inline]

Returns number of elements in the array.

Returns:
Number of elements in the array.

Reimplemented from mcs::Synchro.

Definition at line 4133 of file mcs.hh.

template<class BASE>
mcs::Dynamic_Array< BASE > & mcs::Dynamic_Array< BASE >::operator= ( Dynamic_Array< BASE > &  from  )  [inline]

Assignment operator.

Definition at line 4046 of file mcs.hh.

template<class BASE>
BASE & mcs::Dynamic_Array< BASE >::operator[] ( int  pos  )  [inline]

The operator[] for the array.

With this operator you can use the object it was a C-style array. If you require an index out of the range [0..count()] an exception will be thrown.

Note that this method returns a reference to the object, so any modification you made will be stored in the array. Note also that this is not a thread-safe method (the Synchro beyond this object is not locked during the execution of the method). So in a multi-threaded program you should use this method as follows:

    Dynamic_Array q;

    ...

    q.enter();
    q[0] = 1.2,
    q.leave();

Parameters:
x Positional index of the element to be retrieved (0 <= x < count()).
Returns:
A reference to the object.

Definition at line 4107 of file mcs.hh.

template<class BASE>
BASE mcs::Dynamic_Array< BASE >::peek ( int  pos  )  [inline]

Retrieve an element from the array but don't extract it.

This returns a copy of an element from the array. The object is not removed from the array, if you want to remove this object you should use the pop() method. If the queue is empty an exception will be thrown.

Parameters:
x Positional index of the element to be retrieved (0 <= x < count()).
Returns:
A copy of the object extracted from the queue.

Definition at line 4122 of file mcs.hh.

template<class BASE>
BASE mcs::Dynamic_Array< BASE >::pop ( int  pos = 0  )  [inline]

Extracts an element from the array.

The object is also removed from the array, if you want to maintain this object you should use the operator[] or the peek() method. If the array is empty an exception will be thrown.

Parameters:
x Positional index of the element to be retrieved (0 <= x < count()).
Returns:
The element object extracted from the array.

Definition at line 4087 of file mcs.hh.

template<class BASE>
void mcs::Dynamic_Array< BASE >::push ( BASE &  d  )  [inline]

Push a copy of an existing element at the end of the array.

Passing a reference you are really storing in the array a copy of your object, so the warning given in push(BASE*) doesn't apply here.

Parameters:
d Reference to an element object.

Definition at line 4069 of file mcs.hh.

template<class BASE>
void mcs::Dynamic_Array< BASE >::push ( BASE *  d  )  [inline]

Push an existing element at the end of the array.

This method stores the address passed as parameter in its internal structure.

Parameters:
d Address of the object to push in the array.
Warning:
The object whose address is passed as parameter will be automatically deleted from the destructor of Dynamic_Array. So this object must have been allocated in the heap and you must not destroy it, otherwise errors like "double free or corruption" or segmentation fault can occur. A typical misusing of this method is like the following:
    Data* aa = new Data(1);

    Dynamic_Array<Data> vec1;
    Dynamic_Array<Data> vec2;

    vec1.push(aa);
    vec2.push(aa);

In this case two Dynamic_Arrays have the same object stored in their internal structure, so when these arrays will be destroyed the object will be destroyed twice.

Definition at line 4058 of file mcs.hh.

template<class BASE>
bool mcs::Dynamic_Array< BASE >::ready (  )  [inline]

Tells if there are objects in the array.

Note that in a multithreaded environment it is not sure that if this method returns true a call to pop() or the operator[] methods will really return an object. That's because in the meanwhile another thread can modify the array content.

Returns:
true if there are objects in the queue, false otherwise.

Definition at line 4139 of file mcs.hh.


Member Data Documentation

template<class BASE>
BASE** mcs::Dynamic_Array< BASE >::arr [private]

Array of pointer to template objects.

Definition at line 3831 of file mcs.hh.

template<class BASE>
int mcs::Dynamic_Array< BASE >::lcount [private]

How many elements are in the array.

Definition at line 3837 of file mcs.hh.

template<class BASE>
int mcs::Dynamic_Array< BASE >::step_alloc [private]

How many blocks are allocated.

Definition at line 3834 of file mcs.hh.


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

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