1 //  allocate_local_shared_esft_test.cpp
2 //
3 //  Copyright 2007-2009, 2017 Peter Dimov
4 //
5 //  Distributed under the Boost Software License, Version 1.0.
6 //  See accompanying file LICENSE_1_0.txt or copy at
7 //  http://www.boost.org/LICENSE_1_0.txt
8 
9 #include <boost/config.hpp>
10 
11 #if defined( BOOST_NO_CXX11_RVALUE_REFERENCES ) || defined( BOOST_NO_CXX11_VARIADIC_TEMPLATES )
12 
main()13 int main()
14 {
15 }
16 
17 #else
18 
19 #include <boost/core/lightweight_test.hpp>
20 #include <boost/smart_ptr/make_local_shared.hpp>
21 #include <boost/shared_ptr.hpp>
22 #include <boost/enable_shared_from_this.hpp>
23 #include <memory>
24 
25 class X: public boost::enable_shared_from_this<X>
26 {
27 private:
28 
29     X( X const & );
30     X & operator=( X const & );
31 
32 public:
33 
34     static int instances;
35 
X(int=0,int=0,int=0,int=0,int=0,int=0,int=0,int=0,int=0)36     explicit X( int = 0, int = 0, int = 0, int = 0, int = 0, int = 0, int = 0, int = 0, int = 0 )
37     {
38         ++instances;
39     }
40 
~X()41     ~X()
42     {
43         --instances;
44     }
45 };
46 
47 int X::instances = 0;
48 
main()49 int main()
50 {
51     BOOST_TEST_EQ( X::instances, 0 );
52 
53     {
54         boost::shared_ptr< X > px = boost::allocate_local_shared< X >( std::allocator<void>() );
55         BOOST_TEST_EQ( X::instances, 1 );
56 
57         try
58         {
59             boost::shared_ptr< X > qx = px->shared_from_this();
60 
61             BOOST_TEST_EQ( px, qx );
62             BOOST_TEST( !( px < qx ) && !( qx < px ) );
63 
64             px.reset();
65             BOOST_TEST_EQ( X::instances, 1 );
66         }
67         catch( boost::bad_weak_ptr const& )
68         {
69             BOOST_ERROR( "px->shared_from_this() failed" );
70         }
71     }
72 
73     BOOST_TEST_EQ( X::instances, 0 );
74 
75     {
76         boost::shared_ptr< X > px = boost::allocate_local_shared_noinit< X >( std::allocator<void>() );
77         BOOST_TEST_EQ( X::instances, 1 );
78 
79         try
80         {
81             boost::shared_ptr< X > qx = px->shared_from_this();
82 
83             BOOST_TEST_EQ( px, qx );
84             BOOST_TEST( !( px < qx ) && !( qx < px ) );
85 
86             px.reset();
87             BOOST_TEST_EQ( X::instances, 1 );
88         }
89         catch( boost::bad_weak_ptr const& )
90         {
91             BOOST_ERROR( "px->shared_from_this() failed" );
92         }
93     }
94 
95     BOOST_TEST_EQ( X::instances, 0 );
96 
97     {
98         boost::shared_ptr< X > px = boost::allocate_local_shared< X >( std::allocator<void>(), 1 );
99         BOOST_TEST_EQ( X::instances, 1 );
100 
101         try
102         {
103             boost::shared_ptr< X > qx = px->shared_from_this();
104 
105             BOOST_TEST_EQ( px, qx );
106             BOOST_TEST( !( px < qx ) && !( qx < px ) );
107 
108             px.reset();
109             BOOST_TEST_EQ( X::instances, 1 );
110         }
111         catch( boost::bad_weak_ptr const& )
112         {
113             BOOST_ERROR( "px->shared_from_this() failed" );
114         }
115     }
116 
117     BOOST_TEST_EQ( X::instances, 0 );
118 
119     {
120         boost::shared_ptr< X > px = boost::allocate_local_shared< X >( std::allocator<void>(), 1, 2 );
121         BOOST_TEST_EQ( X::instances, 1 );
122 
123         try
124         {
125             boost::shared_ptr< X > qx = px->shared_from_this();
126 
127             BOOST_TEST_EQ( px, qx );
128             BOOST_TEST( !( px < qx ) && !( qx < px ) );
129 
130             px.reset();
131             BOOST_TEST_EQ( X::instances, 1 );
132         }
133         catch( boost::bad_weak_ptr const& )
134         {
135             BOOST_ERROR( "px->shared_from_this() failed" );
136         }
137     }
138 
139     BOOST_TEST_EQ( X::instances, 0 );
140 
141     {
142         boost::shared_ptr< X > px = boost::allocate_local_shared< X >( std::allocator<void>(), 1, 2, 3 );
143         BOOST_TEST_EQ( X::instances, 1 );
144 
145         try
146         {
147             boost::shared_ptr< X > qx = px->shared_from_this();
148 
149             BOOST_TEST_EQ( px, qx );
150             BOOST_TEST( !( px < qx ) && !( qx < px ) );
151 
152             px.reset();
153             BOOST_TEST_EQ( X::instances, 1 );
154         }
155         catch( boost::bad_weak_ptr const& )
156         {
157             BOOST_ERROR( "px->shared_from_this() failed" );
158         }
159     }
160 
161     BOOST_TEST_EQ( X::instances, 0 );
162 
163     {
164         boost::shared_ptr< X > px = boost::allocate_local_shared< X >( std::allocator<void>(), 1, 2, 3, 4 );
165         BOOST_TEST_EQ( X::instances, 1 );
166 
167         try
168         {
169             boost::shared_ptr< X > qx = px->shared_from_this();
170 
171             BOOST_TEST_EQ( px, qx );
172             BOOST_TEST( !( px < qx ) && !( qx < px ) );
173 
174             px.reset();
175             BOOST_TEST_EQ( X::instances, 1 );
176         }
177         catch( boost::bad_weak_ptr const& )
178         {
179             BOOST_ERROR( "px->shared_from_this() failed" );
180         }
181     }
182 
183     BOOST_TEST_EQ( X::instances, 0 );
184 
185     {
186         boost::shared_ptr< X > px = boost::allocate_local_shared< X >( std::allocator<void>(), 1, 2, 3, 4, 5 );
187         BOOST_TEST_EQ( X::instances, 1 );
188 
189         try
190         {
191             boost::shared_ptr< X > qx = px->shared_from_this();
192 
193             BOOST_TEST_EQ( px, qx );
194             BOOST_TEST( !( px < qx ) && !( qx < px ) );
195 
196             px.reset();
197             BOOST_TEST_EQ( X::instances, 1 );
198         }
199         catch( boost::bad_weak_ptr const& )
200         {
201             BOOST_ERROR( "px->shared_from_this() failed" );
202         }
203     }
204 
205     BOOST_TEST_EQ( X::instances, 0 );
206 
207     {
208         boost::shared_ptr< X > px = boost::allocate_local_shared< X >( std::allocator<void>(), 1, 2, 3, 4, 5, 6 );
209         BOOST_TEST_EQ( X::instances, 1 );
210 
211         try
212         {
213             boost::shared_ptr< X > qx = px->shared_from_this();
214 
215             BOOST_TEST_EQ( px, qx );
216             BOOST_TEST( !( px < qx ) && !( qx < px ) );
217 
218             px.reset();
219             BOOST_TEST_EQ( X::instances, 1 );
220         }
221         catch( boost::bad_weak_ptr const& )
222         {
223             BOOST_ERROR( "px->shared_from_this() failed" );
224         }
225     }
226 
227     BOOST_TEST_EQ( X::instances, 0 );
228 
229     {
230         boost::shared_ptr< X > px = boost::allocate_local_shared< X >( std::allocator<void>(), 1, 2, 3, 4, 5, 6, 7 );
231         BOOST_TEST_EQ( X::instances, 1 );
232 
233         try
234         {
235             boost::shared_ptr< X > qx = px->shared_from_this();
236 
237             BOOST_TEST_EQ( px, qx );
238             BOOST_TEST( !( px < qx ) && !( qx < px ) );
239 
240             px.reset();
241             BOOST_TEST_EQ( X::instances, 1 );
242         }
243         catch( boost::bad_weak_ptr const& )
244         {
245             BOOST_ERROR( "px->shared_from_this() failed" );
246         }
247     }
248 
249     BOOST_TEST_EQ( X::instances, 0 );
250 
251     {
252         boost::shared_ptr< X > px = boost::allocate_local_shared< X >( std::allocator<void>(), 1, 2, 3, 4, 5, 6, 7, 8 );
253         BOOST_TEST_EQ( X::instances, 1 );
254 
255         try
256         {
257             boost::shared_ptr< X > qx = px->shared_from_this();
258 
259             BOOST_TEST_EQ( px, qx );
260             BOOST_TEST( !( px < qx ) && !( qx < px ) );
261 
262             px.reset();
263             BOOST_TEST_EQ( X::instances, 1 );
264         }
265         catch( boost::bad_weak_ptr const& )
266         {
267             BOOST_ERROR( "px->shared_from_this() failed" );
268         }
269     }
270 
271     BOOST_TEST_EQ( X::instances, 0 );
272 
273     {
274         boost::shared_ptr< X > px = boost::allocate_local_shared< X >( std::allocator<void>(), 1, 2, 3, 4, 5, 6, 7, 8, 9 );
275         BOOST_TEST_EQ( X::instances, 1 );
276 
277         try
278         {
279             boost::shared_ptr< X > qx = px->shared_from_this();
280 
281             BOOST_TEST_EQ( px, qx );
282             BOOST_TEST( !( px < qx ) && !( qx < px ) );
283 
284             px.reset();
285             BOOST_TEST_EQ( X::instances, 1 );
286         }
287         catch( boost::bad_weak_ptr const& )
288         {
289             BOOST_ERROR( "px->shared_from_this() failed" );
290         }
291     }
292 
293     BOOST_TEST_EQ( X::instances, 0 );
294 
295     return boost::report_errors();
296 }
297 
298 #endif
299