PTLib  Version 2.18.8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PSafeCollection Class Reference

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

#include <safecoll.h>

Inheritance diagram for PSafeCollection:
Collaboration diagram for PSafeCollection:

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
__inline unsigned GetTraceContextIdentifier () const
 Get PTRACE context identifier. More...
 
__inline void SetTraceContextIdentifier (unsigned id)
 
__inline void SetTraceContextIdentifier (const PObject &obj)
 
__inline void SetTraceContextIdentifier (const PObject *obj)
 
__inline void CopyTraceContextIdentifier (PObject &obj) const
 
__inline void CopyTraceContextIdentifier (PObject *obj) const
 
virtual ~PObject ()
 
__inline const char * GetClass () const
 
__inline bool IsClass (const char *name) const
 
__inline const PObjectPTraceObjectInstance () const
 
virtual PObjectClone () const
 Create a copy of the class on the heap. More...
 
template<class CLS >
CLS * CloneAs () const
 As for Clone() but converts to specified type. More...
 
virtual PINDEX HashFunction () const
 This function yields a hash value required by the PDictionary 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 ReadFrom (istream &strm)
 Input the contents of the object from the stream. More...
 

Protected Member Functions

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

Protected Attributes

PCollectionm_collection
 
PMutex m_collectionMutex
 
bool m_deleteObjects
 
PList< PSafeObjectm_toBeRemoved
 
PMutex m_removalMutex
 
- Protected Attributes inherited from PObject
unsigned m_traceContextIdentifier
 

Operations

virtual void PrintOn (ostream &strm) const
 Output the contents of the object to the stream. More...
 
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...
 
PMutexGetMutex ()
 
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 __inline void CopyTraceContextIdentifier (PObject &to, const PObject &from)
 
static __inline void CopyTraceContextIdentifier (PObject &to, const PObject *from)
 
static __inline void CopyTraceContextIdentifier (PObject *to, const PObject &from)
 
static __inline void CopyTraceContextIdentifier (PObject *to, const PObject *from)
 
static __inline const char * Class ()
 
static __inline const PObjectPTraceObjectInstance (const char *)
 
static __inline const PObjectPTraceObjectInstance (const PObject *obj)
 
template<typename T >
static Comparison Compare2 (T v1, T v2)
 Compare two types, returning Comparison type. 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 ( PCollection collection)

Create a thread safe collection of objects.

Note the collection is automatically deleted on destruction.

Parameters
collectionActual collection of objects
PSafeCollection::~PSafeCollection ( )

Destroy the thread safe collection.

The will delete the collection object provided in the constructor.

Member Function Documentation

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

References m_deleteObjects.

Referenced by PSafeDictionaryBase< PDictionary< K, D >, K, D >::MoveFrom().

void PSafeCollection::CopySafeCollection ( PCollection other)
protected
virtual void PSafeCollection::DeleteObject ( PObject object) const
virtual

Delete an objects that has been removed.

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.

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().

References m_deleteObjects.

Referenced by PSafeDictionaryBase< PDictionary< K, D >, K, D >::MoveFrom().

const PMutex& PSafeCollection::GetMutex ( ) const
inline

Get the mutex for the collection.

References m_collectionMutex.

PMutex& PSafeCollection::GetMutex ( )
inline

References m_collectionMutex.

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.

Referenced by 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.

References GetSize().

virtual void PSafeCollection::PrintOn ( ostream &  strm) const
virtual

Output the contents of the object to the stream.

The exact output is dependent on the exact semantics of the descendent class. This is primarily used by the standard operator<< function.

The default behaviour is to print the class name.

Reimplemented from PObject.

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

Remove all objects in collection.

Parameters
synchronousWait till objects are deleted before returning

Referenced by PSafeDictionaryBase< PDictionary< K, D >, K, D >::MoveFrom(), PSafeColl< PArray< Base >, Base >::operator=(), and PSafeDictionaryBase< PDictionary< K, D >, K, D >::operator=().

bool PSafeCollection::SafeAddObject ( PSafeObject obj,
PSafeObject old 
)
protected
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

Referenced by PSafeColl< PArray< Base >, Base >::Remove(), and PSafeDictionaryBase< PDictionary< K, D >, K, D >::RemoveAt().

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

Referenced by PSafeColl< PArray< Base >, Base >::RemoveAt().

void PSafeCollection::SafeRemoveObject ( PSafeObject obj)
protected
virtual void PSafeCollection::SetAutoDeleteObjects ( )
virtual

Start a timer to automatically call DeleteObjectsToBeRemoved().

Member Data Documentation

bool PSafeCollection::m_deleteObjects
protected
PMutex PSafeCollection::m_removalMutex
protected
PList<PSafeObject> PSafeCollection::m_toBeRemoved
protected

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