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

GCPtr.h

Go to the documentation of this file.
00001 /* Copyright (C) 2002 Carsten Griwodz 00002 * 00003 * You are allowed to use all other parts of the code under the following terms: 00004 * 00005 * For non-commercial use, this code may be used in unmodified form 00006 * provided that this copyright notice and this permission notice appear 00007 * in supporting documentation. 00008 * 00009 * The code may be made subject to the terms of the GNU General Public 00010 * License, Version 2, and re-distributed under the terms of this license. 00011 * 00012 * Commercial use other than under the terms of the GNU General Public 00013 * License is allowed only after negotiation of conditions with the authors. 00014 */ 00015 #ifndef OS_GC_PTR_H 00016 #define OS_GC_PTR_H 00017 00018 #include <stdio.h> 00019 #include <assert.h> 00020 00021 namespace OS 00022 { 00023 00042 template <class T> 00043 class GCPtr 00044 { 00045 public: 00046 GCPtr( ) 00047 : _pointee( NULL ) 00048 , _copies( NULL ) 00049 { 00050 } 00051 00062 explicit GCPtr( T* pointee ) 00063 : _pointee( pointee ) 00064 , _copies( NULL ) 00065 { 00066 if( pointee != NULL ) 00067 { 00068 _copies = new size_t; 00069 *_copies = 1; 00070 } 00071 } 00072 00073 GCPtr( const GCPtr& other ) 00074 : _pointee( NULL ) 00075 , _copies( NULL ) 00076 { 00077 copy( other ); 00078 } 00079 00080 GCPtr& operator=( const GCPtr& other ) 00081 { 00082 reduce( ); 00083 copy( other ); 00084 return *this; 00085 } 00086 00087 GCPtr& operator=( T* pointee ) 00088 { 00089 reduce( ); 00090 _pointee = pointee; 00091 if( pointee != NULL ) 00092 { 00093 _copies = new size_t; 00094 *_copies = 1; 00095 } 00096 return *this; 00097 } 00098 00099 ~GCPtr( ) 00100 { 00101 reduce( ); 00102 } 00103 00104 T& operator*( ) const 00105 { 00106 return *_pointee; 00107 } 00108 00109 T* operator->( ) const 00110 { 00111 return _pointee; 00112 } 00113 00114 inline bool isNull( ) const { 00115 assert( isConsistent() ); 00116 return ( _pointee == NULL ); 00117 } 00118 00119 private: 00120 bool operator==( T* pointee ) 00121 { 00122 return ( _pointee == pointee ); 00123 } 00124 00125 bool operator!=( T* pointee ) 00126 { 00127 return ( _pointee != pointee ); 00128 } 00129 00130 bool operator==( const GCPtr& other ) 00131 { 00132 return ( _pointee == other._pointee ); 00133 } 00134 00135 bool operator!=( const GCPtr& other ) 00136 { 00137 return ( _pointee != other._pointee ); 00138 } 00139 00140 bool isConsistent( ) const 00141 { 00142 if( _pointee == NULL && _copies == NULL ) return true; 00143 if( _pointee != NULL && _copies != NULL ) return true; 00144 return false; 00145 } 00146 00147 private: 00148 T* _pointee; 00149 mutable size_t* _copies; 00150 00151 private: 00157 void copy( const GCPtr& other ) 00158 { 00159 _pointee = other._pointee; 00160 _copies = other._copies; 00161 if( _copies ) 00162 { 00163 *_copies += 1; 00164 } 00165 } 00166 00173 void reduce( ) 00174 { 00175 assert( isConsistent() ); 00176 if( _copies ) 00177 { 00178 assert( *_copies > 0 ); 00179 *_copies -= 1; 00180 if( *_copies == 0 ) 00181 { 00182 delete _pointee; 00183 delete _copies; 00184 _copies = NULL; 00185 _pointee = NULL; 00186 } 00187 } 00188 else 00189 { 00190 _pointee = NULL; 00191 } 00192 } 00193 }; 00194 00195 }; // namespace OS 00196 00197 #endif /* OS_GC_PTR_H */ 00198

Generated on Sun Mar 6 13:35:49 2005 for Komssys by doxygen 1.3.8