1 | // |
---|---|
2 | // weak_from_this_test2.cpp |
3 | // |
4 | // Tests weak_from_this in a destructor |
5 | // |
6 | // Copyright (c) 2014, 2015 Peter Dimov |
7 | // |
8 | // Distributed under the Boost Software License, Version 1.0. |
9 | // |
10 | // See accompanying file LICENSE_1_0.txt or copy at |
11 | // http://www.boost.org/LICENSE_1_0.txt |
12 | // |
13 | |
14 | #include <boost/smart_ptr/enable_shared_from_this.hpp> |
15 | #include <boost/weak_ptr.hpp> |
16 | #include <boost/core/lightweight_test.hpp> |
17 | |
18 | class X: public boost::enable_shared_from_this< X > |
19 | { |
20 | private: |
21 | |
22 | boost::weak_ptr<X> px_; |
23 | |
24 | public: |
25 | |
26 | X() |
27 | { |
28 | boost::weak_ptr<X> p1 = weak_from_this(); |
29 | BOOST_TEST( p1._empty() ); |
30 | BOOST_TEST( p1.expired() ); |
31 | } |
32 | |
33 | void check() |
34 | { |
35 | boost::weak_ptr<X> p2 = weak_from_this(); |
36 | BOOST_TEST( !p2.expired() ); |
37 | |
38 | BOOST_TEST( p2.lock().get() == this ); |
39 | |
40 | px_ = p2; |
41 | } |
42 | |
43 | ~X() |
44 | { |
45 | boost::weak_ptr<X> p3 = weak_from_this(); |
46 | BOOST_TEST( p3.expired() ); |
47 | |
48 | BOOST_TEST( !(px_ < p3) && !(p3 < px_) ); |
49 | } |
50 | }; |
51 | |
52 | int main() |
53 | { |
54 | { |
55 | boost::shared_ptr< X > px( new X ); |
56 | px->check(); |
57 | } |
58 | |
59 | return boost::report_errors(); |
60 | } |
61 |