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 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 }
00138
00139 #endif
00140
00145 00146 00147 00148 00149 00150 00151 00152 00153 00154 00155 00156 00157 00158 00159 00160 00161 00162