#ifndef __DAHUA_MEMORY_ENABLESHAREDFROMTHIS_H__ #define __DAHUA_MEMORY_ENABLESHAREDFROMTHIS_H__ #include "Memory/SharedPtr.h" #include "Memory/WeakPtr.h" namespace Dahua { namespace Memory { template class TEnableSharedFromThis { protected: TEnableSharedFromThis() { } TEnableSharedFromThis(TEnableSharedFromThis const &) { } TEnableSharedFromThis & operator=(TEnableSharedFromThis const &) { return *this; } ~TEnableSharedFromThis() { } public: TSharedPtr shared_from_this() { TSharedPtr p( weak_this_ ); assert( p.get() == this ); return p; } TSharedPtr shared_from_this() const { TSharedPtr p( weak_this_ ); assert( p.get() == this ); return p; } public: // actually private, but avoids compiler template friendship issues template void _internal_accept_owner( TSharedPtr const * ppx, Y * py ) const { if( weak_this_.expired() ) { weak_this_ = TSharedPtr( *ppx, py ); } } private: mutable TWeakPtr weak_this_; }; } // namespace Memory }// namesapce Dahua #endif // end of __DAHUA_MEMORY_ENABLESHAREDFROMTHIS_H__