31 #ifndef PTLIB_SAFE_COLLECTION_H
32 #define PTLIB_SAFE_COLLECTION_H
263 mutable PMutex safetyMutex;
264 unsigned safeReferenceCount;
265 bool safelyBeingRemoved;
573 virtual void Assign(PINDEX idx);
706 virtual void Assign(PINDEX idx);
742 template <
class T,
class BaseClass = PSafePtrBase>
class PSafePtr :
public BaseClass
757 ) : BaseClass(obj, mode) { }
770 ) : BaseClass(safeCollection, mode, idx) { }
783 ) : BaseClass(safeCollection, mode, obj) { }
792 ) : BaseClass(ptr) { }
801 BaseClass::Assign(ptr);
811 BaseClass::Assign(safeCollection);
847 BaseClass::Assign(idx);
869 operator T*()
const {
return (T *)BaseClass::currentObject; }
885 T * previous = (T *)BaseClass::currentObject;
897 return (T *)BaseClass::currentObject;
906 T * previous = (T *)BaseClass::currentObject;
907 BaseClass::Previous();
917 BaseClass::Previous();
918 return (T *)BaseClass::currentObject;
927 template <
class Base,
class Derived>
932 Base * realPtr = oldPtr;
934 newPtr.Assign(oldPtr);
976 if (&other !=
this) {
998 obj->SafeReference())
1055 ptr.SetSafetyMode(mode);
1131 if (&other !=
this) {
1147 virtual void SetAt(
const Key & key, Base * obj)
1152 obj->SafeReference())
1205 ptr.SetSafetyMode(mode);
1234 #endif // PTLIB_SAFE_COLLECTION_H
T * operator++(int)
Post-increment the pointer.
Definition: safecoll.h:883
virtual PSafePtr< Base > FindWithLock(const Key &key, PSafetyMode mode=PSafeReadWrite)
Find the instance in the collection of an object with the same value.
Definition: safecoll.h:1198
PSafePtr< Base > value_type
Definition: safecoll.h:1069
void UnlockReadOnly() const
Release the read only lock on an object.
This class defines a base class for thread-safe pointer to an object.
Definition: safecoll.h:614
PBoolean IsLocked() const
Definition: safecoll.h:301
This class waits for the semaphore on construction and automatically signals the semaphore on destruc...
Definition: psync.h:86
virtual PBoolean DeleteObjectsToBeRemoved()
Delete any objects that have been removed.
virtual PSafePtr< Base > Append(Base *obj, PSafetyMode mode=PSafeReference)
Add an object to the collection.
Definition: safecoll.h:992
PDECLARE_NOTIFIER(PTimer, PSafeCollection, DeleteObjectsTimeout)
virtual void UnlockPtr()
Definition: safecoll.h:593
This class defines a base class for thread-safe pointer to an object.
Definition: safecoll.h:460
EnterSafetyModeOption
Definition: safecoll.h:580
This class defines a thread-safe enumeration of object in a collection.
Definition: safecoll.h:742
T * operator->() const
Allow access to the physical object the pointer is pointing to.
Definition: safecoll.h:877
PSafeObject * currentObject
Definition: safecoll.h:597
This class defines a thread-safe array of objects.
Definition: safecoll.h:1227
const PSafeCollection * GetCollection() const
Get the associated collection this pointer may be contained in.
Definition: safecoll.h:567
PBoolean SafeReference()
Increment the reference count for object.
virtual void SetAutoDeleteObjects()
Start a timer to automatically call DeleteObjectsToBeRemoved().
Definition: safecoll.h:443
~PSafePtrBase()
Unlock and dereference the PSafeObject this is pointing to.
PSafeObject * m_objectToDelete
Definition: safecoll.h:718
ExitSafetyModeOption
Definition: safecoll.h:586
PSafePtrMultiThreaded(PSafeObject *obj=NULL, PSafetyMode mode=PSafeReference)
Create a new pointer to a PSafeObject.
bool operator!() const
Definition: safecoll.h:302
PSafeObject & safeObject
Definition: safecoll.h:286
PSafePtr Set(T *obj)
Set the safe pointer to the specified object.
Definition: safecoll.h:855
PSafeColl(const PSafeColl &other)
Copy constructor for safe collection.
Definition: safecoll.h:964
PSafePtrBase(PSafeObject *obj=NULL, PSafetyMode mode=PSafeReference)
Create a new pointer to a PSafeObject.
PBoolean LockReadOnly() const
Lock the object for Read Only access.
PSafePtr(const PSafeCollection &safeCollection, PSafetyMode mode=PSafeReadWrite, PINDEX idx=0)
Create a new pointer to a PSafeObject.
Definition: safecoll.h:766
void UnlockReadWrite()
Release the read/write lock on an object.
This class defines a thread-safe array of objects.
Definition: safecoll.h:1066
virtual PINDEX Append(PObject *obj)=0
Append a new object to the collection.
This class defines a thread-safe list of objects.
Definition: safecoll.h:1077
virtual void DeleteObject(PSafeObject *obj)
Comparison
Result of the comparison operation performed by the Compare() function.
Definition: object.h:1184
void ExitSafetyMode(ExitSafetyModeOption ref)
virtual void SetAt(const Key &key, Base *obj)
Add an object to the collection.
Definition: safecoll.h:1147
PBoolean locked
Definition: safecoll.h:306
An abstract dictionary container.
Definition: dict.h:626
PMutex collectionMutex
Definition: safecoll.h:428
PSafeDictionaryBase()
Create a safe dictionary wrapper around the real collection.
Definition: safecoll.h:1113
bool deleteObjects
Definition: safecoll.h:429
virtual Comparison Compare(const PObject &obj) const
Compare the pointers.
PBoolean locked
Definition: safecoll.h:287
void SafeRemove()
Set the removed flag.
virtual void RemoveAll(PBoolean synchronous=false)
Remove all objects in collection.
This class defines a thread-safe collection of objects.
Definition: safecoll.h:323
virtual Comparison Compare(const PObject &obj) const
Compare the pointers.
#define PIsDescendant(ptr, cls)
Definition: object.h:1065
virtual PBoolean RemoveAt(PINDEX idx)
Remove an object to the collection.
Definition: safecoll.h:1025
virtual PBoolean Remove(Base *obj)
Remove an object to the collection.
Definition: safecoll.h:1011
PList< PSafeObject > toBeRemoved
Definition: safecoll.h:430
virtual PBoolean RemoveAt(const Key &key)
Remove an object to the collection.
Definition: safecoll.h:1165
virtual PINDEX GetValuesIndex(const PObject &obj) const =0
Search the collection for the specified value of the object.
PBoolean IsLocked() const
Definition: safecoll.h:282
PSafeColl & operator=(const PSafeColl &other)
Assign one safe collection to another.
Definition: safecoll.h:974
virtual void Assign(const PSafePtrBase &ptr)
PBoolean IsEmpty() const
Determine if the collection is empty.
Definition: safecoll.h:414
A class representing a system timer.
Definition: timer.h:181
virtual void LockPtr()
Definition: safecoll.h:592
BOOL PBoolean
Definition: object.h:102
PSafePtr(const PSafePtr &ptr)
Copy the pointer to the PSafeObject.
Definition: safecoll.h:790
virtual void SetNULL()
Set the pointer to NULL, unlocking/dereferencing existing pointer value.
This class defines a thread-safe dictionary of objects.
Definition: safecoll.h:1105
PCollection * collection
Definition: safecoll.h:427
Definition: safecoll.h:588
PMutex removalMutex
Definition: safecoll.h:431
virtual PBoolean SetSafetyMode(PSafetyMode mode)
Change the locking mode used by this pointer.
PSafePtr(const PSafeCollection &safeCollection, PSafetyMode mode, PSafeObject *obj)
Create a new pointer to a PSafeObject.
Definition: safecoll.h:779
void SafeRemoveObject(PSafeObject *obj)
virtual PBoolean SetSafetyMode(PSafetyMode mode)
Change the locking mode used by this pointer.
PArray< Key > GetKeys() const
Get an array containing all the keys for the dictionary.
Definition: safecoll.h:1211
virtual PSafePtr< Base > GetAt(PINDEX idx, PSafetyMode mode=PSafeReadWrite)
Get the instance in the collection of the index.
Definition: safecoll.h:1186
#define PAssertNULL(ptr)
This macro is used to assert that a pointer must be non-null.
Definition: object.h:220
void CopySafeCollection(PCollection *other)
This class defines a thread-safe object in a collection.
Definition: safecoll.h:119
PINDEX GetSize() const
Get the current size of the collection.
virtual PBoolean SafeRemoveAt(PINDEX idx)
Remove an object to the collection.
Definition: safecoll.h:445
bool operator!() const
Definition: safecoll.h:283
Definition: safecoll.h:581
const PMutex & GetMutex() const
Get the mutex for the collection.
Definition: safecoll.h:418
virtual void PrintOn(ostream &strm) const
Output the contents of the object to the stream.
PTimer deleteObjectsTimer
Definition: safecoll.h:432
void CopySafeDictionary(PAbstractDictionary *other)
bool operator!() const
Return true if pointer is NULL.
Definition: safecoll.h:549
PSafePtr & operator=(T *obj)
Set the new pointer to a PSafeObject.
Definition: safecoll.h:830
PSafeCollection(PCollection *collection)
Create a thread safe collection of objects.
void AllowDeleteObjects(PBoolean yes=true)
Disallow the automatic delete any objects that have been removed.
Definition: safecoll.h:380
virtual PSafePtr< Base > FindWithLock(const Base &value, PSafetyMode mode=PSafeReadWrite)
Find the instance in the collection of an object with the same value.
Definition: safecoll.h:1048
PSafePtr< Base > value_type
Definition: safecoll.h:1230
Definition: safecoll.h:582
virtual PBoolean SafeRemove(PSafeObject *obj)
Remove an object to the collection.
PMutex m_mutex
Definition: safecoll.h:717
virtual void SetNULL()
Set the pointer to NULL, unlocking/dereferencing existing pointer value.
T * operator--(int)
Post-decrement the pointer.
Definition: safecoll.h:904
void DisallowDeleteObjects()
Disallow the automatic delete any objects that have been removed.
Definition: safecoll.h:388
PSafeObject & safeObject
Definition: safecoll.h:305
This class defines a thread-safe collection of objects.
Definition: safecoll.h:949
virtual bool GarbageCollection()
Do any garbage collection that may be required by the object so that it may be finally deleted...
Definition: safecoll.h:587
virtual PINDEX GetObjectsIndex(const PObject *obj) const =0
Search the collection for the specific instance of the object.
PSafePtr & operator=(PINDEX idx)
Set the new pointer to a collection index.
Definition: safecoll.h:845
~PSafeCollection()
Destroy the thread safe collection.
virtual void DeleteObject(PObject *object) const
Delete an objects that has been removed.
PBoolean SafelyCanBeDeleted() const
Determine if the object can be safely deleted.
This class defines a thread-safe sorted array of objects.
Definition: safecoll.h:1088
PBoolean EnterSafetyMode(EnterSafetyModeOption ref)
PSafetyMode
Definition: safecoll.h:442
virtual void LockPtr()
Definition: safecoll.h:713
T * operator++()
Pre-increment the pointer.
Definition: safecoll.h:894
PSafePtr< Derived > PSafePtrCast(const PSafePtr< Base > &oldPtr)
Cast the pointer to a different type.
Definition: safecoll.h:928
This template class maps the PArrayObjects to a specific object type.
Definition: array.h:1024
virtual void DeleteObject(PSafeObject *obj)
#define PAssert(b, msg)
This macro is used to assert that a condition must be true.
Definition: object.h:192
PSafePtr & operator=(const PSafePtr &ptr)
Copy the pointer to the PSafeObject.
Definition: safecoll.h:799
T & operator*() const
Return the physical pointer to the object.
Definition: safecoll.h:873
virtual PSafePtr< Base > GetAt(PINDEX idx, PSafetyMode mode=PSafeReadWrite)
Get the instance in the collection of the index.
Definition: safecoll.h:1036
This class defines a thread synchronisation object.
Definition: syncthrd.h:251
PSafetyMode GetSafetyMode() const
Get the locking mode used by this pointer.
Definition: safecoll.h:553
PSafePtr< Base > value_type
Definition: safecoll.h:1091
const PSafeCollection * collection
Definition: safecoll.h:596
virtual void Assign(const PSafePtrMultiThreaded &ptr)
PBoolean SafeDereference()
Decrement the reference count for object.
T * operator--()
Pre-decrement the pointer.
Definition: safecoll.h:915
PSafeObject(PSafeObject *indirectLock=NULL)
Create a thread safe object.
Lock a PSafeObject for read only and automatically unlock it when go out of scope.
Definition: safecoll.h:275
Definition: safecoll.h:444
PSafeDictionaryBase(const PSafeDictionaryBase &other)
Copy constructor for safe collection.
Definition: safecoll.h:1119
Ultimate parent class for all objects in the class library.
Definition: object.h:1118
A collection is a container that collects together descendents of the PObject class.
Definition: contain.h:395
~PSafePtrMultiThreaded()
Unlock and dereference the PSafeObject this is pointing to.
PSafeLockReadWrite(const PSafeObject &object)
PBoolean LockReadWrite()
Lock the object for Read/Write access.
PSafePtr< Base > value_type
Definition: safecoll.h:1080
Lock a PSafeObject for read/write and automatically unlock it when go out of scope.
Definition: safecoll.h:294
PSafeLockReadOnly(const PSafeObject &object)
PSafeDictionaryBase & operator=(const PSafeDictionaryBase &other)
Assign one safe collection to another.
Definition: safecoll.h:1129
PSafeColl()
Create a safe list collection wrapper around the real collection.
Definition: safecoll.h:957
PSafePtr & operator=(const PSafeCollection &safeCollection)
Start an enumerated PSafeObject.
Definition: safecoll.h:809
virtual PBoolean Contains(const Key &key)
Determine of the dictionary contains an entry for the key.
Definition: safecoll.h:1174
PSafetyMode lockMode
Definition: safecoll.h:598
PSafePtr(T *obj=NULL, PSafetyMode mode=PSafeReference)
Create a new pointer to a PSafeObject.
Definition: safecoll.h:754