From af6891221673adca98343e9a0a7bbba0ff558813 Mon Sep 17 00:00:00 2001 From: syntheticpp Date: Sun, 25 Feb 2007 12:49:19 +0000 Subject: [PATCH] move macro switched code into the header to fix linker errors. By this we don't need special libraries for the #if defined (LOKI_OBJECT_LEVEL_THREADING) || defined (LOKI_CLASS_LEVEL_THREADING) case. git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@807 7ec92016-0320-0410-acc4-a06ded1c099a --- include/loki/StrongPtr.h | 73 ++++++++++++++++++++++++++++--- src/StrongPtr.cpp | 92 ---------------------------------------- 2 files changed, 67 insertions(+), 98 deletions(-) diff --git a/include/loki/StrongPtr.h b/include/loki/StrongPtr.h index 9f58238..d521dae 100644 --- a/include/loki/StrongPtr.h +++ b/include/loki/StrongPtr.h @@ -621,11 +621,33 @@ private: class LOKI_EXPORT LockableTwoRefCounts { + typedef SmallValueObject< ::Loki::ClassLevelLockable > ThreadSafePointerAllocator; + protected: - explicit LockableTwoRefCounts( bool strong ); + explicit LockableTwoRefCounts( bool strong ) + : m_counts( NULL ) + { + void * temp = ThreadSafePointerAllocator::operator new( + sizeof(Loki::Private::LockableTwoRefCountInfo) ); +#ifdef DO_EXTRA_LOKI_TESTS + assert( temp != 0 ); +#endif + m_counts = new ( temp ) Loki::Private::LockableTwoRefCountInfo( strong ); + } - LockableTwoRefCounts( const void * p, bool strong ); + LockableTwoRefCounts( const void * p, bool strong ) + : m_counts( NULL ) + { + void * temp = ThreadSafePointerAllocator::operator new( + sizeof(Loki::Private::LockableTwoRefCountInfo) ); +#ifdef DO_EXTRA_LOKI_TESTS + assert( temp != 0 ); +#endif + void * p2 = const_cast< void * >( p ); + m_counts = new ( temp ) + Loki::Private::LockableTwoRefCountInfo( p2, strong ); + } LockableTwoRefCounts( const LockableTwoRefCounts & rhs, bool strong ) : m_counts( rhs.m_counts ) @@ -648,23 +670,62 @@ protected: return Decrement( strong ); } - void Increment( bool strong ); + void Increment( bool strong ) + { + if ( strong ) + { + m_counts->IncStrongCount(); + } + else + { + m_counts->IncWeakCount(); + } + } - bool Decrement( bool strong ); + bool Decrement( bool strong ) + { + if ( strong ) + { + m_counts->DecStrongCount(); + } + else + { + m_counts->DecWeakCount(); + } + return !m_counts->HasStrongPointer(); + } bool HasStrongPointer( void ) const { return m_counts->HasStrongPointer(); } - void Swap( LockableTwoRefCounts & rhs ); + void Swap( LockableTwoRefCounts & rhs ) + { + std::swap( m_counts, rhs.m_counts ); + } void SetPointer( void * p ) { m_counts->SetPointer( p ); } - void ZapPointer( void ); + void ZapPointer( void ) + { +#ifdef DO_EXTRA_LOKI_TESTS + assert( !m_counts->HasStrongPointer() ); +#endif + if ( m_counts->HasWeakPointer() ) + { + m_counts->ZapPointer(); + } + else + { + ThreadSafePointerAllocator::operator delete ( m_counts, + sizeof(Loki::Private::LockableTwoRefCountInfo) ); + m_counts = NULL; + } + } inline void * GetPointer( void ) const { diff --git a/src/StrongPtr.cpp b/src/StrongPtr.cpp index 31403d6..4591c81 100644 --- a/src/StrongPtr.cpp +++ b/src/StrongPtr.cpp @@ -110,98 +110,6 @@ void TwoRefCounts::ZapPointer( void ) } } -// ---------------------------------------------------------------------------- - -#if defined (LOKI_OBJECT_LEVEL_THREADING) || defined (LOKI_CLASS_LEVEL_THREADING) - -typedef SmallValueObject< ::Loki::ClassLevelLockable > ThreadSafePointerAllocator; - -// ---------------------------------------------------------------------------- - -LockableTwoRefCounts::LockableTwoRefCounts( bool strong ) - : m_counts( NULL ) -{ - void * temp = ThreadSafePointerAllocator::operator new( - sizeof(Loki::Private::LockableTwoRefCountInfo) ); -#ifdef DO_EXTRA_LOKI_TESTS - assert( temp != 0 ); -#endif - m_counts = new ( temp ) Loki::Private::LockableTwoRefCountInfo( strong ); -} - -// ---------------------------------------------------------------------------- - -LockableTwoRefCounts::LockableTwoRefCounts( const void * p, bool strong ) - : m_counts( NULL ) -{ - void * temp = ThreadSafePointerAllocator::operator new( - sizeof(Loki::Private::LockableTwoRefCountInfo) ); -#ifdef DO_EXTRA_LOKI_TESTS - assert( temp != 0 ); -#endif - void * p2 = const_cast< void * >( p ); - m_counts = new ( temp ) - Loki::Private::LockableTwoRefCountInfo( p2, strong ); -} - -// ---------------------------------------------------------------------------- - -void LockableTwoRefCounts::Increment( bool strong ) -{ - if ( strong ) - { - m_counts->IncStrongCount(); - } - else - { - m_counts->IncWeakCount(); - } -} - -// ---------------------------------------------------------------------------- - -bool LockableTwoRefCounts::Decrement( bool strong ) -{ - if ( strong ) - { - m_counts->DecStrongCount(); - } - else - { - m_counts->DecWeakCount(); - } - return !m_counts->HasStrongPointer(); -} - -// ---------------------------------------------------------------------------- - -void LockableTwoRefCounts::Swap( LockableTwoRefCounts & rhs ) -{ - std::swap( m_counts, rhs.m_counts ); -} - -// ---------------------------------------------------------------------------- - -void LockableTwoRefCounts::ZapPointer( void ) -{ -#ifdef DO_EXTRA_LOKI_TESTS - assert( !m_counts->HasStrongPointer() ); -#endif - if ( m_counts->HasWeakPointer() ) - { - m_counts->ZapPointer(); - } - else - { - ThreadSafePointerAllocator::operator delete ( m_counts, - sizeof(Loki::Private::LockableTwoRefCountInfo) ); - m_counts = NULL; - } -} - -// ---------------------------------------------------------------------------- - -#endif // if object-level-locking or class-level-locking // ----------------------------------------------------------------------------