#include <mcs.hh>
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_Array & | operator= (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. |
mcs::Dynamic_Array< BASE >::Dynamic_Array | ( | bool | synchro | ) | [inline] |
mcs::Dynamic_Array< BASE >::Dynamic_Array | ( | const Dynamic_Array< BASE > & | ) |
Declared to avoid using of default copy constructor.
mcs::Dynamic_Array< BASE >::~Dynamic_Array | ( | ) | [inline] |
void mcs::Dynamic_Array< BASE >::check_allocation | ( | int | count | ) | [inline, private] |
void mcs::Dynamic_Array< BASE >::clear | ( | ) | [inline] |
int mcs::Dynamic_Array< BASE >::count | ( | ) | [inline] |
Returns number of elements in the array.
Reimplemented from mcs::Synchro.
mcs::Dynamic_Array< BASE > & mcs::Dynamic_Array< BASE >::operator= | ( | Dynamic_Array< BASE > & | from | ) | [inline] |
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();
x | Positional index of the element to be retrieved (0 <= x < count()). |
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.
x | Positional index of the element to be retrieved (0 <= x < count()). |
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.
x | Positional index of the element to be retrieved (0 <= x < count()). |
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.
d | Reference to an element object. |
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.
d | Address of the object to push in the array. |
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.
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.
BASE** mcs::Dynamic_Array< BASE >::arr [private] |
int mcs::Dynamic_Array< BASE >::lcount [private] |
int mcs::Dynamic_Array< BASE >::step_alloc [private] |
![]() |
MCS (My Customizable Server) ver. 0.3.3-alpha3
|