1 /*
2 Copyright 2019 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_SMART_PTR) && !defined(BOOST_NO_CXX11_ALLOCATOR)
10 #include <boost/smart_ptr/allocate_unique.hpp>
11 #include <boost/core/lightweight_test.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 std::unique_ptr<type,
86 boost::alloc_deleter<type, creator<type> > > result =
87 boost::allocate_unique<type>(creator<type>());
88 BOOST_TEST(result.get() != 0);
89 BOOST_TEST(type::instances == 1);
90 result.reset();
91 BOOST_TEST(type::instances == 0);
92 }
93 {
94 std::unique_ptr<const type,
95 boost::alloc_deleter<const type, creator<> > > result =
96 boost::allocate_unique<const type>(creator<>());
97 BOOST_TEST(result.get() != 0);
98 BOOST_TEST(type::instances == 1);
99 result.reset();
100 BOOST_TEST(type::instances == 0);
101 }
102 return boost::report_errors();
103 }
104 #else
main()105 int main()
106 {
107 return 0;
108 }
109 #endif
110