# smartptr.h

Go to the documentation of this file.

    00001 /*
    00002  * smartptr.h
    00003  *
    00004  * Smart pointer template class.
    00005  *
    00006  * Portable Tools Library
    00007  *
    00008  * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
    00009  *
    00010  * The contents of this file are subject to the Mozilla Public License
    00011  * Version 1.0 (the "License"); you may not use this file except in
    00012  * compliance with the License. You may obtain a copy of the License at
    00013  * http://www.mozilla.org/MPL/
    00014  *
    00015  * Software distributed under the License is distributed on an "AS IS"
    00016  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
    00017  * the License for the specific language governing rights and limitations
    00018  * under the License.
    00019  *
    00020  * The Original Code is Portable Windows Library.
    00021  *
    00022  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
    00023  *
    00024  * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
    00025  * All Rights Reserved.
    00026  *
    00027  * Contributor(s): ______________________________________.
    00028  *
    00029  * $Revision: 24177 $
    00030  * $Author: rjongbloed $
    00031  * $Date: 2010-04-05 06:52:04 -0500 (Mon, 05 Apr 2010) $
    00032  */
    00033
    00034 #ifndef PTLIB_SMARTPTR_H
    00035 #define PTLIB_SMARTPTR_H
    00036
    00037 #include <ptlib.h>
    00038 #include <ptlib/object.h>
    00039
    00041 // "Smart" pointers.
    00042
    00052 class PSmartObject : public PObject
    00053 {
    00054   PCLASSINFO(PSmartObject, PObject);
    00055
    00056   public:
    00060     PSmartObject()
    00061       :referenceCount(1) { }
    00062
    00063   protected:
    00067     PAtomicInteger referenceCount;
    00068
    00069
    00070   friend class PSmartPointer;
    00071 };
    00072
    00073
    00088 class PSmartPointer : public PObject
    00089 {
    00090   PCLASSINFO(PSmartPointer, PObject);
    00091
    00092   public:
    00098     PSmartPointer(
    00099       PSmartObject * obj = NULL
    00100     ) { object = obj; }
    00101
    00106     PSmartPointer(
    00107       const PSmartPointer & ptr
    00108     );
    00109
    00114     virtual ~PSmartPointer();
    00115
    00126     PSmartPointer & operator=(
    00127       const PSmartPointer & ptr
    00128     );
    00130
    00142     virtual Comparison Compare(
    00143       const PObject & obj
    00144     ) const;
    00146
    00155     PBoolean IsNULL() const { return object == NULL; }
    00156
    00162     PSmartObject * GetObject() const { return object; }
    00164
    00165   protected:
    00166     // Member variables
    00168     PSmartObject * object;
    00169 };
    00170
    00171
    00174 template <class T> class PSmartPtr : public PSmartPointer
    00175 {
    00176   PCLASSINFO(PSmartPtr, PSmartPointer);
    00177   public:
    00179     PSmartPtr(T * ptr = NULL)
    00180       : PSmartPointer(ptr) { }
    00181
    00183     T * operator->() const
    00184       { return (T *)PAssertNULL(object); }
    00185
    00187     T & operator*() const
    00188       { return *(T *)PAssertNULL(object); }
    00189
    00191     operator T*() const
    00192       { return (T *)object; }
    00193 };
    00194
    00195
    00196 #endif // PTLIB_SMARTPTR_H
    00197
    00198
    00199 // End Of File ///////////////////////////////////////////////////////////////