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