// ============================================================ // // refCount.h // // Copyright 2002, Dennis Meilicke and Rene Peralta // // ============================================================ // // Description: // // Implementation of the reference counting class. // // ============================================================ #ifndef __ln3_refCount__ #define __ln3_refCount__ template< class DATA > class refCount { public: refCount( ); refCount( const refCount< DATA > &source ); ~refCount( ); refCount< DATA >& operator=( const refCount< DATA > &source ); void Disconnect( ); // temporary only unsigned long GetPointer( void ){return (unsigned long)pData;} protected: inline const DATA* const GetData( ) const; inline DATA* const GetData( ); private: inline void ReleaseReference( ); DATA* pData; }; template< class DATA > inline const DATA* const refCount< DATA >::GetData( ) const { return pData; } template< class DATA > inline DATA* const refCount< DATA >::GetData( ) { if( pData->count > 1 ) { DATA* pRefData = new DATA(*pData); ReleaseReference( ); pData = pRefData; } return pData; } template< class DATA > refCount< DATA >::refCount( ) { pData = new DATA; } template< class DATA > refCount< DATA >::refCount( const refCount< DATA > &source ) { pData = source.pData; ++pData->count; } template< class DATA > refCount< DATA >::~refCount( ) { ReleaseReference( ); } template< class DATA > refCount< DATA >& refCount< DATA >::operator=( const refCount< DATA > &source ) { if( source.pData != pData ) { ReleaseReference( ); pData = source.pData; ++pData->count; } return( *this ); } template< class DATA > inline void refCount< DATA >::ReleaseReference( ) { --pData->count; if( ( pData->count ) < 0 ) cout << "less than zero" << endl; if( pData->count == 0 ) { delete pData; pData = (DATA *)0; } } template< class DATA > void refCount< DATA >::Disconnect( ) { ReleaseReference( ); pData = new DATA; } #endif