PTLib  Version 2.10.11
PSafeCollection Class Reference

This class defines a thread-safe collection of objects. More...

#include <safecoll.h>

Inheritance diagram for PSafeCollection:
PObject PSafeColl< Coll, Base > PSafeColl< PArray< Base >, Base > PSafeColl< PList< Base >, Base > PSafeColl< PSortedList< Base >, Base > PSafeDictionaryBase< Coll, Key, Base > PSafeDictionaryBase< PDictionary< Key, Base >, Key, Base > PSafeArray< Base > PSafeList< Base > PSafeSortedList< Base > PSafeDictionary< Key, Base >

Public Member Functions

Construction
 PSafeCollection (PCollection *collection)
 Create a thread safe collection of objects. More...
 
 ~PSafeCollection ()
 Destroy the thread safe collection. More...
 
- Public Member Functions inherited from PObject
virtual ~PObject ()
 
virtual PObjectClone () const
 Create a copy of the class on the heap. More...
 
virtual PINDEX HashFunction () const
 This function yields a hash value required by the PDictionary class. More...
 
virtual const char * GetClass (unsigned ancestor=0) const
 Get the current dynamic type of the object instance. More...
 
PBoolean IsClass (const char *cls) const
 
virtual PBoolean InternalIsDescendant (const char *clsName) const
 Determine if the dynamic type of the current instance is a descendent of the specified class. More...
 
virtual Comparison Compare (const PObject &obj) const
 Compare the two objects and return their relative rank. More...
 
virtual Comparison CompareObjectMemoryDirect (const PObject &obj) const
 Determine the byte wise comparison of two objects. More...
 
bool operator== (const PObject &obj) const
 Compare the two objects. More...
 
bool operator!= (const PObject &obj) const
 Compare the two objects. More...
 
bool operator< (const PObject &obj) const
 Compare the two objects. More...
 
bool operator> (const PObject &obj) const
 Compare the two objects. More...
 
bool operator<= (const PObject &obj) const
 Compare the two objects. More...
 
bool operator>= (const PObject &obj) const
 Compare the two objects. More...
 
virtual void PrintOn (ostream &strm) const
 Output the contents of the object to the stream. More...
 
virtual void ReadFrom (istream &strm)
 Input the contents of the object from the stream. More...
 

Protected Member Functions

void CopySafeCollection (PCollection *other)
 
void CopySafeDictionary (PAbstractDictionary *other)
 
void SafeRemoveObject (PSafeObject *obj)
 
 PDECLARE_NOTIFIER (PTimer, PSafeCollection, DeleteObjectsTimeout)
 
- Protected Member Functions inherited from PObject
 PObject ()
 Constructor for PObject, made protected so cannot ever create one on its own. More...
 

Protected Attributes

PCollectioncollection
 
PMutex collectionMutex
 
bool deleteObjects
 
PList< PSafeObjecttoBeRemoved
 
PMutex removalMutex
 
PTimer deleteObjectsTimer
 

Operations

virtual void RemoveAll (PBoolean synchronous=false)
 Remove all objects in collection. More...
 
void AllowDeleteObjects (PBoolean yes=true)
 Disallow the automatic delete any objects that have been removed. More...
 
void DisallowDeleteObjects ()
 Disallow the automatic delete any objects that have been removed. More...
 
virtual PBoolean DeleteObjectsToBeRemoved ()
 Delete any objects that have been removed. More...
 
virtual void DeleteObject (PObject *object) const
 Delete an objects that has been removed. More...
 
virtual void SetAutoDeleteObjects ()
 Start a timer to automatically call DeleteObjectsToBeRemoved(). More...
 
PINDEX GetSize () const
 Get the current size of the collection. More...
 
PBoolean IsEmpty () const
 Determine if the collection is empty. More...
 
const PMutexGetMutex () const
 Get the mutex for the collection. More...
 
virtual PBoolean SafeRemove (PSafeObject *obj)
 Remove an object to the collection. More...
 
virtual PBoolean SafeRemoveAt (PINDEX idx)
 Remove an object to the collection. More...
 

Additional Inherited Members

- Public Types inherited from PObject
enum  Comparison { LessThan = -1, EqualTo = 0, GreaterThan = 1 }
 Result of the comparison operation performed by the Compare() function. More...
 
- Static Public Member Functions inherited from PObject
static const char * Class ()
 Get the name of the class as a C string. More...
 
static Comparison InternalCompareObjectMemoryDirect (const PObject *obj1, const PObject *obj2, PINDEX size)
 Internal function caled from CompareObjectMemoryDirect() More...
 

