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