MCS
0.3.3-alpha7
|
A thread safe, template class to handle an array of objects. More...
#include <mcs.hh>
Public Member Functions | |
void | clear () |
Empty the array. More... | |
int | count () |
Returns number of elements in the array. More... | |
Dynamic_Array (bool synchro) | |
Constructor with optional synchronization. More... | |
Dynamic_Array (const Dynamic_Array &) | |
Declared to avoid using of default copy constructor. More... | |
Dynamic_Array & | operator= (Dynamic_Array &from) |
Assignment operator. More... | |
BASE & | operator[] (int pos) |
The operator[] for the array. More... | |
BASE | peek (int pos) |
Retrieve an element from the array but don't extract it. More... | |
BASE | pop (int pos=0) |
Extracts an element from the array. More... | |
void | push (BASE *d) |
Push an existing element at the end of the array. More... | |
void | push (BASE &d) |
Push a copy of an existing element at the end of the array. More... | |
bool | ready () |
Tells if there are objects in the array. More... | |
~Dynamic_Array () | |
Destructor, this also destroy all objects in the array. More... | |
![]() | |
int | count () |
Return how many times the current thread has locked the section. More... | |
bool | enter (int op=1, unsigned int timeout=0) |
Enter, or try to enter a critical section. More... | |
int | leave () |
Leave a critical section. More... | |
Synchro & | operator= (const Synchro &) |
Declared to avoid using of default assignment operator. More... | |
Synchro (const Synchro &) | |
Declared to avoid using of default copy constructor. More... | |
Synchro () | |
Constructor. More... | |
void | synchronize (bool setactive) |
Enable or disable the synchronization feature. More... | |
bool | tryenter (unsigned int timeout=0) |
Wrapper around enter(), using op=MCS_SYNCHRO_TRY_LOCK if the parameter is 0, op=MCS_SYNCHRO_TRY_TIMED otherwise. More... | |
~Synchro () | |
Destructor. More... | |
Protected Attributes | |
Dynamic_Array< BASE > & | array |
Private Member Functions | |
void | check_allocation (int count) |
Alloc necessary memory to store data in the array. More... | |
Private Attributes | |
BASE ** | arr |
Array of pointer to template objects. More... | |
int | lcount |
How many elements are in the array. More... | |
int | step_alloc |
How many blocks are allocated. More... | |
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).
mcs::Dynamic_Array< BASE >::Dynamic_Array | ( | bool | synchro | ) |
mcs::Dynamic_Array< BASE >::Dynamic_Array | ( | const Dynamic_Array< BASE > & | ) |
Declared to avoid using of default copy constructor.
mcs::Dynamic_Array< BASE >::~Dynamic_Array | ( | ) |
|
private |
void mcs::Dynamic_Array< BASE >::clear | ( | ) |
int mcs::Dynamic_Array< BASE >::count | ( | ) |
mcs::Dynamic_Array< BASE > & mcs::Dynamic_Array< BASE >::operator= | ( | Dynamic_Array< BASE > & | from | ) |
BASE & mcs::Dynamic_Array< BASE >::operator[] | ( | int | pos | ) |
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:
x | Positional index of the element to be retrieved (0 <= x < count()). |
BASE mcs::Dynamic_Array< BASE >::peek | ( | int | pos | ) |
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 | ) |
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 | ) |
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. |
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.
void mcs::Dynamic_Array< BASE >::push | ( | BASE & | d | ) |
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. |
bool mcs::Dynamic_Array< BASE >::ready | ( | ) |
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.
|
private |
|
private |
|
private |
![]() |
MCS (My Customizable Server) ver. 0.3.3-alpha7
|