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 | #include <boost/smart_ptr/allocate_unique.hpp> |
13 | #include <boost/core/lightweight_test.hpp> |
14 | |
15 | template<class T = void> |
16 | struct creator { |
17 | typedef T value_type; |
18 | typedef T* pointer; |
19 | |
20 | template<class U> |
21 | struct rebind { |
22 | typedef creator<U> other; |
23 | }; |
24 | |
25 | creator() { } |
26 | |
27 | template<class U> |
28 | creator(const creator<U>&) { } |
29 | |
30 | T* allocate(std::size_t size) { |
31 | return static_cast<T*>(::operator new(sizeof(T) * size)); |
32 | } |
33 | |
34 | void deallocate(T* ptr, std::size_t) { |
35 | ::operator delete(ptr); |
36 | } |
37 | }; |
38 | |
39 | template<class T, class U> |
40 | inline bool |
41 | operator==(const creator<T>&, const creator<U>&) |
42 | { |
43 | return true; |
44 | } |
45 | |
46 | template<class T, class U> |
47 | inline bool |
48 | operator!=(const creator<T>&, const creator<U>&) |
49 | { |
50 | return false; |
51 | } |
52 | |
53 | class type { |
54 | public: |
55 | type(int x, int y) |
56 | : value_(x + y) { } |
57 | |
58 | int sum() const { |
59 | return value_; |
60 | } |
61 | |
62 | private: |
63 | int value_; |
64 | }; |
65 | |
66 | int main() |
67 | { |
68 | { |
69 | std::unique_ptr<int, |
70 | boost::alloc_deleter<int, creator<int> > > result = |
71 | boost::allocate_unique<int>(alloc: creator<int>(), value: 1); |
72 | BOOST_TEST(result.get() != 0); |
73 | BOOST_TEST(*result == 1); |
74 | } |
75 | { |
76 | std::unique_ptr<const int, |
77 | boost::alloc_deleter<const int, creator<> > > result = |
78 | boost::allocate_unique<const int>(alloc: creator<>(), args: 1); |
79 | BOOST_TEST(result.get() != 0); |
80 | BOOST_TEST(*result == 1); |
81 | } |
82 | { |
83 | std::unique_ptr<type, |
84 | boost::alloc_deleter<type, creator<type> > > result = |
85 | boost::allocate_unique<type>(alloc: creator<type>(), value: type(1, 2)); |
86 | BOOST_TEST(result.get() != 0); |
87 | BOOST_TEST(result->sum() == 3); |
88 | } |
89 | { |
90 | std::unique_ptr<const type, |
91 | boost::alloc_deleter<const type, creator<> > > result = |
92 | boost::allocate_unique<const type>(alloc: creator<>(), args: type(1, 2)); |
93 | BOOST_TEST(result.get() != 0); |
94 | BOOST_TEST(result->sum() == 3); |
95 | } |
96 | return boost::report_errors(); |
97 | } |
98 | #else |
99 | int main() |
100 | { |
101 | return 0; |
102 | } |
103 | #endif |
104 | |