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
00061
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
00083
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
00110
00111
00112
00113
00114
00115
00116
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 }
00149
00150 #endif
00151
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173