[Cppunit-cvs] cppunit2/include/cpptl intrusiveptr.h,NONE,1.1 forwards.h,1.5,1.6
Brought to you by:
blep
From: Baptiste L. <bl...@us...> - 2005-07-20 21:07:31
|
Update of /cvsroot/cppunit/cppunit2/include/cpptl In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv8806/include/cpptl Modified Files: forwards.h Added Files: intrusiveptr.h Log Message: * Added IntrusivePtr, a reference counted smart-pointer * Changed Test hierarchy and TestFixture to use IntrusivePtr instead of SharedPtr (this allows simple upcasting). Index: forwards.h =================================================================== RCS file: /cvsroot/cppunit/cppunit2/include/cpptl/forwards.h,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** forwards.h 4 Jul 2005 08:09:30 -0000 1.5 --- forwards.h 20 Jul 2005 21:06:49 -0000 1.6 *************** *** 24,27 **** --- 24,32 ---- class Functor0R; + // intrusiveptr.h + class IntrusiveCount; + template<class PointeeType> + class IntrusivePtr; + // reflection.h class Attribut; --- NEW FILE: intrusiveptr.h --- #ifndef CPPTL_INTRUSIVEPTR_H_INCLUDED # define CPPTL_INTRUSIVEPTR_H_INCLUDED # include <cpptl/forwards.h> # include <cpptl/atomiccounter.h> // We use inheritance instead of the typedef in a struct to // simulate the template typedef because the typedef in a struct // trick does not support type deduction in template functions. // If CPPTL_USE_BOOST_INTRUSIVE_PTR is defined, then we use boost::intrusive_ptr // as our smart_pointer, otherwise we use a simple implementation // of our own. namespace CppTL { /// Instrusive counter base class for use with InstrusivePtr. class IntrusiveCount { public: virtual ~IntrusiveCount() { } void incrementReferenceCount() { count_.increment(); } void releaseReferenceCount() { if ( !count_.decrement() ) delete this; } private: mutable AtomicCounter count_; }; } # ifndef CPPTL_USE_BOOST_SHARED_PTR namespace CppTL { namespace Impl { /// Smart-pointer base class to avoid template code bloat class IntrusivePtrBase { public: operator bool() const { return p_ != 0; } bool operator !() const { return p_ == 0; } protected: IntrusivePtrBase() : p_( 0 ) { } IntrusivePtrBase( void *p ) : p_( p ) { } IntrusivePtrBase( const IntrusivePtrBase &other ) : p_( other.p_ ) { } void swap( IntrusivePtrBase &other ) { CppTL::swap( p_, other.p_ ); } void *get() const { return p_; } void *deref() const { // assert( p_ != 0 ) return p_; } //private: // Friend template function is not well supported // Private access required by function staticPointerCast<> public: void *p_; }; } // namespace Impl inline void instrusivePtrAddRef( IntrusiveCount *p ) { p->incrementReferenceCount(); } inline void intrusivePtrRelease( IntrusiveCount *p ) { p->releaseReferenceCount(); } template<class PointeeType> class IntrusivePtr : public Impl::IntrusivePtrBase { public: typedef IntrusivePtr<PointeeType> ThisType; typedef Impl::IntrusivePtrBase SuperClass; IntrusivePtr() { } IntrusivePtr( PointeeType *p ) : SuperClass( p ) { if ( p ) instrusivePtrAddRef( p ); } IntrusivePtr( const ThisType &other ) : Impl::IntrusivePtrBase( other ) { if ( p_ ) instrusivePtrAddRef( get() ); } ~IntrusivePtr() { if ( p_ ) intrusivePtrRelease( get() ); } void reset() { IntrusivePtr tmp; tmp.swap( *this ); } void reset( PointeeType *p ) { IntrusivePtr tmp( p ); tmp.swap( *this ); } PointeeType *get() const { return static_cast<PointeeType *>( IntrusivePtrBase::get() ); } void swap( IntrusivePtr &other ) { SuperClass::swap( other ); } ThisType &operator =( const IntrusivePtr &other ) { ThisType tmp( other ); swap( tmp ); return *this; } PointeeType &operator *() const { // assert( p_ != 0 ) return *( static_cast<PointeeType *>( p_ ) ); } PointeeType *operator ->() const { return static_cast<PointeeType *>( SuperClass::deref() ); } }; template<class T, class U> IntrusivePtr<T> staticPointerCast( const IntrusivePtr<U> & r) { return IntrusivePtr<T>( static_cast<T*>( r.get() ) ); } } // namespace CppTL # else // ifndef CPPTL_USE_BOOST_SHARED_PTR # include <boost/shared_ptr.hpp> namespace CppTL { template<class PointeeType> class IntrusivePtr : public ::boost::intrusive_ptr<PointeeType> { public: typedef IntrusivePtr<PointeeType> ThisType; IntrusivePtr() { } IntrusivePtr( PointeeType *p ) : ::boost::intrusive_ptr( p ) { } IntrusivePtr( const ::boost::intrusive_ptr<PointeeType> &other ) : ::boost::intrusive_ptr( other ) { } ThisType &operator =( const ::boost::intrusive_ptr<PointeeType> &other ) { return ::boost::intrusive_ptr<PointeeType>::operator =( other ); } }; template<class T, class U> IntrusivePtr<T> staticPointerCast( const ::boost::intrusive_ptr<U> & r) { return IntrusivePtr<T>( ::boost::static_pointer_cast<T>( r ) ); } } // namespace CppTL namespace boost { inline void intrusive_ptr_add_ref( CppTL::IntrusiveCount *p ) { p->incrementReferenceCount(); } inline void intrusive_ptr_release( CppTL::IntrusiveCount *p ) { p->releaseReferenceCount(); } } // namespace boost # endif // ifndef CPPTL_USE_BOOST_SHARED_PTR #endif // CPPTL_INTRUSIVEPTR_H_INCLUDED |