1 #include <boost/config.hpp>
2 
3 //  shared_ptr_alloc11_test.cpp
4 //
5 //  Test the allocator constructor with a C++11 minimal allocator
6 //
7 //  Copyright (c) 2005, 2014 Peter Dimov
8 //
9 //  Distributed under the Boost Software License, Version 1.0.
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/detail/lightweight_test.hpp>
15 #include <boost/shared_ptr.hpp>
16 #include <memory>
17 #include <cstddef>
18 
19 #if !defined( BOOST_NO_CXX11_ALLOCATOR )
20 
21 template< class T > class cxx11_allocator
22 {
23 public:
24 
25     typedef T value_type;
26 
cxx11_allocator()27     cxx11_allocator()
28     {
29     }
30 
cxx11_allocator(cxx11_allocator<Y> const &)31     template< class Y > cxx11_allocator( cxx11_allocator<Y> const & )
32     {
33     }
34 
allocate(std::size_t n)35     T * allocate( std::size_t n )
36     {
37         return static_cast< T* >( ::operator new( n * sizeof( T ) ) );
38     }
39 
deallocate(T * p,std::size_t n)40     void deallocate( T * p, std::size_t n )
41     {
42         ::operator delete( p );
43     }
44 };
45 
46 //
47 
48 struct D;
49 
50 struct X
51 {
52     static int instances;
53 
XX54     X(): deleted_( false )
55     {
56         ++instances;
57     }
58 
~XX59     ~X()
60     {
61         BOOST_TEST( deleted_ );
62         --instances;
63     }
64 
65 private:
66 
67     friend struct D;
68 
69     bool deleted_;
70 
71     X( X const & );
72     X & operator=( X const & );
73 };
74 
75 int X::instances = 0;
76 
77 struct D
78 {
operator ()D79     void operator()( X * px ) const
80     {
81         px->deleted_ = true;
82         delete px;
83     }
84 };
85 
main()86 int main()
87 {
88     BOOST_TEST( X::instances == 0 );
89 
90     boost::shared_ptr<void> pv( new X, D(), cxx11_allocator<X>() );
91 
92     BOOST_TEST( X::instances == 1 );
93 
94     pv.reset();
95 
96     BOOST_TEST( X::instances == 0 );
97 
98     pv.reset( new X, D(), cxx11_allocator<void>() );
99 
100     BOOST_TEST( X::instances == 1 );
101 
102     pv.reset();
103 
104     BOOST_TEST( X::instances == 0 );
105 
106     return boost::report_errors();
107 }
108 
109 #else // !defined( BOOST_NO_CXX11_ALLOCATOR )
110 
main()111 int main()
112 {
113     return 0;
114 }
115 
116 #endif
117