00001 #ifndef toast_enable_unintrusive_from_this_hpp_INCLUDED 00002 #define toast_enable_unintrusive_from_this_hpp_INCLUDED 00003 00004 #include <toast/unintrusive_ptr.hpp> 00005 #include <toast/weak_unintrusive_ptr.hpp> 00006 00014 namespace toast { 00015 00040 template<class T> class enable_unintrusive_from_this 00041 { 00042 protected: 00043 enable_unintrusive_from_this() 00044 { 00045 } 00046 00047 enable_unintrusive_from_this(enable_unintrusive_from_this const &) 00048 { 00049 } 00050 00051 enable_unintrusive_from_this & operator=(enable_unintrusive_from_this const &) 00052 { 00053 return *this; 00054 } 00055 00056 ~enable_unintrusive_from_this() 00057 { 00058 } 00059 00060 unintrusive_ptr<T> unintrusive_from_this() 00061 { 00062 unintrusive_ptr<T> p(_internal_weak_this); 00063 TOAST_ASSERT_D(p.get() == this); 00064 return p; 00065 } 00066 00067 unintrusive_ptr<T const> unintrusive_from_this() const 00068 { 00069 unintrusive_ptr<T const> p(_internal_weak_this); 00070 TOAST_ASSERT_D(p.get() == this); 00071 return p; 00072 } 00073 00074 private: 00075 // We don't need to initialize this because unintrusive_ptr's special 00076 // constructor will do so later. 00077 mutable weak_unintrusive_ptr<T> _internal_weak_this; 00078 00079 template<class Y> 00080 friend class unintrusive_ptr; 00081 }; 00082 00085 } 00086 00087 #endif