| 1 | #ifndef BOOST_SMART_PTR_ENABLE_SHARED_FROM_THIS_HPP_INCLUDED |
|---|---|
| 2 | #define BOOST_SMART_PTR_ENABLE_SHARED_FROM_THIS_HPP_INCLUDED |
| 3 | |
| 4 | // |
| 5 | // enable_shared_from_this.hpp |
| 6 | // |
| 7 | // Copyright 2002, 2009 Peter Dimov |
| 8 | // |
| 9 | // Distributed under the Boost Software License, Version 1.0. |
| 10 | // See accompanying file LICENSE_1_0.txt or copy at |
| 11 | // http://www.boost.org/LICENSE_1_0.txt |
| 12 | // |
| 13 | // See http://www.boost.org/libs/smart_ptr/ for documentation. |
| 14 | // |
| 15 | |
| 16 | #include <boost/smart_ptr/detail/requires_cxx11.hpp> |
| 17 | #include <boost/smart_ptr/weak_ptr.hpp> |
| 18 | #include <boost/smart_ptr/shared_ptr.hpp> |
| 19 | #include <boost/smart_ptr/detail/sp_noexcept.hpp> |
| 20 | #include <boost/assert.hpp> |
| 21 | #include <boost/config.hpp> |
| 22 | |
| 23 | namespace boost |
| 24 | { |
| 25 | |
| 26 | template<class T> class enable_shared_from_this |
| 27 | { |
| 28 | protected: |
| 29 | |
| 30 | BOOST_CONSTEXPR enable_shared_from_this() BOOST_SP_NOEXCEPT |
| 31 | { |
| 32 | } |
| 33 | |
| 34 | BOOST_CONSTEXPR enable_shared_from_this(enable_shared_from_this const &) BOOST_SP_NOEXCEPT |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | enable_shared_from_this & operator=(enable_shared_from_this const &) BOOST_SP_NOEXCEPT |
| 39 | { |
| 40 | return *this; |
| 41 | } |
| 42 | |
| 43 | ~enable_shared_from_this() BOOST_SP_NOEXCEPT // ~weak_ptr<T> newer throws, so this call also must not throw |
| 44 | { |
| 45 | } |
| 46 | |
| 47 | public: |
| 48 | |
| 49 | shared_ptr<T> shared_from_this() |
| 50 | { |
| 51 | shared_ptr<T> p( weak_this_ ); |
| 52 | BOOST_ASSERT( p.get() == this ); |
| 53 | return p; |
| 54 | } |
| 55 | |
| 56 | shared_ptr<T const> shared_from_this() const |
| 57 | { |
| 58 | shared_ptr<T const> p( weak_this_ ); |
| 59 | BOOST_ASSERT( p.get() == this ); |
| 60 | return p; |
| 61 | } |
| 62 | |
| 63 | weak_ptr<T> weak_from_this() BOOST_SP_NOEXCEPT |
| 64 | { |
| 65 | return weak_this_; |
| 66 | } |
| 67 | |
| 68 | weak_ptr<T const> weak_from_this() const BOOST_SP_NOEXCEPT |
| 69 | { |
| 70 | return weak_this_; |
| 71 | } |
| 72 | |
| 73 | public: // actually private, but avoids compiler template friendship issues |
| 74 | |
| 75 | // Note: invoked automatically by shared_ptr; do not call |
| 76 | template<class X, class Y> void _internal_accept_owner( shared_ptr<X> const * ppx, Y * py ) const BOOST_SP_NOEXCEPT |
| 77 | { |
| 78 | if( weak_this_.expired() ) |
| 79 | { |
| 80 | weak_this_ = shared_ptr<T>( *ppx, py ); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | private: |
| 85 | |
| 86 | mutable weak_ptr<T> weak_this_; |
| 87 | }; |
| 88 | |
| 89 | } // namespace boost |
| 90 | |
| 91 | #endif // #ifndef BOOST_SMART_PTR_ENABLE_SHARED_FROM_THIS_HPP_INCLUDED |
| 92 |
