Add weak_from_this.

This commit is contained in:
Peter Dimov
2015-01-25 20:10:57 +02:00
parent 3fd53ced83
commit 7104e7dc7e
6 changed files with 178 additions and 28 deletions

View File

@ -2,21 +2,25 @@
<html>
<head>
<title>enable_shared_from_this</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<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>
<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 <STRONG>&lt;boost/enable_shared_from_this.hpp&gt;</STRONG> defines
the class template <STRONG>enable_shared_from_this</STRONG>. It is used as a
base class that allows a <A href="shared_ptr.htm">shared_ptr</A> to the current
object to be obtained from within a member function.
The header <code>&lt;boost/enable_shared_from_this.hpp&gt;</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&lt;T&gt;</code> defines two member functions
called <code>shared_from_this</code> that return a <code>shared_ptr&lt;T&gt;</code>
and <code>shared_ptr&lt;T const&gt;</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>
<P><STRONG>enable_shared_from_this&lt;T&gt;</STRONG> defines two member functions
called <STRONG>shared_from_this</STRONG> that return a <STRONG>shared_ptr&lt;T&gt;</STRONG>
and <STRONG>shared_ptr&lt;T const&gt;</STRONG>, depending on constness, to <STRONG>this</STRONG>.</P>
<h2><a name="Example">Example</a></h2>
<pre>
#include &lt;boost/enable_shared_from_this.hpp&gt;
@ -41,7 +45,7 @@ int main()
assert(!(p &lt; q || q &lt; p)); // p and q must share ownership
}
</pre>
<h3><a name="Synopsis">Synopsis</a></h3>
<h2><a name="Synopsis">Synopsis</a></h2>
<pre>
namespace boost
{
@ -52,34 +56,55 @@ public:
shared_ptr&lt;T&gt; shared_from_this();
shared_ptr&lt;T const&gt; shared_from_this() const;
weak_ptr&lt;T&gt; weak_from_this() noexcept;
weak_ptr&lt;T const&gt; weak_from_this() const noexcept;
}
}
</pre>
<h4>template&lt;class T&gt; shared_ptr&lt;T&gt;
enable_shared_from_this&lt;T&gt;::shared_from_this();</h4>
<h4>template&lt;class T&gt; shared_ptr&lt;T const&gt;
enable_shared_from_this&lt;T&gt;::shared_from_this() const;</h4>
<h4><code>template&lt;class T&gt; shared_ptr&lt;T&gt;
enable_shared_from_this&lt;T&gt;::shared_from_this();</code></h4>
<h4><code>template&lt;class T&gt; shared_ptr&lt;T const&gt;
enable_shared_from_this&lt;T&gt;::shared_from_this() const;</code></h4>
<blockquote>
<p>
<b>Requires:</b> <STRONG>enable_shared_from_this&lt;T&gt;</STRONG> must be an
accessible base class of <b>T</b>. <STRONG>*this</STRONG> must be a subobject
of an instance <STRONG>t</STRONG> of type <STRONG>T</STRONG> . There must exist
at least one <STRONG>shared_ptr</STRONG> instance <STRONG>p</STRONG> that <EM>owns</EM>
<STRONG>t</STRONG>.
<b>Requires:</b> <code>enable_shared_from_this&lt;T&gt;</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> A <b>shared_ptr&lt;T&gt;</b> instance <b>r</b> that shares
ownership with <b>p</b>.
<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&lt;T&gt;</code> instance <code>r</code> that shares
ownership with <code>p</code>.
</p>
<p>
<b>Postconditions:</b> <tt>r.get() == this</tt>.
<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>
<p>$Date$</p>
<h4><code>template&lt;class T&gt; weak_ptr&lt;T&gt;
enable_shared_from_this&lt;T&gt;::weak_from_this() noexcept;</code></h4>
<h4><code>template&lt;class T&gt; weak_ptr&lt;T const&gt;
enable_shared_from_this&lt;T&gt;::weak_from_this() const noexcept;</code></h4>
<blockquote>
<p>
<b>Requires:</b> <code>enable_shared_from_this&lt;T&gt;</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&lt;T&gt;</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 &copy; 2002, 2003 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>
<small>Copyright &copy; 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>

View File

@ -58,6 +58,16 @@ public:
return p;
}
weak_ptr<T> weak_from_this() BOOST_NOEXCEPT
{
return weak_this_;
}
weak_ptr<T const> weak_from_this() const BOOST_NOEXCEPT
{
return weak_this_;
}
public: // actually private, but avoids compiler template friendship issues
// Note: invoked automatically by shared_ptr; do not call

View File

@ -175,5 +175,8 @@ import testing ;
[ run weak_from_raw_test4.cpp ]
[ compile sp_explicit_inst_test.cpp ]
[ run weak_from_this_test.cpp ]
[ run weak_from_this_test2.cpp ]
;
}

View File

@ -1,7 +1,7 @@
//
// weak_from_raw_test4.cpp
//
// As weak_from_raw_test2.cpp, but uses weak_from_this
// As weak_from_raw_test2.cpp, but uses weak_from_raw
// in the constructor
//
// Copyright (c) 2014, 2015 Peter Dimov

View File

@ -0,0 +1,52 @@
#include <boost/config.hpp>
//
// weak_from_this_test.cpp
//
// Copyright (c) 2002, 2003, 2015 Peter Dimov
//
// Distributed under the Boost Software License, Version 1.0.
//
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
//
#include <boost/enable_shared_from_this.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
#include <boost/detail/lightweight_test.hpp>
class V: public boost::enable_shared_from_this<V>
{
};
void test()
{
boost::shared_ptr<V> p( new V );
boost::weak_ptr<V> q = p;
BOOST_TEST( !q.expired() );
boost::weak_ptr<V> q2 = p->weak_from_this();
BOOST_TEST( !q2.expired() );
BOOST_TEST( !(q < q2) && !(q2 < q) );
V v2( *p );
boost::weak_ptr<V> q3 = v2.weak_from_this();
BOOST_TEST( q3.expired() );
*p = V();
boost::weak_ptr<V> q4 = p->shared_from_this();
BOOST_TEST( !q4.expired() );
BOOST_TEST( !(q < q4) && !(q4 < q) );
BOOST_TEST( !(q2 < q4) && !(q4 < q2) );
}
int main()
{
test();
return boost::report_errors();
}

View File

@ -0,0 +1,60 @@
//
// weak_from_this_test2.cpp
//
// Tests weak_from_this in a destructor
//
// Copyright (c) 2014, 2015 Peter Dimov
//
// Distributed under the Boost Software License, Version 1.0.
//
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
//
#include <boost/smart_ptr/enable_shared_from_this.hpp>
#include <boost/weak_ptr.hpp>
#include <boost/core/lightweight_test.hpp>
class X: public boost::enable_shared_from_this< X >
{
private:
boost::weak_ptr<X> px_;
public:
X()
{
boost::weak_ptr<X> p1 = weak_from_this();
BOOST_TEST( p1._empty() );
BOOST_TEST( p1.expired() );
}
void check()
{
boost::weak_ptr<X> p2 = weak_from_this();
BOOST_TEST( !p2.expired() );
BOOST_TEST( p2.lock().get() == this );
px_ = p2;
}
~X()
{
boost::weak_ptr<X> p3 = weak_from_this();
BOOST_TEST( p3.expired() );
BOOST_TEST( !(px_ < p3) && !(p3 < px_) );
}
};
int main()
{
{
boost::shared_ptr< X > px( new X );
px->check();
}
return boost::report_errors();
}