forked from boostorg/smart_ptr
Add weak_from_this.
This commit is contained in:
@ -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><boost/enable_shared_from_this.hpp></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><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>
|
||||
<P><STRONG>enable_shared_from_this<T></STRONG> defines two member functions
|
||||
called <STRONG>shared_from_this</STRONG> that return a <STRONG>shared_ptr<T></STRONG>
|
||||
and <STRONG>shared_ptr<T const></STRONG>, depending on constness, to <STRONG>this</STRONG>.</P>
|
||||
<h2><a name="Example">Example</a></h2>
|
||||
<pre>
|
||||
#include <boost/enable_shared_from_this.hpp>
|
||||
@ -41,7 +45,7 @@ int main()
|
||||
assert(!(p < q || q < 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<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>template<class T> shared_ptr<T>
|
||||
enable_shared_from_this<T>::shared_from_this();</h4>
|
||||
<h4>template<class T> shared_ptr<T const>
|
||||
enable_shared_from_this<T>::shared_from_this() const;</h4>
|
||||
<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> <STRONG>enable_shared_from_this<T></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<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> A <b>shared_ptr<T></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<T></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<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 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 © 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>
|
||||
|
@ -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
|
||||
|
@ -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 ]
|
||||
;
|
||||
}
|
||||
|
@ -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
|
||||
|
52
test/weak_from_this_test.cpp
Normal file
52
test/weak_from_this_test.cpp
Normal 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();
|
||||
}
|
60
test/weak_from_this_test2.cpp
Normal file
60
test/weak_from_this_test2.cpp
Normal 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();
|
||||
}
|
Reference in New Issue
Block a user