00001 #ifndef SIMPLESMARTPTR_H 00002 #define SIMPLESMARTPTR_H 00003 00004 #include "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 class SimpleSmartPointer< Type > ; 00103 friend class SimpleSmartPointer< const Type > ; 00108 // 00109 // Originally the following template parameter was made a friend. 00110 // This is not allowed by the standard so comment it out: 00111 // 00112 // friend Type ; 00113 // 00114 // Initially hack things by making the necessary classes friends 00115 // even though we don't know really which they are. This is an 00116 // Hideous Hack. 00117 friend class FileEntry ; 00118 friend class Bogus ; 00119 00120 public: 00122 ReferenceCount() : _ref_count( 0 ) {} 00123 00126 ReferenceCount( const ReferenceCount & /*src*/ ) : _ref_count( 0 ) {} 00127 00130 const ReferenceCount &operator= ( const ReferenceCount & /*src*/ ) { return *this; } 00131 private: 00132 00134 void ref() const { ++_ref_count ; } 00135 00137 unsigned int unref() const { return --_ref_count ; } 00138 00140 unsigned int getReferenceCount() const { return _ref_count; } 00141 00143 mutable unsigned short _ref_count ; 00144 }; 00145 00146 00147 00148 } // namespace 00149 00150 #endif 00151 00156 /* 00157 Zipios++ - a small C++ library that provides easy access to .zip files. 00158 Copyright (C) 2000 Thomas Søndergaard 00159 00160 This library is free software; you can redistribute it and/or 00161 modify it under the terms of the GNU Lesser General Public 00162 License as published by the Free Software Foundation; either 00163 version 2 of the License, or (at your option) any later version. 00164 00165 This library is distributed in the hope that it will be useful, 00166 but WITHOUT ANY WARRANTY; without even the implied warranty of 00167 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00168 Lesser General Public License for more details. 00169 00170 You should have received a copy of the GNU Lesser General Public 00171 License along with this library; if not, write to the Free Software 00172 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00173 */