1 /*
2  * Copyright (c) 2012-2014 Glen Joseph Fernandes
3  * glenfe at live dot com
4  *
5  * Distributed under the Boost Software License,
6  * Version 1.0. (See accompanying file LICENSE_1_0.txt
7  * or copy at http://boost.org/LICENSE_1_0.txt)
8  */
9 #include <boost/detail/lightweight_test.hpp>
10 #if !defined(BOOST_NO_CXX11_ALLOCATOR)
11 #include <boost/smart_ptr/allocate_shared_array.hpp>
12 
13 template<typename T>
14 class creator {
15 public:
16     typedef T value_type;
17 
creator()18     creator() {
19     }
20 
21     template<typename U>
creator(const creator<U> &)22     creator(const creator<U>&) {
23     }
24 
allocate(std::size_t size)25     T* allocate(std::size_t size) {
26         void* p1 = ::operator new(size * sizeof(T));
27         return static_cast<T*>(p1);
28     }
29 
deallocate(T * memory,std::size_t)30     void deallocate(T* memory, std::size_t) {
31         void* p1 = memory;
32         ::operator delete(p1);
33     }
34 
35     template<typename U>
construct(U * memory)36     void construct(U* memory) {
37         void* p1 = memory;
38         ::new(p1) U();
39     }
40 
41     template<typename U>
destroy(U * memory)42     void destroy(U* memory) {
43         memory->~U();
44     }
45 };
46 
47 class type {
48     friend class creator<type>;
49 
50 public:
51     static unsigned int instances;
52     static const type object;
53 
54 protected:
type()55     explicit type() {
56         instances++;
57     }
58 
type(const type &)59     type(const type&) {
60         instances++;
61     }
62 
~type()63     ~type() {
64         instances--;
65     }
66 };
67 
68 unsigned int type::instances;
69 const type type::object;
70 
main()71 int main() {
72     BOOST_TEST(type::instances == 1);
73     {
74         boost::shared_ptr<type[]> a1 = boost::allocate_shared<type[]>(creator<void>(), 3);
75         BOOST_TEST(a1.use_count() == 1);
76         BOOST_TEST(a1.get() != 0);
77         BOOST_TEST(type::instances == 4);
78         a1.reset();
79         BOOST_TEST(type::instances == 1);
80     }
81 
82     BOOST_TEST(type::instances == 1);
83     {
84         boost::shared_ptr<type[3]> a1 = boost::allocate_shared<type[3]>(creator<void>());
85         BOOST_TEST(a1.use_count() == 1);
86         BOOST_TEST(a1.get() != 0);
87         BOOST_TEST(type::instances == 4);
88         a1.reset();
89         BOOST_TEST(type::instances == 1);
90     }
91 
92     BOOST_TEST(type::instances == 1);
93     {
94         boost::shared_ptr<type[][2]> a1 = boost::allocate_shared<type[][2]>(creator<void>(), 2);
95         BOOST_TEST(a1.get() != 0);
96         BOOST_TEST(a1.use_count() == 1);
97         BOOST_TEST(type::instances == 5);
98         a1.reset();
99         BOOST_TEST(type::instances == 1);
100     }
101 
102     BOOST_TEST(type::instances == 1);
103     {
104         boost::shared_ptr<type[2][2]> a1 = boost::allocate_shared<type[2][2]>(creator<void>());
105         BOOST_TEST(a1.get() != 0);
106         BOOST_TEST(a1.use_count() == 1);
107         BOOST_TEST(type::instances == 5);
108         a1.reset();
109         BOOST_TEST(type::instances == 1);
110     }
111 
112     BOOST_TEST(type::instances == 1);
113     {
114         boost::shared_ptr<const type[]> a1 = boost::allocate_shared<const type[]>(creator<void>(), 3);
115         BOOST_TEST(a1.get() != 0);
116         BOOST_TEST(a1.use_count() == 1);
117         BOOST_TEST(type::instances == 4);
118         a1.reset();
119         BOOST_TEST(type::instances == 1);
120     }
121 
122     BOOST_TEST(type::instances == 1);
123     {
124         boost::shared_ptr<const type[3]> a1 = boost::allocate_shared<const type[3]>(creator<void>());
125         BOOST_TEST(a1.get() != 0);
126         BOOST_TEST(a1.use_count() == 1);
127         BOOST_TEST(type::instances == 4);
128         a1.reset();
129         BOOST_TEST(type::instances == 1);
130     }
131 
132     BOOST_TEST(type::instances == 1);
133     {
134         boost::shared_ptr<const type[][2]> a1 = boost::allocate_shared<const type[][2]>(creator<void>(), 2);
135         BOOST_TEST(a1.get() != 0);
136         BOOST_TEST(a1.use_count() == 1);
137         BOOST_TEST(type::instances == 5);
138         a1.reset();
139         BOOST_TEST(type::instances == 1);
140     }
141 
142     BOOST_TEST(type::instances == 1);
143     {
144         boost::shared_ptr<const type[2][2]> a1 = boost::allocate_shared<const type[2][2]>(creator<void>());
145         BOOST_TEST(a1.get() != 0);
146         BOOST_TEST(a1.use_count() == 1);
147         BOOST_TEST(type::instances == 5);
148         a1.reset();
149         BOOST_TEST(type::instances == 1);
150     }
151 
152     return boost::report_errors();
153 }
154 #else
155 
main()156 int main() {
157     return 0;
158 }
159 
160 #endif
161