1 | // |
---|---|
2 | // weak_from_raw_test2.cpp |
3 | // |
4 | // Test that weak_from_raw returns consistent values |
5 | // throughout the object lifetime |
6 | // |
7 | // Copyright (c) 2014 Peter Dimov |
8 | // |
9 | // Distributed under the Boost Software License, Version 1.0. |
10 | // |
11 | // See accompanying file LICENSE_1_0.txt or copy at |
12 | // http://www.boost.org/LICENSE_1_0.txt |
13 | // |
14 | |
15 | #include <boost/smart_ptr/enable_shared_from_raw.hpp> |
16 | #include <boost/weak_ptr.hpp> |
17 | #include <boost/core/lightweight_test.hpp> |
18 | |
19 | class X; |
20 | |
21 | static boost::weak_ptr< X > r_; |
22 | |
23 | void register_( boost::weak_ptr< X > const & r ) |
24 | { |
25 | r_ = r; |
26 | } |
27 | |
28 | void check_( boost::weak_ptr< X > const & r ) |
29 | { |
30 | BOOST_TEST( !( r < r_ ) && !( r_ < r ) ); |
31 | } |
32 | |
33 | void unregister_( boost::weak_ptr< X > const & r ) |
34 | { |
35 | BOOST_TEST( !( r < r_ ) && !( r_ < r ) ); |
36 | r_.reset(); |
37 | } |
38 | |
39 | class X: public boost::enable_shared_from_raw |
40 | { |
41 | public: |
42 | |
43 | X() |
44 | { |
45 | register_( r: boost::shared_from_raw( p: this ) ); |
46 | } |
47 | |
48 | ~X() |
49 | { |
50 | unregister_( r: boost::weak_from_raw( p: this ) ); |
51 | } |
52 | |
53 | void check() |
54 | { |
55 | check_( r: boost::weak_from_raw( p: this ) ); |
56 | } |
57 | }; |
58 | |
59 | int main() |
60 | { |
61 | { |
62 | boost::shared_ptr< X > px( new X ); |
63 | px->check(); |
64 | } |
65 | |
66 | return boost::report_errors(); |
67 | } |
68 |