00001 #ifndef __SHARED_POINTER_H__ 00002 #define __SHARED_POINTER_H__ 00003 00004 #include <utilities/allocator.h> 00005 00006 namespace nitro 00007 { 00008 00021 class SharedPointerInterface{ 00022 00023 public: 00024 00035 virtual ~SharedPointerInterface(){}; 00036 00051 virtual void * GetDataAddress( void ) = 0; 00052 }; 00053 00066 template< class managed_type >class SharedPointer : public SharedPointerInterface{ 00067 public: 00068 00079 SharedPointer( void ); 00080 00095 SharedPointer( managed_type * p ); 00096 00111 SharedPointer( const SharedPointer & p ); 00112 00127 void Reset( managed_type * p ); 00128 00143 SharedPointer< managed_type > operator=( const SharedPointer< managed_type > & p ); 00144 00159 managed_type * GetData( void ); 00160 00175 virtual void * GetDataAddress( void ); 00176 00187 managed_type * operator->( void ); 00188 00199 managed_type & operator*( void ); 00200 00211 operator managed_type * ( void ); 00212 00223 virtual ~SharedPointer(); 00224 00225 private: 00226 00237 int * ReferenceCounter; 00238 00249 managed_type * Data; 00250 00261 void Release( void ); 00262 00273 nitro::Allocator * DefaultAllocator; 00274 00275 }; 00276 00277 template< class managed_type >SharedPointer< managed_type >::SharedPointer( void ) 00278 { 00279 DefaultAllocator = nitro::GetAllocator(); 00280 ReferenceCounter = new int; 00281 * ReferenceCounter = 1; 00282 Data = DefaultAllocator->AllocateBlock(); 00283 } 00284 00285 template< class managed_type >SharedPointer< managed_type >::SharedPointer( managed_type * p ) 00286 { 00287 DefaultAllocator = nitro::GetAllocator(); 00288 ReferenceCounter = new int; 00289 * ReferenceCounter = 1; 00290 Data = p; 00291 } 00292 00293 template< class managed_type >SharedPointer< managed_type >::SharedPointer( const SharedPointer & p ) 00294 { 00295 DefaultAllocator = nitro::GetAllocator(); 00296 ReferenceCounter = p.ReferenceCounter; 00297 Data = p.Data; 00298 ( * ReferenceCounter )++; 00299 } 00300 00301 template< class managed_type >void SharedPointer< managed_type >::Reset( managed_type * p ) 00302 { 00303 Release(); 00304 00305 ReferenceCounter = new int; 00306 * ReferenceCounter = 1; 00307 Data = p; 00308 } 00309 00310 template< class managed_type >SharedPointer< managed_type > SharedPointer< managed_type >::operator=( const SharedPointer< managed_type > & p ) 00311 { 00312 if( this != & p ) 00313 { 00314 Release(); 00315 00316 ReferenceCounter = p.ReferenceCounter; 00317 Data = p.Data; 00318 ( * ReferenceCounter )++; 00319 } 00320 00321 return( * this ); 00322 } 00323 00324 template< class managed_type >managed_type * SharedPointer< managed_type >::GetData( void ) 00325 { 00326 return( Data ); 00327 } 00328 00329 template< class managed_type >void * SharedPointer< managed_type >::GetDataAddress( void ) 00330 { 00331 return( ( void * )Data ); 00332 } 00333 00334 template< class managed_type >managed_type * SharedPointer< managed_type >::operator->( void ) 00335 { 00336 return( Data ); 00337 } 00338 00339 template< class managed_type >managed_type & SharedPointer< managed_type >::operator*( void ) 00340 { 00341 return( * Data ); 00342 } 00343 00344 template< class managed_type > SharedPointer< managed_type >::operator managed_type * ( void ) 00345 { 00346 return( Data ); 00347 } 00348 00349 template< class managed_type >/* virtual */ SharedPointer< managed_type >::~SharedPointer() 00350 { 00351 Release(); 00352 } 00353 00354 template< class managed_type >void SharedPointer< managed_type >::Release( void ) 00355 { 00356 if( * ReferenceCounter == 1 ) 00357 { 00358 delete ReferenceCounter; 00359 ReferenceCounter = 0; 00360 00361 //delete Data; 00362 DefaultAllocator->DeallocateBlock( Data ); 00363 Data = 0; 00364 } 00365 else 00366 { 00367 ( * ReferenceCounter )--; 00368 } 00369 } 00370 } 00371 00372 #endif