Main Page   Class Hierarchy   Compound List   File List   Compound Members  

simplesmartptr.h

Go to the documentation of this file.
00001 #ifndef SIMPLESMARTPTR_H
00002 #define SIMPLESMARTPTR_H
00003 
00004 #include "zipios++/zipios-config.h"
00005 
00006 namespace zipios {
00007 
00014 template< class Type >
00015 class SimpleSmartPointer {
00016 public:
00017   Type *operator-> () const { return _p ;  }
00018 
00019   Type &operator* ()  const { return *_p ; }
00020 
00021   SimpleSmartPointer( Type *p = 0 ) : _p( p ) { ref() ; }
00022 
00023   template< class T2 > SimpleSmartPointer( const SimpleSmartPointer< T2 > &src ) 
00024     : _p( src.get() ) { ref() ; }
00025 
00026   SimpleSmartPointer( const SimpleSmartPointer &src ) : _p( src.get() ) { 
00027     ref() ; 
00028   }
00029 
00030   ~SimpleSmartPointer () { if ( unref() == 0 ) deleteIt() ; }
00031 
00032   template< class T2 > 
00033   SimpleSmartPointer &operator= ( const SimpleSmartPointer< T2 > &src ) {
00034     ref( src.get() ) ;
00035     if ( unref() == 0 )
00036       deleteIt() ;
00037     _p = src.get() ;
00038     return *this ;
00039   }
00040 
00041   SimpleSmartPointer &operator= ( const SimpleSmartPointer &src ) {
00042     ref( src.get() ) ;
00043     if ( unref() == 0 )
00044       deleteIt() ;
00045     _p = src.get() ;
00046     return *this ;
00047   }
00048 
00049   SimpleSmartPointer &operator=( Type *src ) {
00050     _p = src ;
00051     ref() ; 
00052     return *this ;
00053   }
00054 
00055   bool operator== ( const Type *p )                const { return _p == p     ; }
00056   bool operator!= ( const Type *p )                const { return _p != p     ; }
00057   bool operator== ( const SimpleSmartPointer &sp ) const { return _p == sp.get() ; }
00058   bool operator!= ( const SimpleSmartPointer &sp ) const { return _p != sp.get() ; }
00059   bool operator!  ()                               const { return ! _p        ; }
00060   // This next method is inspired by iostream, and is for use with 
00061   // if ( some_smart_pointer ).
00062   operator void*() const { return _p ? (void *)(-1) : (void *)(0) ; }
00063 
00064   Type *get() const { return _p ; }
00065 
00067   unsigned int getReferenceCount() const { return _p->getReferenceCount(); }
00068 
00069 
00070 private:
00071   template< class T2 >
00072   void ref( const T2 *ptr ) { if ( ptr ) ptr->ref() ; }
00073 
00074   void ref() const { if ( _p ) _p->ref() ; }
00075   unsigned int unref() const {
00076     if ( _p )
00077       return _p->unref();
00078     else
00079       return 0 ;
00080   }
00081   void deleteIt() {
00082 //      if( _p )
00083 //        cerr << "SimpleSmartPointer: Deleting object!" << endl ;
00084     delete _p ;
00085   }
00086   Type *_p ;
00087 };
00088 
00089 
00098 template< class Type >
00099 class ReferenceCount {
00102   friend SimpleSmartPointer< Type > ;
00103   friend SimpleSmartPointer< const Type > ;
00108   friend Type ;
00109 public:
00111   ReferenceCount() : _ref_count( 0 ) {}
00112 
00115   ReferenceCount( const ReferenceCount &src ) : _ref_count( 0 ) {}
00116 
00119   const ReferenceCount &operator= ( const ReferenceCount &src ) {}
00120 private:
00121 
00123   void ref() const           { ++_ref_count ;        }
00124 
00126   unsigned int unref() const { return --_ref_count ; }
00127 
00129   unsigned int getReferenceCount() const { return _ref_count; }
00130 
00132   mutable unsigned short _ref_count ;
00133 };
00134 
00135 
00136 
00137 } // namespace
00138 
00139 #endif
00140 
00145 /*
00146   Zipios++ - a small C++ library that provides easy access to .zip files.
00147   Copyright (C) 2000  Thomas Søndergaard
00148   
00149   This library is free software; you can redistribute it and/or
00150   modify it under the terms of the GNU Lesser General Public
00151   License as published by the Free Software Foundation; either
00152   version 2 of the License, or (at your option) any later version.
00153   
00154   This library is distributed in the hope that it will be useful,
00155   but WITHOUT ANY WARRANTY; without even the implied warranty of
00156   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00157   Lesser General Public License for more details.
00158   
00159   You should have received a copy of the GNU Lesser General Public
00160   License along with this library; if not, write to the Free Software
00161   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00162 */

Generated at Tue Aug 14 20:39:26 2001 for Zipios++ by doxygen1.2.0 written by Dimitri van Heesch, © 1997-2000