Imported Upstream version 1.57.0
[platform/upstream/boost.git] / libs / smart_ptr / test / intrusive_ptr_move_test.cpp
1 #include <boost/config.hpp>
2
3 #if defined(BOOST_MSVC)
4
5 #pragma warning(disable: 4786)  // identifier truncated in debug info
6 #pragma warning(disable: 4710)  // function not inlined
7 #pragma warning(disable: 4711)  // function selected for automatic inline expansion
8 #pragma warning(disable: 4514)  // unreferenced inline removed
9 #pragma warning(disable: 4355)  // 'this' : used in base member initializer list
10 #pragma warning(disable: 4511)  // copy constructor could not be generated
11 #pragma warning(disable: 4512)  // assignment operator could not be generated
12
13 #if (BOOST_MSVC >= 1310)
14 #pragma warning(disable: 4675)  // resolved overload found with Koenig lookup
15 #endif
16
17 #endif
18
19 //
20 //  intrusive_ptr_move_test.cpp
21 //
22 //  Copyright (c) 2002-2005 Peter Dimov
23 //
24 // Distributed under the Boost Software License, Version 1.0. (See
25 // accompanying file LICENSE_1_0.txt or copy at
26 // http://www.boost.org/LICENSE_1_0.txt)
27 //
28
29 #include <boost/detail/lightweight_test.hpp>
30 #include <boost/intrusive_ptr.hpp>
31 #include <boost/detail/atomic_count.hpp>
32 #include <boost/config.hpp>
33 #include <utility>
34
35 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
36
37 namespace N
38 {
39
40 class base
41 {
42 private:
43
44     boost::detail::atomic_count use_count_;
45
46     base(base const &);
47     base & operator=(base const &);
48
49 protected:
50
51     base(): use_count_(0)
52     {
53         ++instances;
54     }
55
56     virtual ~base()
57     {
58         --instances;
59     }
60
61 public:
62
63     static long instances;
64
65     long use_count() const
66     {
67         return use_count_;
68     }
69
70 #if !defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
71
72     inline friend void intrusive_ptr_add_ref(base * p)
73     {
74         ++p->use_count_;
75     }
76
77     inline friend void intrusive_ptr_release(base * p)
78     {
79         if(--p->use_count_ == 0) delete p;
80     }
81
82 #else
83
84     void add_ref()
85     {
86         ++use_count_;
87     }
88
89     void release()
90     {
91         if(--use_count_ == 0) delete this;
92     }
93
94 #endif
95 };
96
97 long base::instances = 0;
98
99 } // namespace N
100
101 #if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
102
103 namespace boost
104 {
105
106 inline void intrusive_ptr_add_ref(N::base * p)
107 {
108     p->add_ref();
109 }
110
111 inline void intrusive_ptr_release(N::base * p)
112 {
113     p->release();
114 }
115
116 } // namespace boost
117
118 #endif
119
120 //
121
122 struct X: public virtual N::base
123 {
124 };
125
126 struct Y: public X
127 {
128 };
129
130 int main()
131 {
132     BOOST_TEST( N::base::instances == 0 );
133
134     {
135         boost::intrusive_ptr<X> p( new X );
136         BOOST_TEST( N::base::instances == 1 );
137
138         boost::intrusive_ptr<X> p2( std::move( p ) );
139         BOOST_TEST( N::base::instances == 1 );
140         BOOST_TEST( p.get() == 0 );
141
142         p2.reset();
143         BOOST_TEST( N::base::instances == 0 );
144     }
145
146     {
147         boost::intrusive_ptr<X> p( new X );
148         BOOST_TEST( N::base::instances == 1 );
149
150         boost::intrusive_ptr<X> p2;
151         p2 = std::move( p );
152         BOOST_TEST( N::base::instances == 1 );
153         BOOST_TEST( p.get() == 0 );
154
155         p2.reset();
156         BOOST_TEST( N::base::instances == 0 );
157     }
158
159     {
160         boost::intrusive_ptr<X> p( new X );
161         BOOST_TEST( N::base::instances == 1 );
162
163         boost::intrusive_ptr<X> p2( new X );
164         BOOST_TEST( N::base::instances == 2 );
165         p2 = std::move( p );
166         BOOST_TEST( N::base::instances == 1 );
167         BOOST_TEST( p.get() == 0 );
168
169         p2.reset();
170         BOOST_TEST( N::base::instances == 0 );
171     }
172
173     return boost::report_errors();
174 }
175
176 #else // defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
177
178 int main()
179 {
180     return 0;
181 }
182
183 #endif