31 #ifndef PTLIB_SAFE_COLLECTION_H 32 #define PTLIB_SAFE_COLLECTION_H 263 mutable PMutex safetyMutex;
264 unsigned safeReferenceCount;
265 bool safelyBeingRemoved;
372 virtual void RemoveAll(
382 ) { deleteObjects = yes; }
394 virtual PBoolean DeleteObjectsToBeRemoved();
398 virtual void DeleteObject(
PObject *
object)
const;
402 virtual void SetAutoDeleteObjects();
408 PINDEX GetSize()
const;
545 virtual void SetNULL();
549 bool operator!()
const {
return currentObject == NULL; }
573 virtual void Assign(PINDEX idx);
577 virtual void Previous();
689 virtual void SetNULL();
706 virtual void Assign(PINDEX idx);
710 virtual void Previous();
714 virtual void UnlockPtr();
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);
968 CopySafeCollection(dynamic_cast<Coll *>(other.
collection));
976 if (&other !=
this) {
980 CopySafeCollection(dynamic_cast<Coll *>(other.
collection));
997 if (
PAssert(collection->GetObjectsIndex(obj) == P_MAX_INDEX,
"Cannot insert safe object twice") &&
998 obj->SafeReference())
1028 return SafeRemoveAt(idx);
1052 collectionMutex.Wait();
1054 collectionMutex.Signal();
1055 ptr.SetSafetyMode(mode);
1123 CopySafeDictionary(dynamic_cast<Coll *>(other.
collection));
1131 if (&other !=
this) {
1135 CopySafeDictionary(dynamic_cast<Coll *>(other.
collection));
1147 virtual void SetAt(
const Key & key, Base * obj)
1149 collectionMutex.Wait();
1150 SafeRemove(((Coll *)collection)->GetAt(key));
1151 if (
PAssert(collection->GetObjectsIndex(obj) == P_MAX_INDEX,
"Cannot insert safe object twice") &&
1152 obj->SafeReference())
1153 ((Coll *)collection)->SetAt(key, obj);
1154 collectionMutex.Signal();
1169 return SafeRemove(((Coll *)collection)->GetAt(key));
1178 return ((Coll *)collection)->Contains(key);
1202 collectionMutex.Wait();
1204 collectionMutex.Signal();
1205 ptr.SetSafetyMode(mode);
1214 collectionMutex.Wait();
1215 ((Coll *)collection)->AbstractGetKeys(keys);
1216 collectionMutex.Signal();
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
This class defines a base class for thread-safe pointer to an object.
Definition: safecoll.h:614
#define PDECLARE_NOTIFIER(notifier, notifiee, func)
Declare PNotifier derived class with INT parameter. Uses PDECLARE_NOTIFIER2 macro.
Definition: notifier.h:174
This class waits for the semaphore on construction and automatically signals the semaphore on destruc...
Definition: psync.h:86
virtual Comparison Compare(const PObject &obj) const
Compare the two objects and return their relative rank.
virtual PSafePtr< Base > Append(Base *obj, PSafetyMode mode=PSafeReference)
Add an object to the collection.
Definition: safecoll.h:992
void UnlockReadOnly() const
Release the read only lock on an object.
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
#define PCLASSINFO(cls, par)
Declare all the standard PTLib class information.
Definition: object.h:1049
PSafeObject * currentObject
Definition: safecoll.h:597
This class defines a thread-safe array of objects.
Definition: safecoll.h:1227
PBoolean SafeReference()
Increment the reference count for object.
Definition: safecoll.h:443
PArray< Key > GetKeys() const
Get an array containing all the keys for the dictionary.
Definition: safecoll.h:1211
PSafeObject * m_objectToDelete
Definition: safecoll.h:718
ExitSafetyModeOption
Definition: safecoll.h:586
PSafeObject & safeObject
Definition: safecoll.h:286
PBoolean IsLocked() const
Definition: safecoll.h:282
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
const PSafeCollection * GetCollection() const
Get the associated collection this pointer may be contained in.
Definition: safecoll.h:567
PSafePtr(const PSafeCollection &safeCollection, PSafetyMode mode=PSafeReadWrite, PINDEX idx=0)
Create a new pointer to a PSafeObject.
Definition: safecoll.h:766
PBoolean LockReadOnly() const
Lock the object for Read Only access.
void UnlockReadWrite()
Release the read/write lock on an object.
friend class PSafeCollection
Definition: safecoll.h:269
This class defines a thread-safe array of objects.
Definition: safecoll.h:1066
This class defines a thread-safe list of objects.
Definition: safecoll.h:1077
T * operator->() const
Allow access to the physical object the pointer is pointing to.
Definition: safecoll.h:877
Comparison
Result of the comparison operation performed by the Compare() function.
Definition: object.h:1184
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
PBoolean locked
Definition: safecoll.h:287
PBoolean SafelyCanBeDeleted() const
Determine if the object can be safely deleted.
void SafeRemove()
Set the removed flag.
This class defines a thread-safe collection of objects.
Definition: safecoll.h:323
#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
PSafeColl & operator=(const PSafeColl &other)
Assign one safe collection to another.
Definition: safecoll.h:974
virtual void PrintOn(ostream &strm) const
Output the contents of the object to the stream.
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
const PMutex & GetMutex() const
Get the mutex for the collection.
Definition: safecoll.h:418
This class defines a thread-safe dictionary of objects.
Definition: safecoll.h:1105
PCollection * collection
Definition: safecoll.h:427
PMutex removalMutex
Definition: safecoll.h:431
PSafePtr(const PSafeCollection &safeCollection, PSafetyMode mode, PSafeObject *obj)
Create a new pointer to a PSafeObject.
Definition: safecoll.h:779
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
This class defines a thread-safe object in a collection.
Definition: safecoll.h:119
Definition: safecoll.h:445
Definition: safecoll.h:581
PTimer deleteObjectsTimer
Definition: safecoll.h:432
PSafePtr & operator=(T *obj)
Set the new pointer to a PSafeObject.
Definition: safecoll.h:830
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
bool operator!() const
Return true if pointer is NULL.
Definition: safecoll.h:549
PSafePtr< Base > value_type
Definition: safecoll.h:1230
PMutex m_mutex
Definition: safecoll.h:717
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
PSafePtr & operator=(PINDEX idx)
Set the new pointer to a collection index.
Definition: safecoll.h:845
This class defines a thread-safe sorted array of objects.
Definition: safecoll.h:1088
PSafetyMode
Definition: safecoll.h:442
virtual void LockPtr()
Definition: safecoll.h:713
PBoolean IsEmpty() const
Determine if the collection is empty.
Definition: safecoll.h:414
T * operator++()
Pre-increment the pointer.
Definition: safecoll.h:894
PSafetyMode GetSafetyMode() const
Get the locking mode used by this pointer.
Definition: safecoll.h:553
PSafePtr< Derived > PSafePtrCast(const PSafePtr< Base > &oldPtr)
Cast the pointer to a different type.
Definition: safecoll.h:928
bool operator!() const
Definition: safecoll.h:283
bool operator!() const
Definition: safecoll.h:302
This template class maps the PArrayObjects to a specific object type.
Definition: array.h:1024
#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
PBoolean IsLocked() const
Definition: safecoll.h:301
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
PSafePtr< Base > value_type
Definition: safecoll.h:1091
const PSafeCollection * collection
Definition: safecoll.h:596
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
T & operator*() const
Return the physical pointer to the object.
Definition: safecoll.h:873
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
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
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