smartptr.h
Go to the documentation of this file.
/*
* smartptr.h
*
* Smart pointer template class.
*
* Portable Tools Library
*
* Copyright (c) 1993-1998 Equivalence Pty. Ltd.
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is Portable Windows Library.
*
* The Initial Developer of the Original Code is Equivalence Pty. Ltd.
*
* Portions are Copyright (C) 1993 Free Software Foundation, Inc.
* All Rights Reserved.
*
* Contributor(s): ______________________________________.
*
* $Revision: 24177 $
* $Author: rjongbloed $
* $Date: 2010-04-05 06:52:04 -0500 (Mon, 05 Apr 2010) $
*/
#ifndef PTLIB_SMARTPTR_H
#define PTLIB_SMARTPTR_H
#include <ptlib.h>
#include <ptlib/object.h>
// "Smart" pointers.
class PSmartObject : public PObject
{
PCLASSINFO(PSmartObject, PObject);
public:
PSmartObject()
:referenceCount(1) { }
protected:
PAtomicInteger referenceCount;
friend class PSmartPointer;
};
class PSmartPointer : public PObject
{
PCLASSINFO(PSmartPointer, PObject);
public:
PSmartPointer(
PSmartObject * obj = NULL
) { object = obj; }
PSmartPointer(
const PSmartPointer & ptr
);
virtual ~PSmartPointer();
PSmartPointer & operator=(
const PSmartPointer & ptr
);
virtual Comparison Compare(
const PObject & obj
) const;
PBoolean IsNULL() const { return object == NULL; }
PSmartObject * GetObject() const { return object; }
protected:
// Member variables
PSmartObject * object;
};
template <class T> class PSmartPtr : public PSmartPointer
{
PCLASSINFO(PSmartPtr, PSmartPointer);
public:
PSmartPtr(T * ptr = NULL)
: PSmartPointer(ptr) { }
T * operator->() const
{ return (T *)PAssertNULL(object); }
T & operator*() const
{ return *(T *)PAssertNULL(object); }
operator T*() const
{ return (T *)object; }
};
#endif // PTLIB_SMARTPTR_H
// End Of File ///////////////////////////////////////////////////////////////
PObject::Compare
virtual Comparison Compare(const PObject &obj) const
Compare the two objects and return their relative rank.
PSmartPointer::object
PSmartObject * object
Object the smart pointer points to.
Definition: smartptr.h:168
PCLASSINFO
#define PCLASSINFO(cls, par)
Declare all the standard PTLib class information.
Definition: object.h:1049
PObject::Comparison
Comparison
Result of the comparison operation performed by the Compare() function.
Definition: object.h:1184
PSmartObject::PSmartObject
PSmartObject()
Construct a new smart object, subject to a PSmartPointer instance referencing it. ...
Definition: smartptr.h:60
PSmartPointer
This is the class for pointers to objects that use the smart pointer system.
Definition: smartptr.h:88
PSmartPtr::PSmartPtr
PSmartPtr(T *ptr=NULL)
Constructor.
Definition: smartptr.h:179
PSmartPtr::operator*
T & operator*() const
Access to the dereferenced smart object in the smart pointer.
Definition: smartptr.h:187
PBoolean
BOOL PBoolean
Definition: object.h:102
PAtomicInteger
This class implements an integer that can be atomically incremented and decremented in a thread-safe ...
Definition: critsec.h:171
PAssertNULL
#define PAssertNULL(ptr)
This macro is used to assert that a pointer must be non-null.
Definition: object.h:220
PSmartObject::PSmartPointer
friend class PSmartPointer
Definition: smartptr.h:70
PSmartPointer::GetObject
PSmartObject * GetObject() const
Get the current value if the internal smart object pointer.
Definition: smartptr.h:162
PSmartPtr::operator->
T * operator->() const
Access to the members of the smart object in the smart pointer.
Definition: smartptr.h:183
PSmartPtr
This template class creates a type safe version of PSmartPointer.
Definition: smartptr.h:174
PSmartPointer::IsNULL
PBoolean IsNULL() const
Determine if the smart pointer has been set to point to an actual object instance.
Definition: smartptr.h:155
object.h
PSmartObject
This is the base class for objects that use the smart pointer system.
Definition: smartptr.h:52
PSmartPointer::PSmartPointer
PSmartPointer(PSmartObject *obj=NULL)
Create a new smart pointer instance and have it point to the specified PSmartObject instance...
Definition: smartptr.h:98
PSmartObject::referenceCount
PAtomicInteger referenceCount
Count of number of instances of PSmartPointer that currently reference the object instance...
Definition: smartptr.h:67
PObject
Ultimate parent class for all objects in the class library.
Definition: object.h:1118