27 #ifndef PTLIB_SAFE_COLLECTION_H
28 #define PTLIB_SAFE_COLLECTION_H
195 __inline
bool LockReadOnly()
const {
return InternalLockReadOnly(NULL); }
294 void InternalUnlockReadOnly(
const PDebugLocation * location)
const;
295 bool InternalLockReadWrite(
const PDebugLocation * location)
const;
296 void InternalUnlockReadWrite(
const PDebugLocation * location)
const;
300 unsigned m_safeReferenceCount;
301 bool m_safelyBeingRemoved;
302 bool m_safeMutexCreated;
379 #define P_INSTRUMENTED_LOCK_READ_ONLY2(var, obj) PInstrumentedSafeLockReadOnly var((obj), P_DEBUG_LOCATION)
380 #define P_INSTRUMENTED_LOCK_READ_WRITE2(var, obj) PInstrumentedSafeLockReadWrite var((obj), P_DEBUG_LOCATION)
382 #define P_INSTRUMENTED_LOCK_READ_ONLY2(var, obj) PSafeLockReadOnly var((obj))
383 #define P_INSTRUMENTED_LOCK_READ_WRITE2(var, obj) PSafeLockReadWrite var((obj))
386 #define P_MAKE_UNIQUE_VAR(base) P_MAKE_UNIQUE_VAR1(base, __LINE__)
387 #define P_MAKE_UNIQUE_VAR1(base, line) P_MAKE_UNIQUE_VAR2(base, line)
388 #define P_MAKE_UNIQUE_VAR2(base, line) base##line
390 #define P_READ_WRITE_RETURN_ARG_0()
391 #define P_READ_WRITE_RETURN_ARG_1(arg) ; if (!P_MAKE_UNIQUE_VAR(lock).IsLocked()) arg
392 #define P_READ_WRITE_RETURN_PART1(narg, args) P_READ_WRITE_RETURN_PART2(narg, args)
393 #define P_READ_WRITE_RETURN_PART2(narg, args) P_READ_WRITE_RETURN_ARG_##narg args
395 #define P_INSTRUMENTED_LOCK_READ_ONLY(...) P_INSTRUMENTED_LOCK_READ_ONLY2(P_MAKE_UNIQUE_VAR(lock),*this) P_READ_WRITE_RETURN_PART1(PARG_COUNT(__VA_ARGS__), (__VA_ARGS__))
396 #define P_INSTRUMENTED_LOCK_READ_WRITE(...) P_INSTRUMENTED_LOCK_READ_WRITE2(P_MAKE_UNIQUE_VAR(lock),*this) P_READ_WRITE_RETURN_PART1(PARG_COUNT(__VA_ARGS__), (__VA_ARGS__))
534 PTimer * m_deleteObjectsTimer;
542 , m_deleteObjectsTimer()
686 virtual void Assign(PINDEX idx);
819 virtual void Assign(PINDEX idx);
855 template <
class T,
class BaseClass = PSafePtrBase>
class PSafePtr :
public BaseClass
870 ) : BaseClass(obj, mode) { }
883 ) : BaseClass(safeCollection, mode, idx) { }
896 ) : BaseClass(safeCollection, mode, obj) { }
905 ) : BaseClass(ptr) { }
924 this->Assign(safeCollection);
982 operator T*()
const {
return dynamic_cast<T *
>(this->m_currentObject); }
998 T * previous =
dynamic_cast<T *
>(this->m_currentObject);
1010 return dynamic_cast<T *
>(this->m_currentObject);
1019 T * previous =
dynamic_cast<T *
>(this->m_currentObject);
1031 return dynamic_cast<T *
>(this->m_currentObject);
1040 template <
class Base,
class Derived>
1044 if (dynamic_cast<Derived *>(oldPtr.GetObject()) != NULL)
1045 newPtr.Assign(oldPtr);
1087 if (&other !=
this) {
1165 ptr.SetSafetyMode(mode);
1241 if (&other !=
this) {
1257 virtual void SetAt(
const Key & key, Base * obj)
1313 ptr.SetSafetyMode(mode);
1328 ptr.SetSafetyMode(mode);
1375 dynamic_cast<Coll &
>(*this->
m_collection).AbstractGetKeys(keys);
1387 template <
class K,
class D>
1517 #endif // PTLIB_SAFE_COLLECTION_H
T * operator++(int)
Post-increment the pointer.
Definition: safecoll.h:996
Information about a source file location.
Definition: object.h:333
PSafePtr< Base > value_type
Definition: safecoll.h:1179
PSafeObject * m_currentObject
Definition: safecoll.h:710
This class defines a base class for thread-safe pointer to an object.
Definition: safecoll.h:727
const_iterator operator--()
Definition: safecoll.h:1497
iterator(dict_type *dict, const K &key)
Definition: safecoll.h:1466
PMutex & GetMutex()
Definition: safecoll.h:517
friend class PInstrumentedSafeLockReadWrite
Definition: safecoll.h:310
iterator()
Definition: safecoll.h:1469
This class waits for the semaphore on construction and automatically signals the semaphore on destruc...
Definition: psync.h:115
iterator find(const K &key)
Definition: safecoll.h:1484
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:1103
value_type m_internal_second
Definition: safecoll.h:1403
const_iterator operator--(int)
Definition: safecoll.h:1499
PSafePtr< D > value_type
Definition: safecoll.h:1393
PMutex m_collectionMutex
Definition: safecoll.h:527
virtual void UnlockPtr()
Definition: safecoll.h:706
PList< PSafeObject > m_toBeRemoved
Definition: safecoll.h:529
virtual PINDEX GetSize() const
Get size of array.
iterator_base(const dict_type *dict)
Definition: safecoll.h:1417
This class defines a base class for thread-safe pointer to an object.
Definition: safecoll.h:568
const dict_type * m_dictionary
Definition: safecoll.h:1405
const K & first
Definition: safecoll.h:1456
EnterSafetyModeOption
Definition: safecoll.h:693
K key_type
Definition: safecoll.h:1391
This class defines a thread-safe enumeration of object in a collection.
Definition: safecoll.h:855
iterator begin()
Definition: safecoll.h:1482
T * operator->() const
Allow access to the physical object the pointer is pointing to.
Definition: safecoll.h:990
PSafeObject * GetObject() const
Return pointer to safe object.
Definition: safecoll.h:661
void Prev()
Definition: safecoll.h:1447
This class defines a thread-safe array of objects.
Definition: safecoll.h:1388
PBoolean SafeReference()
Increment the reference count for object.
LockFn m_lock
Definition: safecoll.h:334
virtual void SetAutoDeleteObjects()
Start a timer to automatically call DeleteObjectsToBeRemoved().
Definition: safecoll.h:551
~PSafePtrBase()
Unlock and dereference the PSafeObject this is pointing to.
PSafeObject * m_objectToDelete
Definition: safecoll.h:831
ExitSafetyModeOption
Definition: safecoll.h:699
PSafePtrMultiThreaded(PSafeObject *obj=NULL, PSafetyMode mode=PSafeReference)
Create a new pointer to a PSafeObject.
virtual bool Move(const Key &from, const Key &to)
Move an object from one key location to another.
Definition: safecoll.h:1339
This class implements critical section mutexes using the most efficient mechanism available on the ho...
Definition: mutex.h:270
PSafePtr Set(T *obj)
Set the safe pointer to the specified object.
Definition: safecoll.h:968
__inline void UnlockReadOnly(const PDebugLocation &location) const
Definition: safecoll.h:209
PSafeColl(const PSafeColl &other)
Copy constructor for safe collection.
Definition: safecoll.h:1075
PSafePtrBase(PSafeObject *obj=NULL, PSafetyMode mode=PSafeReference)
Create a new pointer to a PSafeObject.
iterator end()
Definition: safecoll.h:1483
PCollection * m_collection
Definition: safecoll.h:526
PSafePtr(const PSafeCollection &safeCollection, PSafetyMode mode=PSafeReadWrite, PINDEX idx=0)
Create a new pointer to a PSafeObject.
Definition: safecoll.h:879
This class defines a thread-safe array of objects.
Definition: safecoll.h:1176
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:1187
virtual void PrintOn(ostream &strm) const
Output the contents of the object to the stream.
virtual void DeleteObject(PSafeObject *obj)
unsigned IsSafelyBeingRemoved() const
Indicate the object is being safely removed.
Definition: safecoll.h:259
Comparison
Result of the comparison operation performed by the Compare() function.
Definition: object.h:2251
void ExitSafetyMode(ExitSafetyModeOption ref)
virtual void SetAt(const Key &key, Base *obj)
Add an object to the collection.
Definition: safecoll.h:1257
A set of ordinal integers.
Definition: dict.h:762
PSafeDictionary< K, D > dict_type
Definition: safecoll.h:1394
D data_type
Definition: safecoll.h:1392
PSafeDictionaryBase()
Create a safe dictionary wrapper around the real collection.
Definition: safecoll.h:1223
const iterator_pair * operator->() const
Definition: safecoll.h:1476
virtual Comparison Compare(const PObject &obj) const
Compare the pointers.
iterator_base()
Definition: safecoll.h:1409
const_iterator find(const K &key) const
Definition: safecoll.h:1509
__inline bool LockReadOnly(const PDebugLocation &location) const
Definition: safecoll.h:196
const_iterator operator++()
Definition: safecoll.h:1496
value_type second
Definition: safecoll.h:1457
K * m_internal_first
Definition: safecoll.h:1402
iterator operator++(int)
Definition: safecoll.h:1473
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:411
virtual Comparison Compare(const PObject &obj) const
Compare the pointers.
virtual PBoolean RemoveAt(PINDEX idx)
Remove an object to the collection.
Definition: safecoll.h:1135
virtual PBoolean Remove(Base *obj)
Remove an object to the collection.
Definition: safecoll.h:1121
virtual PBoolean RemoveAt(const Key &key)
Remove an object to the collection.
Definition: safecoll.h:1273
virtual PINDEX GetValuesIndex(const PObject &obj) const =0
Search the collection for the specified value of the object.
PSafeColl & operator=(const PSafeColl &other)
Assign one safe collection to another.
Definition: safecoll.h:1085
virtual void Assign(const PSafePtrBase &ptr)
PBoolean IsEmpty() const
Determine if the collection is empty.
Definition: safecoll.h:512
bool SafeAddObject(PSafeObject *obj, PSafeObject *old)
const_iterator(const typename dict_type::iterator &it)
Definition: safecoll.h:1494
virtual void LockPtr()
Definition: safecoll.h:705
PSafePtr(const PSafePtr &ptr)
Copy the pointer to the PSafeObject.
Definition: safecoll.h:903
virtual void SetNULL()
Set the pointer to NULL, unlocking/dereferencing existing pointer value.
iterator operator--(int)
Definition: safecoll.h:1474
This class defines a thread-safe dictionary of objects.
Definition: safecoll.h:1215
PMutex m_removalMutex
Definition: safecoll.h:530
const_iterator(const dict_type *dict, const K &key)
Definition: safecoll.h:1490
Definition: safecoll.h:701
PSafetyMode m_lockMode
Definition: safecoll.h:711
Definition: safecoll.h:1454
T * GetObjectAs() const
Return pointer to safe object.
Definition: safecoll.h:666
iterator_base(const dict_type *dict, const K &key)
Definition: safecoll.h:1424
void(PSafeObject::* UnlockFn)(const PDebugLocation *location) const
Definition: safecoll.h:318
const_iterator operator++(int)
Definition: safecoll.h:1498
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:892
virtual P_DEPRECATED PSafePtr< Base > FindWithLock(const Key &key, PSafetyMode mode=PSafeReadWrite) const
Definition: safecoll.h:1332
__inline void UnlockReadWrite() const
Release the read/write lock on an object.
Definition: safecoll.h:241
bool operator==(const iterator_base &it) const
Definition: safecoll.h:1450
void SafeRemoveObject(PSafeObject *obj)
PSafeLockBase(const PSafeObject &object, const PDebugLocation *location, LockFn lock, UnlockFn unlock)
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:1371
PSafeObject & m_safeObject
Definition: safecoll.h:332
virtual PSafePtr< Base > GetAt(PINDEX idx, PSafetyMode mode=PSafeReadWrite)
Get the instance in the collection of the index.
Definition: safecoll.h:1294
#define PAssertNULL(ptr)
This macro is used to assert that a pointer must be non-null.
Definition: object.h:428
void CopySafeCollection(PCollection *other)
This class defines a thread-safe object in a collection.
Definition: safecoll.h:123
PINDEX GetSize() const
Get the current size of the collection.
virtual PBoolean SafeRemoveAt(PINDEX idx)
Remove an object to the collection.
bool PBoolean
Definition: object.h:174
Definition: safecoll.h:553
Definition: safecoll.h:694
const PMutex & GetMutex() const
Get the mutex for the collection.
Definition: safecoll.h:516
virtual void PrintOn(ostream &strm) const
Output the contents of the object to the stream.
const PSafeCollection * m_collection
Definition: safecoll.h:709
bool operator!=(const iterator_base &it) const
Definition: safecoll.h:1451
void erase(const iterator &it)
Definition: safecoll.h:1511
void Next()
Definition: safecoll.h:1446
void CopySafeDictionary(PAbstractDictionary *other)
__inline bool LockReadWrite(const PDebugLocation &location) const
Definition: safecoll.h:229
const iterator_pair & operator*() const
Definition: safecoll.h:1477
#define P_MAX_INDEX
Definition: object.h:80
bool operator!() const
Return true if pointer is NULL.
Definition: safecoll.h:657
PSafePtr & operator=(T *obj)
Set the new pointer to a PSafeObject.
Definition: safecoll.h:943
bool operator!() const
Definition: safecoll.h:329
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:478
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:1158
const_iterator()
Definition: safecoll.h:1493
Definition: safecoll.h:695
__inline void UnlockReadOnly() const
Release the read only lock on an object.
Definition: safecoll.h:208
virtual PBoolean SafeRemove(PSafeObject *obj)
Remove an object to the collection.
PMutex m_mutex
Definition: safecoll.h:830
virtual void SetNULL()
Set the pointer to NULL, unlocking/dereferencing existing pointer value.
void MoveFrom(PSafeDictionaryBase &other)
Move all objects from other dictionary to this one.
Definition: safecoll.h:1354
T * operator--(int)
Post-decrement the pointer.
Definition: safecoll.h:1017
__inline bool LockReadWrite() const
Lock the object for Read/Write access.
Definition: safecoll.h:228
This class defines a thread mutual exclusion object.
Definition: mutex.h:101
virtual void Signal()
Signal that the synchronisation object is available.
void DisallowDeleteObjects()
Disallow the automatic delete any objects that have been removed.
Definition: safecoll.h:486
Definition: safecoll.h:1487
This class defines a thread-safe collection of objects.
Definition: safecoll.h:1060
virtual bool GarbageCollection()
Do any garbage collection that may be required by the object so that it may be finally deleted...
__inline bool LockReadOnly() const
Lock the object for Read Only access.
Definition: safecoll.h:195
Definition: safecoll.h:700
void erase(const const_iterator &it)
Definition: safecoll.h:1512
__inline void UnlockReadWrite(const PDebugLocation &location) const
Definition: safecoll.h:242
PSafePtr & operator=(PINDEX idx)
Set the new pointer to a collection index.
Definition: safecoll.h:958
~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.
unsigned GetSafeReferenceCount() const
Get count of references to this object.
Definition: safecoll.h:287
iterator operator++()
Definition: safecoll.h:1471
This class defines a thread-safe sorted array of objects.
Definition: safecoll.h:1198
PBoolean EnterSafetyMode(EnterSafetyModeOption ref)
PSafetyMode
Definition: safecoll.h:550
bool(PSafeObject::* LockFn)(const PDebugLocation *location) const
Definition: safecoll.h:317
iterator operator--()
Definition: safecoll.h:1472
friend class PInstrumentedSafeLockReadOnly
Definition: safecoll.h:309
virtual void LockPtr()
Definition: safecoll.h:826
Definition: safecoll.h:314
PArray< K > m_keys
Definition: safecoll.h:1406
T * operator++()
Pre-increment the pointer.
Definition: safecoll.h:1007
Definition: safecoll.h:1400
const_iterator(const dict_type *dict)
Definition: safecoll.h:1489
iterator(dict_type *dict)
Definition: safecoll.h:1465
PSafePtr< Derived > PSafePtrCast(const PSafePtr< Base > &oldPtr)
Cast the pointer to a different type.
Definition: safecoll.h:1041
This template class maps the PArrayObjects to a specific object type.
Definition: array.h:925
PDebugLocation m_location
Definition: safecoll.h:333
bool m_locked
Definition: safecoll.h:336
virtual void DeleteObject(PSafeObject *obj)
PSafePtr & operator=(const PSafePtr &ptr)
Copy the pointer to the PSafeObject.
Definition: safecoll.h:912
T & operator*() const
Return the physical pointer to the object.
Definition: safecoll.h:986
const_iterator end() const
Definition: safecoll.h:1508
virtual PSafePtr< Base > GetAt(PINDEX idx, PSafetyMode mode=PSafeReadWrite)
Get the instance in the collection of the index.
Definition: safecoll.h:1146
This class defines a thread synchronisation object.
Definition: syncthrd.h:256
#define P_DEPRECATED
Definition: object.h:141
PSafetyMode GetSafetyMode() const
Get the locking mode used by this pointer.
Definition: safecoll.h:670
PSafePtr< Base > value_type
Definition: safecoll.h:1201
const_iterator begin() const
Definition: safecoll.h:1507
virtual void Assign(const PSafePtrMultiThreaded &ptr)
PBoolean SafeDereference()
Decrement the reference count for object.
T * operator--()
Pre-decrement the pointer.
Definition: safecoll.h:1028
Lock a PSafeObject for read only and automatically unlock it when go out of scope.
Definition: safecoll.h:342
Definition: safecoll.h:552
virtual PSafePtr< Base > Find(const Key &key, PSafetyMode mode=PSafeReadWrite) const
Find the instance in the collection of an object with the same value.
Definition: safecoll.h:1306
#define PDECLARE_NOTIFIER(notifierType, notifiee, func)
Declare PNotifier derived class with P_INT_PTR parameter. Uses PDECLARE_NOTIFIER_EXT macro...
Definition: notifier.h:202
bool SetPosition(PINDEX position)
Definition: safecoll.h:1431
Definition: safecoll.h:1463
PINDEX m_position
Definition: safecoll.h:1407
PSafeDictionaryBase(const PSafeDictionaryBase &other)
Copy constructor for safe collection.
Definition: safecoll.h:1229
Ultimate parent class for all objects in the class library.
Definition: object.h:2204
UnlockFn m_unlock
Definition: safecoll.h:335
A collection is a container that collects together descendents of the PObject class.
Definition: contain.h:392
PSafeObject()
Create a thread safe object.
~PSafePtrMultiThreaded()
Unlock and dereference the PSafeObject this is pointing to.
PSafeLockReadWrite(const PSafeObject &object)
Definition: safecoll.h:356
virtual PSafePtr< Base > FindIterator(const Key &key, PSafetyMode mode=PSafeReadWrite) const
Find instance and use PSafePtr as an iterator.
Definition: safecoll.h:1321
virtual PINDEX GetValuesIndex(const PObject &obj) const
Search the collection for the specified value of the object.
PSafePtr< Base > value_type
Definition: safecoll.h:1190
Lock a PSafeObject for read/write and automatically unlock it when go out of scope.
Definition: safecoll.h:353
PSafeLockReadOnly(const PSafeObject &object)
Definition: safecoll.h:345
PSafeDictionaryBase & operator=(const PSafeDictionaryBase &other)
Assign one safe collection to another.
Definition: safecoll.h:1239
virtual void Wait()
Block until the synchronisation object is available.
PSafeColl()
Create a safe list collection wrapper around the real collection.
Definition: safecoll.h:1068
PSafePtr & operator=(const PSafeCollection &safeCollection)
Start an enumerated PSafeObject.
Definition: safecoll.h:922
virtual PBoolean Contains(const Key &key)
Determine of the dictionary contains an entry for the key.
Definition: safecoll.h:1282
bool m_deleteObjects
Definition: safecoll.h:528
bool IsLocked() const
Definition: safecoll.h:328
PSafePtr(T *obj=NULL, PSafetyMode mode=PSafeReference)
Create a new pointer to a PSafeObject.
Definition: safecoll.h:867
const iterator_pair & operator*() const
Definition: safecoll.h:1502
const iterator_pair * operator->() const
Definition: safecoll.h:1501