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

This is the class for pointers to objects that use the smart pointer system. More...

#include <smartptr.h>

Inheritance diagram for PSmartPointer:
Collaboration diagram for PSmartPointer:

Public Member Functions

Construction
 PSmartPointer (PSmartObject *obj=NULL)
 Create a new smart pointer instance and have it point to the specified PSmartObject instance. More...
 
 PSmartPointer (const PSmartPointer &ptr)
 Create a new smart pointer and point it at the data pointed to by the ptr parameter. More...
 
virtual ~PSmartPointer ()
 Destroy the smart pointer and decrement the reference count on the object being pointed to. More...
 
PSmartPointeroperator= (const PSmartPointer &ptr)
 Assign this pointer to the value specified in the ptr parameter. More...
 
Overrides from class PObject
virtual Comparison Compare (const PObject &obj) const
 Determine the relative rank of the pointers. More...
 
Pointer access functions
PBoolean IsNULL () const
 Determine if the smart pointer has been set to point to an actual object instance. More...
 
PSmartObjectGetObject () const
 Get the current value if the internal smart object pointer. 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 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 PrintOn (ostream &strm) const
 Output the contents of the object to the stream. More...
 
virtual void ReadFrom (istream &strm)
 Input the contents of the object from the stream. More...
 

Protected Attributes

PSmartObjectobject
 Object the smart pointer points to. More...
 
- Protected Attributes inherited from PObject
unsigned m_traceContextIdentifier
 

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...
 
- Protected Member Functions inherited from PObject
 PObject ()
 Constructor for PObject, made protected so cannot ever create one on its own. More...
 

Detailed Description

This is the class for pointers to objects that use the smart pointer system.

In conjunction with the PSmartObject class, this class references objects that can have the automatic deletion of the object instance when there are no more smart pointer instances pointing to it.

A PSmartPointer carries the pointer to a PSmartObject instance which contains a reference count. Assigning or copying instances of smart pointers will automatically increment and decrement the reference count. When the last instance that references a PSmartObject instance is destroyed or overwritten, the PSmartObject is deleted.

A NULL value is possible for a smart pointer. It can be detected via the IsNULL() function.

Note that a PSmartPointer is not 100% thread safe. The case of assigment to and from a pointer at the same time is not handled. Consider the assignment code for A = B in one thread and C = A in another, when A was unique at the time of A = B.

if ((object != NULL) && (–object->referenceCount == 0)) context switch here delete object;

object = ptr.object; if (object != NULL) ++object->referenceCount;

The A = B operation in thread 1 is interrupted after it has decremented the count, then C = B then copies the pointer "object" in thread 2. Thread 1 then deletes it leaving C pointing to a deleted pointer.

Constructor & Destructor Documentation

PSmartPointer::PSmartPointer ( PSmartObject obj = NULL)
inline

Create a new smart pointer instance and have it point to the specified PSmartObject instance.

Parameters
objSmart object to point to.
PSmartPointer::PSmartPointer ( const PSmartPointer ptr)

Create a new smart pointer and point it at the data pointed to by the ptr parameter.

The reference count for the object being pointed at is incremented.

Parameters
ptrSmart pointer to make a copy of.
virtual PSmartPointer::~PSmartPointer ( )
virtual

Destroy the smart pointer and decrement the reference count on the object being pointed to.

If there are no more references then the object is deleted.

Member Function Documentation

virtual Comparison PSmartPointer::Compare ( const PObject obj) const
virtual

Determine the relative rank of the pointers.

This is identical to determining the relative rank of the integer values represented by the memory pointers.

Returns
EqualTo if objects point to the same object instance, otherwise LessThan and GreaterThan may be returned depending on the relative values of the memory pointers.
Parameters
objOther smart pointer to compare against.

Reimplemented from PObject.

PSmartObject* PSmartPointer::GetObject ( ) const
inline

Get the current value if the internal smart object pointer.

Returns
pointer to object instance.

References object.

Referenced by PNotifierTemplate< PChannel::AsyncContext & >::GetTarget(), and PNotifierTemplate< PChannel::AsyncContext & >::operator()().

PBoolean PSmartPointer::IsNULL ( ) const
inline

Determine if the smart pointer has been set to point to an actual object instance.

Returns
true if the pointer is NULL.
PSmartPointer& PSmartPointer::operator= ( const PSmartPointer ptr)

Assign this pointer to the value specified in the ptr parameter.

The previous object being pointed to has its reference count decremented as this will no longer point to it. If there are no more references then the object is deleted.

The new object being pointed to after the assignment has its reference count incremented.

Parameters
ptrSmart pointer to assign.

Member Data Documentation

PSmartObject* PSmartPointer::object
protected

Object the smart pointer points to.

Referenced by GetObject(), and PSmartPtr< T >::operator T *().


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