/export/rook2/users/build/gfcppsancout64/product/include/gfcpp/SharedPtr.hpp

Go to the documentation of this file.
00001 #ifndef _GEMFIRE_SHAREDPTR_HPP_
00002 #define _GEMFIRE_SHAREDPTR_HPP_
00003 
00004 /*=========================================================================
00005  * (c) Copyright 2004-2007, GemStone Systems, Inc. All Rights Reserved.
00006  * 1260 NW Waterhouse Ave Suite 200, Beaverton OR 97006.
00007  *=========================================================================
00008  */
00009 
00010 #include "SharedBase.hpp"
00011 #include "Assert.hpp"
00012 #include "TypeHelper.hpp"
00013 #include <typeinfo>
00014 #include "SharedPtrHelper.hpp"
00015 
00019 namespace gemfire {
00020 
00021 #if GF_DEVEL_ASSERTS == 1
00022 #define GF_CHECK_NPE(x) if ( x != NULL ) { } else gemfire::SPEHelper::throwNullPointerException( typeid( *this ).name() )
00023 #else
00024 #define GF_CHECK_NPE(x)
00025 #endif
00026 
00027 class MapEntry;
00028 class MapEntryImpl;
00029 
00030 template<class Target>
00033 class SharedPtr
00034 {
00035 
00036 public:
00038   inline SharedPtr()
00039     : m_ptr(NULL)
00040   {}
00041 
00043   inline SharedPtr(const NullSharedBase* ptr)
00044     : m_ptr(NULL)
00045   {}
00046 
00050   inline explicit SharedPtr(const Target* ptr)
00051     : m_ptr(const_cast<Target *>(ptr))
00052   {
00053     if (NULL != m_ptr)
00054       getSB( m_ptr )->preserveSB();
00055   }
00056 
00058   inline SharedPtr(const SharedPtr& other)
00059     : m_ptr(other.m_ptr)
00060   {
00061     if (NULL != m_ptr)
00062       getSB( m_ptr )->preserveSB();
00063   }
00064 
00068   template<class Other>
00069   inline SharedPtr(const SharedPtr<Other>& other)
00070     : m_ptr( getTarget< Target >( other.ptr( ) ) )
00071   {
00072     if (NULL != m_ptr)
00073       getSB( m_ptr )->preserveSB();
00074   }
00075 
00077   inline ~SharedPtr()
00078   {
00079     if (NULL != m_ptr)
00080       getSB( m_ptr )->releaseSB();
00081 
00082     m_ptr = NULL;
00083   }
00084 
00085   inline Target* operator -> () const
00086   {
00087     GF_CHECK_NPE( m_ptr );
00088     GF_DEV_ASSERT( getSB( m_ptr )->refCount( ) > 0 );
00089 
00090     return m_ptr;
00091   }
00092 
00093   inline Target& operator * () const
00094   {
00095     GF_CHECK_NPE( m_ptr );
00096     return *m_ptr;
00097   }
00098 
00102   inline SharedPtr& operator = (Target * other)
00103   {
00104     if (NULL != other)
00105       getSB( other )->preserveSB();
00106 
00107     if (NULL != m_ptr)
00108       getSB( m_ptr )->releaseSB();
00109 
00110     m_ptr = other;
00111 
00112     return *this;
00113   }
00114 
00115   inline SharedPtr& operator = (const SharedPtr& other)
00116   {
00117     Target* otherPtr = other.m_ptr;
00118 
00119     if ( NULL != otherPtr ) {
00120       getSB( otherPtr )->preserveSB( );
00121     }
00122     if ( NULL != m_ptr ) {
00123       getSB( m_ptr )->releaseSB( );
00124     }
00125     m_ptr = otherPtr;
00126 
00127     GF_DEV_ASSERT( otherPtr == other.m_ptr );
00128 
00129     return *this;
00130   }
00131 
00135   template<class Other>
00136   inline SharedPtr& operator = (const SharedPtr<Other>& other)
00137   {
00138     Other* otherPtr = other.ptr( );
00139 
00140     Target* otherTargetPtr = getTarget< Target >( otherPtr );
00141 
00142     if ( NULL != otherPtr ) {
00143       getSB( otherPtr )->preserveSB( );
00144     }
00145     if ( NULL != m_ptr ) {
00146       getSB( m_ptr )->releaseSB( );
00147     }
00148     m_ptr = otherTargetPtr;
00149 
00150     GF_DEV_ASSERT( otherPtr == other.ptr( ) );
00151 
00152     return *this;
00153   }
00154 
00155   inline SharedPtr& operator = (const NullSharedBase* nullOther)
00156   {
00157     if (m_ptr != NULL) {
00158       getSB( m_ptr )->releaseSB();
00159     }
00160     m_ptr = NULL;
00161     return *this;
00162   }
00163 
00168   template<class Other>
00169   inline SharedPtr& operator = (Other* other)
00170   {
00171     Target* otherTargetPtr = getTarget< Target >( other );
00172 
00173     if ( NULL != other ) {
00174       getSB( other )->preserveSB( );
00175     }
00176     if ( NULL != m_ptr ) {
00177       getSB( m_ptr )->releaseSB( );
00178     }
00179     m_ptr = otherTargetPtr;
00180 
00181     return *this;
00182   }
00183 
00184   inline bool operator == (const Target* other) const
00185     { return m_ptr == other; }
00186 
00187   inline bool operator != (const Target* other) const
00188     { return m_ptr != other; }
00189 
00190   inline bool operator == (const NullSharedBase* nullOther) const
00191     { return m_ptr == NULL; }
00192 
00193   inline bool operator != (const NullSharedBase* nullOther) const
00194     { return m_ptr != NULL; }
00195 
00196   inline bool operator == (const SharedPtr& other) const
00197     { return m_ptr == other.m_ptr; }
00198 
00199   inline bool operator != (const SharedPtr& other) const
00200     { return m_ptr != other.m_ptr; }
00201 
00202   template<class Other>
00203   inline bool operator == (const SharedPtr<Other>& other)
00204   {
00205     return ((const void*)m_ptr) == ((const void*) other.ptr() );
00206   }
00207 
00208   template<class Other>
00209   inline bool operator != (const SharedPtr<Other>& other)
00210     { return ! operator == (other); }
00211 
00212   inline Target* ptr() const
00213   {
00214     return m_ptr;
00215   }
00216 
00217 
00218 private:
00219 
00220   inline explicit SharedPtr(bool noInit)
00221   {}
00222 
00223   Target* m_ptr;
00224 
00225 friend class MapEntry;
00226 friend class MapEntryImpl;
00227 
00228 };
00229 
00230 typedef SharedPtr<SharedBase> SharedBasePtr;
00231 
00232 
00244 template <class TargetSP, class Other>
00245 TargetSP staticCast( const SharedPtr<Other>& other )
00246 {
00247   GF_D_ASSERT( ( other.ptr( ) == NULL ) ||
00248       ( dynamic_cast<GF_UNWRAP_SP( TargetSP )*>( other.ptr( ) ) != NULL ) );
00249 
00250   return TargetSP( static_cast<GF_UNWRAP_SP( TargetSP )*>( other.ptr( ) ) );
00251 }
00252 
00256 template <class TargetSP, class Other>
00257 TargetSP dynCast( const SharedPtr<Other>& other )
00258 {
00259   GF_UNWRAP_SP( TargetSP )* otherPtr;
00260 
00261   if ( ( other.ptr( ) == NULL ) ) {
00262     return NULLPTR;
00263   } else if ( ( otherPtr = dynamic_cast<GF_UNWRAP_SP( TargetSP )*>
00264         ( other.ptr( ) ) ) != NULL ) {
00265     return TargetSP( otherPtr );
00266   } else {
00267     SPEHelper::throwClassCastException( "dynCast: cast failed",
00268         typeid( other ).name( ), typeid( TargetSP ).name( ) );
00269     return NULLPTR;
00270   }
00271 }
00272 
00276 template<class TargetSP, class Other>
00277 bool instanceOf(const SharedPtr<Other>& other)
00278 {
00279   return (dynamic_cast<GF_UNWRAP_SP(TargetSP)*> (other.ptr()) != NULL);
00280 }
00281 
00282 }
00283 
00284 #endif

GemFire C++ Cache API Documentation