CppCMS
booster/enable_shared_from_this.h
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 //  enable_shared_from_this.hpp
00006 //
00007 //  Copyright 2002, 2009 Peter Dimov
00008 //
00009 //  Distributed under the Boost Software License, Version 1.0.
00010 //  See accompanying file LICENSE_1_0.txt or copy at
00011 //  http://www.boost.org/LICENSE_1_0.txt
00012 //
00013 //  http://www.boost.org/libs/smart_ptr/enable_shared_from_this.html
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: // actually private, but avoids compiler template friendship issues
00068 
00069     // Note: invoked automatically by shared_ptr; do not call
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 } // namespace boost
00084 
00085 #endif  // #ifndef BOOST_SMART_PTR_ENABLE_SHARED_FROM_THIS_HPP_INCLUDED