PTLib  Version 2.10.11
PSafePtr< T, BaseClass > Class Template Reference

This class defines a thread-safe enumeration of object in a collection. More...

#include <safecoll.h>

Inheritance diagram for PSafePtr< T, BaseClass >:

Public Member Functions

Construction
 PSafePtr (T *obj=NULL, PSafetyMode mode=PSafeReference)
 Create a new pointer to a PSafeObject. More...
 
 PSafePtr (const PSafeCollection &safeCollection, PSafetyMode mode=PSafeReadWrite, PINDEX idx=0)
 Create a new pointer to a PSafeObject. More...
 
 PSafePtr (const PSafeCollection &safeCollection, PSafetyMode mode, PSafeObject *obj)
 Create a new pointer to a PSafeObject. More...
 
 PSafePtr (const PSafePtr &ptr)
 Copy the pointer to the PSafeObject. More...
 
PSafePtroperator= (const PSafePtr &ptr)
 Copy the pointer to the PSafeObject. More...
 
PSafePtroperator= (const PSafeCollection &safeCollection)
 Start an enumerated PSafeObject. More...
 
PSafePtroperator= (T *obj)
 Set the new pointer to a PSafeObject. More...
 
PSafePtroperator= (PINDEX idx)
 Set the new pointer to a collection index. More...
 
PSafePtr Set (T *obj)
 Set the safe pointer to the specified object. More...
 
Operations
 operator T* () const
 Return the physical pointer to the object. More...
 
T & operator* () const
 Return the physical pointer to the object. More...
 
T * operator-> () const
 Allow access to the physical object the pointer is pointing to. More...
 
T * operator++ (int)
 Post-increment the pointer. More...
 
T * operator++ ()
 Pre-increment the pointer. More...
 
T * operator-- (int)
 Post-decrement the pointer. More...
 
T * operator-- ()
 Pre-decrement the pointer. More...
 

Detailed Description

template<class T, class BaseClass = PSafePtrBase>
class PSafePtr< T, BaseClass >

This class defines a thread-safe enumeration of object in a 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.

There are two modes of safe pointer: one that is enumerating a collection; and one that is independent of the collection that the safe object is in. There are subtle semantics that must be observed in each of these two modes especially when switching from one to the other.

NOTE: the PSafePtr will allow safe and mutexed access to objects but may not be thread safe itself! This depends on the base class being used. If the more efficient PSafePtrBase class is used you should not share PSafePtr instances across threads.

See the PSafeObject class for more details, especially in regards to enumeration of collections.

Constructor & Destructor Documentation

§ PSafePtr() [1/4]

template<class T, class BaseClass = PSafePtrBase>
PSafePtr< T, BaseClass >::PSafePtr ( T *  obj = NULL,
PSafetyMode  mode = PSafeReference 
)
inline

Create a new pointer to a PSafeObject.

An optional locking mode may be provided to lock the object for reading or writing and automatically unlock it on destruction.

Note that this version is not associated with a collection so the ++ and – operators will not work.

Parameters
objPhysical object to point to.
modeLocking mode for the object

§ PSafePtr() [2/4]

template<class T, class BaseClass = PSafePtrBase>
PSafePtr< T, BaseClass >::PSafePtr ( const PSafeCollection safeCollection,
PSafetyMode  mode = PSafeReadWrite,
PINDEX  idx = 0 
)
inline

Create a new pointer to a PSafeObject.

An optional locking mode may be provided to lock the object for reading or writing and automatically unlock it on destruction.

The idx'th entry of the collection is pointed to by this object. If the idx is beyond the size of the collection, the pointer is NULL.

Parameters
safeCollectionCollection pointer will enumerate
modeLocking mode for the object
idxIndex into collection to point to

§ PSafePtr() [3/4]

template<class T, class BaseClass = PSafePtrBase>
PSafePtr< T, BaseClass >::PSafePtr ( const PSafeCollection safeCollection,
PSafetyMode  mode,
PSafeObject obj 
)
inline

Create a new pointer to a PSafeObject.

An optional locking mode may be provided to lock the object for reading or writing and automatically unlock it on destruction.

The obj parameter is only set if it contained in the collection, otherwise the pointer is NULL.

Parameters
safeCollectionCollection pointer will enumerate
modeLocking mode for the object
objInital object in collection to point to

§ PSafePtr() [4/4]

template<class T, class BaseClass = PSafePtrBase>
PSafePtr< T, BaseClass >::PSafePtr ( const PSafePtr< T, BaseClass > &  ptr)
inline

Copy the pointer to the PSafeObject.

This will create a copy of the pointer with the same locking mode and lock on the PSafeObject. It will also increment the reference count on the PSafeObject as well.

Parameters
ptrPointer to copy

Member Function Documentation

§ operator T*()

template<class T, class BaseClass = PSafePtrBase>
PSafePtr< T, BaseClass >::operator T* ( ) const
inline

Return the physical pointer to the object.

§ operator*()

template<class T, class BaseClass = PSafePtrBase>
T& PSafePtr< T, BaseClass >::operator* ( ) const
inline

Return the physical pointer to the object.

§ operator++() [1/2]

template<class T, class BaseClass = PSafePtrBase>
T* PSafePtr< T, BaseClass >::operator++ ( int  )
inline

Post-increment the pointer.

This requires that the pointer has been created with a PSafeCollection object so that it can enumerate the collection.

§ operator++() [2/2]

template<class T, class BaseClass = PSafePtrBase>
T* PSafePtr< T, BaseClass >::operator++ ( )
inline

Pre-increment the pointer.

This requires that the pointer has been created with a PSafeCollection object so that it can enumerate the collection.

§ operator--() [1/2]

template<class T, class BaseClass = PSafePtrBase>
T* PSafePtr< T, BaseClass >::operator-- ( int  )
inline

Post-decrement the pointer.

This requires that the pointer has been created with a PSafeCollection object so that it can enumerate the collection.

§ operator--() [2/2]

template<class T, class BaseClass = PSafePtrBase>
T* PSafePtr< T, BaseClass >::operator-- ( )
inline

Pre-decrement the pointer.

This requires that the pointer has been created with a PSafeCollection object so that it can enumerate the collection.

§ operator->()

template<class T, class BaseClass = PSafePtrBase>
T* PSafePtr< T, BaseClass >::operator-> ( ) const
inline

Allow access to the physical object the pointer is pointing to.

§ operator=() [1/4]

template<class T, class BaseClass = PSafePtrBase>
PSafePtr& PSafePtr< T, BaseClass >::operator= ( const PSafePtr< T, BaseClass > &  ptr)
inline

Copy the pointer to the PSafeObject.

This will create a copy of the pointer with the same locking mode and lock on the PSafeObject. It will also increment the reference count on the PSafeObject as well.

§ operator=() [2/4]

template<class T, class BaseClass = PSafePtrBase>
PSafePtr& PSafePtr< T, BaseClass >::operator= ( const PSafeCollection safeCollection)