Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

osmartpointer.h

Go to the documentation of this file.
00001 // -*- Mode: C++; -*-
00002 /*
00003                             This file is part of the Opie Project
00004                             Copyright (C) 2004 Rajko Albrecht <alwin@handhelds.org>
00005                             Copyright (C) The Opie Team <opie-devel@handhelds.org>
00006              =.
00007            .=l.
00008           .>+-=
00009 _;:,     .>    :=|.         This program is free software; you can
00010 .> <`_,   >  .   <=         redistribute it and/or  modify it under
00011 :`=1 )Y*s>-.--   :          the terms of the GNU Library General Public
00012 .="- .-=="i,     .._        License as published by the Free Software
00013 - .   .-<_>     .<>         Foundation; either version 2 of the License,
00014     ._= =}       :          or (at your option) any later version.
00015    .%`+i>       _;_.
00016    .i_,=:_.      -<s.       This program is distributed in the hope that
00017     +  .  -:.       =       it will be useful,  but WITHOUT ANY WARRANTY;
00018    : ..    .:,     . . .    without even the implied warranty of
00019    =_        +     =;=|`    MERCHANTABILITY or FITNESS FOR A
00020  _.=:.       :    :=>`:     PARTICULAR PURPOSE. See the GNU
00021 ..}^=.=       =       ;     Library General Public License for more
00022 ++=   -.     .`     .:      details.
00023 :     =  ...= . :.=-
00024 -.   .:....=;==+<;          You should have received a copy of the GNU
00025  -_. . .   )=.  =           Library General Public License along with
00026    --        :-=`           this library; see the file COPYING.LIB.
00027                             If not, write to the Free Software Foundation,
00028                             Inc., 59 Temple Place - Suite 330,
00029                             Boston, MA 02111-1307, USA.
00030 */
00031 
00032 #ifndef _OSmartPointer_h
00033 #define _OSmartPointer_h
00034 
00035 #include <stddef.h>
00036 
00044 namespace Opie {
00045 namespace Core {
00046 
00048 class ORefCount {
00049 protected:
00051     unsigned long m_RefCount;
00052 public:
00054     ORefCount();
00055     virtual ~ORefCount();
00057     void Incr();
00059     void Decr();
00061     bool Shared();
00062 };
00063 
00065 template<class T> class OSmartPointer {
00067 
00072     T *ptr;
00073 public:
00075     OSmartPointer() { ptr = NULL; }
00077 
00081     ~OSmartPointer()
00082     {
00083         if (ptr){
00084             ptr->Decr();
00085             if (!ptr->Shared())
00086                 delete ptr;
00087         }
00088     }
00090     OSmartPointer(T* t) { if (ptr = t) ptr->Incr(); }
00092     OSmartPointer(const OSmartPointer<T>& p)
00093     { if (ptr = p.ptr) ptr->Incr(); }
00095     OSmartPointer<T>& operator= (const OSmartPointer<T>& p)
00096     {
00097         // already same: nothing to do
00098         if (ptr == p.ptr) return *this;
00099         // decouple reference
00100         if (ptr) { ptr->Decr(); if (!ptr->Shared()) delete ptr; }
00101         // establish new reference
00102         if (ptr = p.ptr) ptr->Incr();
00103         return *this;
00104     }
00105     OSmartPointer<T>& operator= (T*p)
00106     {
00107         if (ptr==p)return *this;
00108         if (ptr) {
00109             ptr->Decr();
00110             if (!ptr->Shared()) delete ptr;
00111         }
00112         if (ptr=p) ptr->Incr();
00113         return *this;
00114     }
00115 
00117     operator T* () const { return ptr; }
00118 
00120     T& operator* () {return *ptr; }
00122     const T& operator* ()const {return *ptr; }
00123 
00125     T* operator-> () {return ptr; }
00127     const T* operator-> ()const {return ptr; }
00128 
00130     operator bool () const { return (ptr != NULL); }
00132     operator bool () { return ptr != NULL;}
00133 
00135     bool operator! () const { return (ptr == NULL); }
00137     bool operator! () { return (ptr == NULL); }
00138 };
00139 
00140 }
00141 }
00142 
00143 #endif
00144 

Generated on Sat Nov 5 16:16:10 2005 for OPIE by  doxygen 1.4.2