PTLib
Version 2.18.8
|
This is the class for pointers to objects that use the smart pointer system. More...
#include <smartptr.h>
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... | |
PSmartPointer & | operator= (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... | |
PSmartObject * | GetObject () const |
Get the current value if the internal smart object pointer. More... | |
![]() | |
__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 PObject * | PTraceObjectInstance () const |
virtual PObject * | Clone () 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 | |
PSmartObject * | object |
Object the smart pointer points to. More... | |
![]() | |
unsigned | m_traceContextIdentifier |
Additional Inherited Members | |
![]() | |
enum | Comparison { LessThan = -1, EqualTo = 0, GreaterThan = 1 } |
Result of the comparison operation performed by the Compare() function. More... | |
![]() | |
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 PObject * | PTraceObjectInstance (const char *) |
static __inline const PObject * | PTraceObjectInstance (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... | |
![]() | |
PObject () | |
Constructor for PObject, made protected so cannot ever create one on its own. More... | |
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.
|
inline |
Create a new smart pointer instance and have it point to the specified PSmartObject
instance.
obj | Smart 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.
ptr | Smart pointer to make a copy of. |
|
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.
|
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.
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. obj | Other smart pointer to compare against. |
Reimplemented from PObject.
|
inline |
Get the current value if the internal smart object pointer.
References object.
Referenced by PNotifierTemplate< PChannel::AsyncContext & >::GetTarget(), and PNotifierTemplate< PChannel::AsyncContext & >::operator()().
|
inline |
Determine if the smart pointer has been set to point to an actual object instance.
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.
ptr | Smart pointer to assign. |
|
protected |
Object the smart pointer points to.
Referenced by GetObject(), and PSmartPtr< T >::operator T *().