00001 #ifndef BOOSTER_SMART_PTR_ENABLE_SHARED_FROM_THIS_HPP_INCLUDED
00002 #define BOOSTER_SMART_PTR_ENABLE_SHARED_FROM_THIS_HPP_INCLUDED
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include <booster/weak_ptr.h>
00017 #include <booster/shared_ptr.h>
00018 #include <assert.h>
00019 #include <booster/config.h>
00020
00021 namespace booster
00022 {
00023
00029
00030 template<class T> class enable_shared_from_this
00031 {
00032 protected:
00033
00034 enable_shared_from_this()
00035 {
00036 }
00037
00038 enable_shared_from_this(enable_shared_from_this const &)
00039 {
00040 }
00041
00042 enable_shared_from_this & operator=(enable_shared_from_this const &)
00043 {
00044 return *this;
00045 }
00046
00047 ~enable_shared_from_this()
00048 {
00049 }
00050
00051 public:
00052
00053 shared_ptr<T> shared_from_this()
00054 {
00055 shared_ptr<T> p( weak_this_ );
00056 assert( p.get() == this );
00057 return p;
00058 }
00059
00060 shared_ptr<T const> shared_from_this() const
00061 {
00062 shared_ptr<T const> p( weak_this_ );
00063 assert( p.get() == this );
00064 return p;
00065 }
00066
00067 public:
00068
00069
00070 template<class X, class Y> void _internal_accept_owner( shared_ptr<X> const * ppx, Y * py ) const
00071 {
00072 if( weak_this_.expired() )
00073 {
00074 weak_this_ = shared_ptr<T>( *ppx, py );
00075 }
00076 }
00077
00078 private:
00079
00080 mutable weak_ptr<T> weak_this_;
00081 };
00082
00083 }
00084
00085 #endif // #ifndef BOOST_SMART_PTR_ENABLE_SHARED_FROM_THIS_HPP_INCLUDED