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_LIBSTDCXX_VERSION) || \
10 BOOST_LIBSTDCXX_VERSION >= 46000) && \
11 !defined(BOOST_NO_CXX11_SMART_PTR) && \
12 !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
13 #include <boost/smart_ptr/allocate_unique.hpp>
14 #include <boost/core/lightweight_test.hpp>
15
16 template<class T = void>
17 struct creator {
18 typedef T value_type;
19 typedef T* pointer;
20
21 template<class U>
22 struct rebind {
23 typedef creator<U> other;
24 };
25
creatorcreator26 creator() { }
27
28 template<class U>
creatorcreator29 creator(const creator<U>&) { }
30
allocatecreator31 T* allocate(std::size_t size) {
32 return static_cast<T*>(::operator new(sizeof(T) * size));
33 }
34
deallocatecreator35 void deallocate(T* ptr, std::size_t) {
36 ::operator delete(ptr);
37 }
38 };
39
40 template<class T, class U>
41 inline bool
operator ==(const creator<T> &,const creator<U> &)42 operator==(const creator<T>&, const creator<U>&)
43 {
44 return true;
45 }
46
47 template<class T, class U>
48 inline bool
operator !=(const creator<T> &,const creator<U> &)49 operator!=(const creator<T>&, const creator<U>&)
50 {
51 return false;
52 }
53
54 class type {
55 public:
56 static unsigned instances;
57
type(int v1,int v2,int v3,int v4,int v5)58 type(int v1, int v2, int v3, int v4, int v5)
59 : sum_(v1 + v2 + v3 + v4 + v5) {
60 ++instances;
61 }
62
~type()63 ~type() {
64 --instances;
65 }
66
sum() const67 int sum() const {
68 return sum_;
69 }
70
71 private:
72 int sum_;
73
74 type(const type&);
75 type& operator=(const type&);
76 };
77
78 unsigned type::instances = 0;
79
main()80 int main()
81 {
82 BOOST_TEST(type::instances == 0);
83 {
84 std::unique_ptr<type,
85 boost::alloc_deleter<type, creator<type> > > result =
86 boost::allocate_unique<type>(creator<type>(), 1, 2, 3, 4, 5);
87 BOOST_TEST(result.get() != 0);
88 BOOST_TEST(type::instances == 1);
89 BOOST_TEST(result->sum() == 15);
90 result.reset();
91 BOOST_TEST(type::instances == 0);
92 }
93 BOOST_TEST(type::instances == 0);
94 {
95 std::unique_ptr<const type,
96 boost::alloc_deleter<const type, creator<> > > result =
97 boost::allocate_unique<const type>(creator<>(), 1, 2, 3, 4, 5);
98 BOOST_TEST(result.get() != 0);
99 BOOST_TEST(type::instances == 1);
100 BOOST_TEST(result->sum() == 15);
101 result.reset();
102 BOOST_TEST(type::instances == 0);
103 }
104 return boost::report_errors();
105 }
106 #else
main()107 int main()
108 {
109 return 0;
110 }
111 #endif
112