base_safe_ptr.hh

Go to the documentation of this file.
00001 /*
00008  * LEGAL:   COPYRIGHT (C) JIM E. BROOKS 2006
00009  *          THIS SOURCE CODE IS RELEASED UNDER THE TERMS
00010  *          OF THE GNU GENERAL PUBLIC LICENSE VERSION 2 (GPL 2).
00011  *****************************************************************************/
00012 
00056 
00057 #ifndef BASE_SAFE_PTR_HH
00058 #define BASE_SAFE_PTR_HH 1
00059 
00060 #include "base_common.hh"
00061 
00062 namespace base {
00063 
00064 template<typename T>
00065 class SafePtr
00066 {
00067 public:
00068   //SafePtr( void ) : mObj(NULL) { }  // perpetrator of false advertisement
00069     SafePtr( T* obj ) : mObj(obj) { }
00070     ~SafePtr() { }  // SafePtr<> doesn't own actual object
00071 
00072     // Copy ctors for related types.
00073     // SafePtr<Base> = new Derived (v.v. is illegal and won't compile).
00074     template<typename T2> SafePtr( T2* obj ) : mObj(obj) { }
00075     template<typename T2> SafePtr( const SafePtr<T2> ptr ) : mObj(ptr.mObj) { }
00076 
00077     T*      operator->( void ) const { return mObj; }
00078     T&      operator*( void ) const { return *mObj; }  // dereference, not multiply
00079 
00080 // Methods for identical types:
00081 
00082     // Assign this SafePtr with an actual pointer.
00083     SafePtr<T>& operator=( T* obj ) { mObj = obj; return *this; }
00084 
00085     // Compare addresses of actual objects of same type.
00086     bool    operator==( const SafePtr& other ) const { return mObj == other.mObj; }
00087     bool    operator!=( const SafePtr& other ) const { return mObj != other.mObj; }
00088 
00089 // Methods for related types (base and derived):
00090 
00091     // Assign SafePtr<> with an actual pointer to a related type.
00092     // SafePtr<Base> ptrBase = new Derived;  // ok
00093     // SafePtr<Derived> ptrDerived = new Base;  // wrong (missing slice), should not compile
00094     template<typename T2> SafePtr<T>& operator=( T2* obj )
00095     {
00096         mObj = obj;    // Base* obj = pDerived (T=Base T2=Derived)
00097         return *this;  // return SafePtr<Base>
00098     }
00099 
00100     // Assign SafePtr<Base> = SafePtr<Derived> (v.v. is illegal).
00101     // Equivalent to: Base* pBase = pDerived;
00102     template<typename T2> SafePtr<T>& operator=( SafePtr<T2> ptr )
00103     {
00104         T* obj = ptr.mObj;  // Base* base = SafePtr<Derived>.mObj (T=Base T2=Derived)
00105         mObj = obj;         // Base* mObj = base
00106         return *this;       // return SafePtr<Base>
00107     }
00108 
00109     // Limited use.
00110     T*             PTR( void ) const { return mObj; }
00111     const T* CONST_PTR( void ) const { return mObj; }
00112     T&             REF( void ) const { return *mObj; }
00113     const T& CONST_REF( void ) const { return *mObj; }
00114 
00115 private:
00116     T*      mObj;
00117 
00118 template<typename> friend class SafePtr;  // variations of SafePtr<> are distinct types so friend is necessary
00119 };
00120 
00121 // Comparing addresses of actual objects.
00122 template<typename T>              bool operator==( const SafePtr<T>& ptr, const T* obj )          { return ptr.CONST_PTR() == obj; }
00123 template<typename T>              bool operator!=( const SafePtr<T>& ptr, const T* obj )          { return ptr.CONST_PTR() != obj; }
00124 template<typename T>              bool operator==( const T* obj, const SafePtr<T>& ptr )          { return ptr.CONST_PTR() == obj; }
00125 template<typename T>              bool operator!=( const T* obj, const SafePtr<T>& ptr )          { return ptr.CONST_PTR() != obj; }
00126 template<typename T1,typename T2> bool operator==( const T1* obj, const SafePtr<T2>& ptr )        { return ptr.CONST_PTR() == obj; }
00127 template<typename T1,typename T2> bool operator!=( const T1* obj, const SafePtr<T2>& ptr )        { return ptr.CONST_PTR() != obj; }
00128 template<typename T1,typename T2> bool operator==( const SafePtr<T1>& ptr1, const SafePtr<T2>& ptr2 ) { return ptr1.CONST_PTR() == ptr2.CONST_PTR(); }
00129 template<typename T1,typename T2> bool operator!=( const SafePtr<T1>& ptr1, const SafePtr<T2>& ptr2 ) { return ptr1.CONST_PTR() != ptr2.CONST_PTR(); }
00130 template<typename T>              bool operator<(  const SafePtr<T>& ptr1, const SafePtr<T>& ptr2 )   { return ptr1.CONST_PTR()  < ptr2.CONST_PTR(); }
00131 
00132 } // namespace base
00133 
00134 #endif // BASE_SAFE_PTR_HH
Palomino 3D Engine documents generated by doxygen 1.5.3 on Fri Nov 23 11:26:06 2007