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

osharedpointer.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) 2004 Holger Hans Peter Freyther <freyther@handhelds.org>
00006                             Copyright (C) The Opie Team <opie-devel@handhelds.org>
00007              =.
00008            .=l.
00009           .>+-=
00010 _;:,     .>    :=|.         This program is free software; you can
00011 .> <`_,   >  .   <=         redistribute it and/or  modify it under
00012 :`=1 )Y*s>-.--   :          the terms of the GNU Library General Public
00013 .="- .-=="i,     .._        License as published by the Free Software
00014 - .   .-<_>     .<>         Foundation; either version 2 of the License,
00015     ._= =}       :          or (at your option) any later version.
00016    .%`+i>       _;_.
00017    .i_,=:_.      -<s.       This program is distributed in the hope that
00018     +  .  -:.       =       it will be useful,  but WITHOUT ANY WARRANTY;
00019    : ..    .:,     . . .    without even the implied warranty of
00020    =_        +     =;=|`    MERCHANTABILITY or FITNESS FOR A
00021  _.=:.       :    :=>`:     PARTICULAR PURPOSE. See the GNU
00022 ..}^=.=       =       ;     Library General Public License for more
00023 ++=   -.     .`     .:      details.
00024 :     =  ...= . :.=-
00025 -.   .:....=;==+<;          You should have received a copy of the GNU
00026  -_. . .   )=.  =           Library General Public License along with
00027    --        :-=`           this library; see the file COPYING.LIB.
00028                             If not, write to the Free Software Foundation,
00029                             Inc., 59 Temple Place - Suite 330,
00030                             Boston, MA 02111-1307, USA.
00031 */
00032 
00033 
00034 #ifndef O_SHARED_POINTER_H
00035 #define O_SHARED_POINTER_H
00036 
00037 #include <opie2/osmartpointer.h>
00038 
00045 namespace Opie {
00046 namespace Core {
00047 template<class T>
00048 class OSharedPointerData : public ORefCount {
00049 public:
00050     OSharedPointerData( T* dt ) {
00051         data = dt;
00052     }
00053 
00054     ~OSharedPointerData() {
00055         delete data;
00056     }
00057 
00058     T *data;
00059 };
00060 
00061 
00068 template<class T>
00069 class OSharedPointer {
00070     typedef OSharedPointerData<T> Data;
00071     Data* data;
00072 
00073     void unref() {
00074         if (data) { data->Decr(); if (!data->Shared()) delete data; }
00075         data = 0;
00076     }
00077 
00078 public:
00079     OSharedPointer () { data = 0;}
00080 
00081     OSharedPointer( T* t ) {
00082         data = new Data( t );
00083         data->Incr();
00084     }
00085 
00086     OSharedPointer( const OSharedPointer<T>& p ) {
00087         if ( data = p.data ) data->Incr();
00088     }
00089 
00090     ~OSharedPointer() {
00091         unref();
00092     }
00093 
00094     OSharedPointer<T> &operator=( const OSharedPointer<T>& p ) {
00095         // already same: nothing to do
00096         if (data == p.data) return *this;
00097         // decouple reference
00098         unref();
00099         // establish new reference
00100         if (data = p.data) data->Incr();
00101         return *this;
00102     }
00103 
00104     OSharedPointer<T> &operator=( T *p ) {
00105         unref();
00106         data = new Data( p );
00107         data->Incr();
00108 
00109         return *this;
00110     }
00111 
00112     operator T* () const { return data->data;  }
00113     T& operator*()       { return *data->data; }
00114     const T& operator*()const { return data->data; }
00115 
00117     T* operator-> () {return data->data; }
00119     const T* operator-> ()const {return data->data; }
00120 
00121 
00123     operator bool () const { return (data != 0 && data->data != 0); }
00125     operator bool () { return ( data != 0 && data->data != NULL );}
00126 
00128     bool operator! () const { return (data == 0 || data->data == 0); }
00130     bool operator! () { return (data == 0 || data->data == 0); }
00131 };
00132 
00133 }
00134 }
00135 
00136 #endif

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