diff options
Diffstat (limited to 'libs/smart_ptr')
-rw-r--r-- | libs/smart_ptr/enable_shared_from_this.html | 79 | ||||
-rw-r--r-- | libs/smart_ptr/test/Jamfile.v2 | 8 | ||||
-rw-r--r-- | libs/smart_ptr/test/array_fail_array_access.cpp | 6 | ||||
-rw-r--r-- | libs/smart_ptr/test/sp_explicit_inst_test.cpp | 23 | ||||
-rw-r--r-- | libs/smart_ptr/test/weak_from_raw_test.cpp | 3 | ||||
-rw-r--r-- | libs/smart_ptr/test/weak_from_raw_test3.cpp | 46 | ||||
-rw-r--r-- | libs/smart_ptr/test/weak_from_raw_test4.cpp | 67 | ||||
-rw-r--r-- | libs/smart_ptr/test/weak_from_raw_test5.cpp | 45 | ||||
-rw-r--r-- | libs/smart_ptr/test/weak_from_this_test.cpp | 52 | ||||
-rw-r--r-- | libs/smart_ptr/test/weak_from_this_test2.cpp | 60 |
10 files changed, 359 insertions, 30 deletions
diff --git a/libs/smart_ptr/enable_shared_from_this.html b/libs/smart_ptr/enable_shared_from_this.html index 028cf57d7..f04f3126b 100644 --- a/libs/smart_ptr/enable_shared_from_this.html +++ b/libs/smart_ptr/enable_shared_from_this.html @@ -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> 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>Returns:</b> A <b>shared_ptr<T></b> instance <b>r</b> that shares - ownership with <b>p</b>. + <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>Postconditions:</b> <tt>r.get() == this</tt>. + <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> - <p>$Date$</p> + <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> diff --git a/libs/smart_ptr/test/Jamfile.v2 b/libs/smart_ptr/test/Jamfile.v2 index 027367ced..b17e55f19 100644 --- a/libs/smart_ptr/test/Jamfile.v2 +++ b/libs/smart_ptr/test/Jamfile.v2 @@ -171,5 +171,13 @@ import testing ; [ run weak_from_raw_test.cpp ] [ run weak_from_raw_test2.cpp ] + [ run weak_from_raw_test3.cpp ] + [ run weak_from_raw_test4.cpp ] + [ run weak_from_raw_test5.cpp ] + + [ compile sp_explicit_inst_test.cpp ] + + [ run weak_from_this_test.cpp ] + [ run weak_from_this_test2.cpp ] ; } diff --git a/libs/smart_ptr/test/array_fail_array_access.cpp b/libs/smart_ptr/test/array_fail_array_access.cpp index abfacbe38..4f4e3f8cb 100644 --- a/libs/smart_ptr/test/array_fail_array_access.cpp +++ b/libs/smart_ptr/test/array_fail_array_access.cpp @@ -12,8 +12,12 @@ struct X { }; +template<class T> void f( T & /*t*/ ) +{ +} + int main() { boost::shared_ptr<X> px( new X ); - px[ 0 ]; + f( px[ 0 ] ); } diff --git a/libs/smart_ptr/test/sp_explicit_inst_test.cpp b/libs/smart_ptr/test/sp_explicit_inst_test.cpp new file mode 100644 index 000000000..d8de782ee --- /dev/null +++ b/libs/smart_ptr/test/sp_explicit_inst_test.cpp @@ -0,0 +1,23 @@ +// +// Explicit instantiations are reported to exist in user code +// +// Copyright (c) 2014 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/shared_ptr.hpp> + +template class boost::shared_ptr< int >; + +struct X +{ +}; + +template class boost::shared_ptr< X >; + +int main() +{ +} diff --git a/libs/smart_ptr/test/weak_from_raw_test.cpp b/libs/smart_ptr/test/weak_from_raw_test.cpp index ba95ecc0f..34d21eb26 100644 --- a/libs/smart_ptr/test/weak_from_raw_test.cpp +++ b/libs/smart_ptr/test/weak_from_raw_test.cpp @@ -12,7 +12,6 @@ #include <boost/smart_ptr/enable_shared_from_raw.hpp> - #include <boost/detail/lightweight_test.hpp> @@ -23,7 +22,7 @@ void basic_weak_from_raw_test() { X *p(new X); boost::weak_ptr<X> weak = boost::weak_from_raw(p); - BOOST_TEST(weak.expired()); + BOOST_TEST(!weak.expired()); boost::shared_ptr<X> shared(p); weak = boost::weak_from_raw(p); BOOST_TEST(weak.expired() == false); diff --git a/libs/smart_ptr/test/weak_from_raw_test3.cpp b/libs/smart_ptr/test/weak_from_raw_test3.cpp new file mode 100644 index 000000000..ee73b4c88 --- /dev/null +++ b/libs/smart_ptr/test/weak_from_raw_test3.cpp @@ -0,0 +1,46 @@ +// +// weak_from_raw_test3.cpp +// +// Test that weak_from_raw and shared_from_raw +// return consistent values from a constructor +// +// Copyright (c) 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_raw.hpp> +#include <boost/weak_ptr.hpp> +#include <boost/core/lightweight_test.hpp> + +class X: public boost::enable_shared_from_raw +{ +public: + + X() + { + boost::weak_ptr<X> p1 = boost::weak_from_raw( this ); + BOOST_TEST( !p1.expired() ); + + boost::weak_ptr<X> p2 = boost::weak_from_raw( this ); + BOOST_TEST( !p2.expired() ); + BOOST_TEST( !( p1 < p2 ) && !( p2 < p1 ) ); + + boost::weak_ptr<X> p3 = boost::shared_from_raw( this ); + BOOST_TEST( !( p1 < p3 ) && !( p3 < p1 ) ); + + boost::weak_ptr<X> p4 = boost::weak_from_raw( this ); + BOOST_TEST( !p4.expired() ); + BOOST_TEST( !( p3 < p4 ) && !( p4 < p3 ) ); + BOOST_TEST( !( p1 < p4 ) && !( p4 < p1 ) ); + } +}; + +int main() +{ + boost::shared_ptr< X > px( new X ); + return boost::report_errors(); +} diff --git a/libs/smart_ptr/test/weak_from_raw_test4.cpp b/libs/smart_ptr/test/weak_from_raw_test4.cpp new file mode 100644 index 000000000..2408f0092 --- /dev/null +++ b/libs/smart_ptr/test/weak_from_raw_test4.cpp @@ -0,0 +1,67 @@ +// +// weak_from_raw_test4.cpp +// +// As weak_from_raw_test2.cpp, but uses weak_from_raw +// in the constructor +// +// 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_raw.hpp> +#include <boost/weak_ptr.hpp> +#include <boost/core/lightweight_test.hpp> + +class X; + +static boost::weak_ptr< X > r_; + +void register_( boost::weak_ptr< X > const & r ) +{ + r_ = r; +} + +void check_( boost::weak_ptr< X > const & r ) +{ + BOOST_TEST( !( r < r_ ) && !( r_ < r ) ); +} + +void unregister_( boost::weak_ptr< X > const & r ) +{ + BOOST_TEST( !( r < r_ ) && !( r_ < r ) ); + r_.reset(); +} + +class X: public boost::enable_shared_from_raw +{ +public: + + X() + { + register_( boost::weak_from_raw( this ) ); + } + + ~X() + { + unregister_( boost::weak_from_raw( this ) ); + } + + void check() + { + check_( boost::weak_from_raw( this ) ); + } +}; + +int main() +{ + { + boost::shared_ptr< X > px( new X ); + px->check(); + } + + return boost::report_errors(); +} diff --git a/libs/smart_ptr/test/weak_from_raw_test5.cpp b/libs/smart_ptr/test/weak_from_raw_test5.cpp new file mode 100644 index 000000000..5425c27a4 --- /dev/null +++ b/libs/smart_ptr/test/weak_from_raw_test5.cpp @@ -0,0 +1,45 @@ +// +// weak_from_raw_test5.cpp +// +// Tests whether pointers returned from weak_from_raw +// expire properly +// +// Copyright 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_raw.hpp> +#include <boost/weak_ptr.hpp> +#include <boost/core/lightweight_test.hpp> + +class X: public boost::enable_shared_from_raw +{ +public: + + explicit X( boost::weak_ptr< X > & r ) + { + r = boost::weak_from_raw( this ); + } +}; + +int main() +{ + boost::weak_ptr<X> p1, p2; + + { + boost::shared_ptr< X > px( new X( p1 ) ); + p2 = boost::weak_from_raw( px.get() ); + + BOOST_TEST( !p1.expired() ); + BOOST_TEST( !p2.expired() ); + } + + BOOST_TEST( p1.expired() ); + BOOST_TEST( p2.expired() ); + + return boost::report_errors(); +} diff --git a/libs/smart_ptr/test/weak_from_this_test.cpp b/libs/smart_ptr/test/weak_from_this_test.cpp new file mode 100644 index 000000000..1a874e16f --- /dev/null +++ b/libs/smart_ptr/test/weak_from_this_test.cpp @@ -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(); +} diff --git a/libs/smart_ptr/test/weak_from_this_test2.cpp b/libs/smart_ptr/test/weak_from_this_test2.cpp new file mode 100644 index 000000000..05f3df2e6 --- /dev/null +++ b/libs/smart_ptr/test/weak_from_this_test2.cpp @@ -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(); +} |