MCS  0.3.3-alpha7
mcs::Dynamic_Array< BASE > Class Template Reference

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

#include <mcs.hh>

+ Inheritance diagram for mcs::Dynamic_Array< BASE >:

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_Arrayoperator= (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...
 
- Public Member Functions inherited from mcs::Synchro
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...
 
Synchrooperator= (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...
 

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

Constructor & Destructor Documentation

◆ Dynamic_Array() [1/2]

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

Constructor with optional synchronization.

Parameters
synchroIf true the array will use synchronization.

Definition at line 4012 of file mcs.hh.

◆ Dynamic_Array() [2/2]

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.

◆ ~Dynamic_Array()

template<class BASE >
mcs::Dynamic_Array< BASE >::~Dynamic_Array ( )

Destructor, this also destroy all objects in the array.

Definition at line 4023 of file mcs.hh.

Member Function Documentation

◆ check_allocation()

template<class BASE >
void mcs::Dynamic_Array< BASE >::check_allocation ( int  count)
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 3992 of file mcs.hh.

◆ clear()

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

Empty the array.

This destroy all objects in the array.

Definition at line 4130 of file mcs.hh.

◆ count()

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

Returns number of elements in the array.

Returns
Number of elements in the array.

Definition at line 4118 of file mcs.hh.

◆ operator=()

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

Assignment operator.

Definition at line 4031 of file mcs.hh.

◆ operator[]()

template<class BASE >
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:

...
q.enter();
q[0] = 1.2,
q.leave();
Parameters
xPositional index of the element to be retrieved (0 <= x < count()).
Returns
A reference to the object.

Definition at line 4092 of file mcs.hh.

◆ peek()

template<class BASE >
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.

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

Definition at line 4107 of file mcs.hh.

◆ pop()

template<class BASE >
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.

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

Definition at line 4072 of file mcs.hh.

◆ push() [1/2]

template<class BASE>
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.

Parameters
dAddress 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 4043 of file mcs.hh.

◆ push() [2/2]

template<class BASE>
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.

Parameters
dReference to an element object.

Definition at line 4054 of file mcs.hh.

◆ ready()

template<class BASE >
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.

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

Definition at line 4124 of file mcs.hh.

Member Data Documentation

◆ arr

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

Array of pointer to template objects.

Definition at line 3816 of file mcs.hh.

◆ lcount

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

How many elements are in the array.

Definition at line 3822 of file mcs.hh.

◆ step_alloc

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

How many blocks are allocated.

Definition at line 3819 of file mcs.hh.


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

mcslogo

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