Detailed Description

This class defines a thread-safe collection of objects.

This class is a wrapper around a standard PCollection class which allows only safe, mutexed, access to the collection.

This is part of a set of classes to solve the general problem of a collection (eg a PList or PDictionary) of objects that needs to be a made thread safe. Any thread can add, read, write or remove an object with both the object and the database of objects itself kept thread safe.

See the PSafeObject class for more details. Especially in regard to enumeration of collections.

Constructor & Destructor Documentation

§ PSafeCollection()

PSafeCollection::PSafeCollection ( PCollection collection)

Create a thread safe collection of objects.

Note the collection is automatically deleted on destruction.

Parameters
collectionActual collection of objects

§ ~PSafeCollection()

PSafeCollection::~PSafeCollection ( )

Destroy the thread safe collection.

The will delete the collection object provided in the constructor.

Member Function Documentation

§ AllowDeleteObjects()

void PSafeCollection::AllowDeleteObjects ( PBoolean  yes = true)
inline

Disallow the automatic delete any objects that have been removed.

Objects are simply removed from the collection and not marked for deletion using PSafeObject::SafeRemove() and DeleteObject().

Parameters
yesNew value for flag for deleting objects

§ CopySafeCollection()

void PSafeCollection::CopySafeCollection ( PCollection other)
protected

§ CopySafeDictionary()

void PSafeCollection::CopySafeDictionary ( PAbstractDictionary other)
protected

§ DeleteObject()

virtual void PSafeCollection::DeleteObject ( PObject object) const
virtual

Delete an objects that has been removed.

§ DeleteObjectsToBeRemoved()

virtual PBoolean PSafeCollection::DeleteObjectsToBeRemoved ( )
virtual

Delete any objects that have been removed.

Returns true if all objects in the collection have been removed and their pending deletions carried out.

§ DisallowDeleteObjects()

void PSafeCollection::DisallowDeleteObjects ( )
inline

Disallow the automatic delete any objects that have been removed.

Objects are simply removed from the collection and not marked for deletion using PSafeObject::SafeRemove() and DeleteObject().

§ GetMutex()

const PMutex& PSafeCollection::GetMutex ( ) const
inline

Get the mutex for the collection.

References PDECLARE_NOTIFIER.

§ GetSize()

PINDEX PSafeCollection::GetSize ( ) const

Get the current size of the collection.

Note that usefulness of this function is limited as it is merely an instantaneous snapshot of the state of the collection.

§ IsEmpty()

PBoolean PSafeCollection::IsEmpty ( ) const
inline

Determine if the collection is empty.

Note that usefulness of this function is limited as it is merely an instantaneous snapshot of the state of the collection.

§ PDECLARE_NOTIFIER()

PSafeCollection::PDECLARE_NOTIFIER ( PTimer  ,
PSafeCollection  ,
DeleteObjectsTimeout   
)
protected

§ RemoveAll()

virtual void PSafeCollection::RemoveAll ( PBoolean  synchronous = false)
virtual

Remove all objects in collection.

Parameters
synchronousWait till objects are deleted before returning

§ SafeRemove()

virtual PBoolean PSafeCollection::SafeRemove ( PSafeObject obj)
protectedvirtual

Remove an object to the collection.

This function removes the object from the collection itself, but does not actually delete the object. It simply moves the object to a list of objects to be garbage collected at a later time.

As for Append() full mutual exclusion locking on the collection itself is maintained.

Parameters
objObject to remove from collection

§ SafeRemoveAt()

virtual PBoolean PSafeCollection::SafeRemoveAt ( PINDEX  idx)
protectedvirtual

Remove an object to the collection.

This function removes the object from the collection itself, but does not actually delete the object. It simply moves the object to a list of objects to be garbage collected at a later time.

As for Append() full mutual exclusion locking on the collection itself is maintained.

Parameters
idxObject index to remove

§ SafeRemoveObject()

void PSafeCollection::SafeRemoveObject ( PSafeObject obj)
protected

§ SetAutoDeleteObjects()

virtual void PSafeCollection::SetAutoDeleteObjects ( )
virtual

Start a timer to automatically call DeleteObjectsToBeRemoved().

Member Data Documentation

§ collection

§ collectionMutex

§ deleteObjects

bool PSafeCollection::deleteObjects
protected

§ deleteObjectsTimer

PTimer PSafeCollection::deleteObjectsTimer
protected

§ removalMutex

PMutex PSafeCollection::removalMutex
protected

§ toBeRemoved

PList<PSafeObject> PSafeCollection::toBeRemoved
protected

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