forked from boostorg/smart_ptr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenable_shared_from_this.html
110 lines (103 loc) · 4.71 KB
/
enable_shared_from_this.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>enable_shared_from_this</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body text="#000000" bgcolor="#ffffff" link="#0000ff" vlink="#0000ff">
<h1><img height="86" alt="boost.png (6897 bytes)" src="../../boost.png"
width="277" align="middle" border="0" />enable_shared_from_this</h1>
<h2><a name="Purpose">Purpose</a></h2>
<p>
The header <code><boost/enable_shared_from_this.hpp></code> defines
the class template <code>enable_shared_from_this</code>. It is used as a
base class that allows a <a href="shared_ptr.htm">shared_ptr</a> or
a <a href="weak_ptr.htm">weak_ptr</a> to the current object to be obtained
from within a member function.
</p>
<p><code>enable_shared_from_this<T></code> defines two member functions
called <code>shared_from_this</code> that return a <code>shared_ptr<T></code>
and <code>shared_ptr<T const></code>, depending on constness, to <code>this</code>.
It also defines two member functions called <code>weak_from_this</code> that return
a corresponding <code>weak_ptr</code>.
</p>
<h2><a name="Example">Example</a></h2>
<pre>
#include <boost/enable_shared_from_this.hpp>
#include <boost/shared_ptr.hpp>
#include <cassert>
class Y: public boost::enable_shared_from_this<Y>
{
public:
boost::shared_ptr<Y> f()
{
return shared_from_this();
}
};
int main()
{
boost::shared_ptr<Y> p(new Y);
boost::shared_ptr<Y> q = p->f();
assert(p == q);
assert(!(p < q || q < p)); // p and q must share ownership
}
</pre>
<h2><a name="Synopsis">Synopsis</a></h2>
<pre>
namespace boost
{
template<class T> class enable_shared_from_this
{
public:
shared_ptr<T> shared_from_this();
shared_ptr<T const> shared_from_this() const;
weak_ptr<T> weak_from_this() noexcept;
weak_ptr<T const> weak_from_this() const noexcept;
}
}
</pre>
<h4><code>template<class T> shared_ptr<T>
enable_shared_from_this<T>::shared_from_this();</code></h4>
<h4><code>template<class T> shared_ptr<T const>
enable_shared_from_this<T>::shared_from_this() const;</code></h4>
<blockquote>
<p>
<b>Requires:</b> <code>enable_shared_from_this<T></code> must be an
accessible base class of <code>T</code>. <code>*this</code> must be a subobject
of an instance <code>t</code> of type <code>T</code>.
</p>
<p>
<b>Returns:</b> If a <code>shared_ptr</code> instance <code>p</code> that <em>owns</em>
<code>t</code> exists, a <code>shared_ptr<T></code> instance <code>r</code> that shares
ownership with <code>p</code>.
</p>
<p>
<b>Postconditions:</b> <code>r.get() == this</code>.
</p>
<p>
<b>Throws:</b> <code>bad_weak_ptr</code> when no <code>shared_ptr</code> <em>owns</em> <code>*this</code>.
</p>
</blockquote>
<h4><code>template<class T> weak_ptr<T>
enable_shared_from_this<T>::weak_from_this() noexcept;</code></h4>
<h4><code>template<class T> weak_ptr<T const>
enable_shared_from_this<T>::weak_from_this() const noexcept;</code></h4>
<blockquote>
<p>
<b>Requires:</b> <code>enable_shared_from_this<T></code> must be an
accessible base class of <code>T</code>. <code>*this</code> must be a subobject
of an instance <code>t</code> of type <code>T</code>.
</p>
<p>
<b>Returns:</b> If a <code>shared_ptr</code> instance <code>p</code> that <em>owns</em>
<code>t</code> exists or has existed in the past, a <code>weak_ptr<T></code> instance
<code>r</code> that shares ownership with <code>p</code>. Otherwise, an empty <code>weak_ptr</code>.
</p>
</blockquote>
<hr />
<p>
<small>Copyright © 2002, 2003, 2015 by Peter Dimov. Distributed under the Boost Software License, Version
1.0. See accompanying file <a href="../../LICENSE_1_0.txt">LICENSE_1_0.txt</a> or
copy at <a href="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</a>.</small></p>
</body>
</html>