Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 10:08:50

0001 #ifndef BOOST_SMART_PTR_ENABLE_SHARED_FROM_THIS_HPP_INCLUDED
0002 #define BOOST_SMART_PTR_ENABLE_SHARED_FROM_THIS_HPP_INCLUDED
0003 
0004 //
0005 //  enable_shared_from_this.hpp
0006 //
0007 //  Copyright 2002, 2009 Peter Dimov
0008 //
0009 //  Distributed under the Boost Software License, Version 1.0.
0010 //  See accompanying file LICENSE_1_0.txt or copy at
0011 //  http://www.boost.org/LICENSE_1_0.txt
0012 //
0013 //  See http://www.boost.org/libs/smart_ptr/ for documentation.
0014 //
0015 
0016 #include <boost/smart_ptr/weak_ptr.hpp>
0017 #include <boost/smart_ptr/shared_ptr.hpp>
0018 #include <boost/assert.hpp>
0019 
0020 namespace boost
0021 {
0022 
0023 template<class T> class enable_shared_from_this
0024 {
0025 protected:
0026 
0027     constexpr enable_shared_from_this() noexcept
0028     {
0029     }
0030 
0031     constexpr enable_shared_from_this(enable_shared_from_this const &) noexcept
0032     {
0033     }
0034 
0035     enable_shared_from_this & operator=(enable_shared_from_this const &) noexcept
0036     {
0037         return *this;
0038     }
0039 
0040     ~enable_shared_from_this() noexcept // ~weak_ptr<T> newer throws, so this call also must not throw
0041     {
0042     }
0043 
0044 public:
0045 
0046     shared_ptr<T> shared_from_this()
0047     {
0048         shared_ptr<T> p( weak_this_ );
0049         BOOST_ASSERT( p.get() == this );
0050         return p;
0051     }
0052 
0053     shared_ptr<T const> shared_from_this() const
0054     {
0055         shared_ptr<T const> p( weak_this_ );
0056         BOOST_ASSERT( p.get() == this );
0057         return p;
0058     }
0059 
0060     weak_ptr<T> weak_from_this() noexcept
0061     {
0062         return weak_this_;
0063     }
0064 
0065     weak_ptr<T const> weak_from_this() const noexcept
0066     {
0067         return weak_this_;
0068     }
0069 
0070 public: // actually private, but avoids compiler template friendship issues
0071 
0072     // Note: invoked automatically by shared_ptr; do not call
0073     template<class X, class Y> void _internal_accept_owner( shared_ptr<X> const * ppx, Y * py ) const noexcept
0074     {
0075         if( weak_this_.expired() )
0076         {
0077             weak_this_ = shared_ptr<T>( *ppx, py );
0078         }
0079     }
0080 
0081 private:
0082 
0083     mutable weak_ptr<T> weak_this_;
0084 };
0085 
0086 } // namespace boost
0087 
0088 #endif  // #ifndef BOOST_SMART_PTR_ENABLE_SHARED_FROM_THIS_HPP_INCLUDED