| 1 | /* |
| 2 | Copyright 2021 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> |
| 16 | class point { |
| 17 | public: |
| 18 | point() |
| 19 | : state_() |
| 20 | , ptr_() { } |
| 21 | |
| 22 | point(int count, T* value) |
| 23 | : state_(count) |
| 24 | , ptr_(value) { } |
| 25 | |
| 26 | operator bool() const { |
| 27 | return static_cast<bool>(ptr_); |
| 28 | } |
| 29 | |
| 30 | T* operator->() const { |
| 31 | return ptr_; |
| 32 | } |
| 33 | |
| 34 | int state() const { |
| 35 | return state_; |
| 36 | } |
| 37 | |
| 38 | private: |
| 39 | int state_; |
| 40 | T* ptr_; |
| 41 | }; |
| 42 | |
| 43 | template<class T = void> |
| 44 | class creator { |
| 45 | public: |
| 46 | typedef T value_type; |
| 47 | typedef point<T> pointer; |
| 48 | typedef std::ptrdiff_t difference_type; |
| 49 | |
| 50 | creator() |
| 51 | : state_() { } |
| 52 | |
| 53 | explicit creator(int value) |
| 54 | : state_(value) { } |
| 55 | |
| 56 | template<class U> |
| 57 | creator(const creator<U>& other) |
| 58 | : state_(other.state()) { } |
| 59 | |
| 60 | pointer allocate(std::size_t size) { |
| 61 | return pointer(state_, |
| 62 | static_cast<T*>(::operator new(sizeof(T) * size))); |
| 63 | } |
| 64 | |
| 65 | void deallocate(pointer ptr, std::size_t) { |
| 66 | ::operator delete(ptr.operator->()); |
| 67 | } |
| 68 | |
| 69 | int state() const { |
| 70 | return state_; |
| 71 | } |
| 72 | |
| 73 | private: |
| 74 | int state_; |
| 75 | }; |
| 76 | |
| 77 | template<class T, class U> |
| 78 | inline bool |
| 79 | operator==(const creator<T>&, const creator<U>&) |
| 80 | { |
| 81 | return true; |
| 82 | } |
| 83 | |
| 84 | template<class T, class U> |
| 85 | inline bool |
| 86 | operator!=(const creator<T>&, const creator<U>&) |
| 87 | { |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | int main() |
| 92 | { |
| 93 | { |
| 94 | std::unique_ptr<int, |
| 95 | boost::alloc_deleter<int, creator<> > > result = |
| 96 | boost::allocate_unique<int>(alloc: creator<>(1)); |
| 97 | point<int> ptr = boost::get_allocator_pointer(p: result); |
| 98 | BOOST_TEST_EQ(ptr.state(), 1); |
| 99 | } |
| 100 | { |
| 101 | std::unique_ptr<const int, |
| 102 | boost::alloc_deleter<const int, creator<> > > result = |
| 103 | boost::allocate_unique<const int>(alloc: creator<>(2)); |
| 104 | point<int> ptr = boost::get_allocator_pointer(p: result); |
| 105 | BOOST_TEST_EQ(ptr.state(), 2); |
| 106 | } |
| 107 | { |
| 108 | std::unique_ptr<int[], |
| 109 | boost::alloc_deleter<int[], creator<> > > result = |
| 110 | boost::allocate_unique<int[]>(alloc: creator<>(3), size: 3); |
| 111 | point<int> ptr = boost::get_allocator_pointer(p: result); |
| 112 | BOOST_TEST_EQ(ptr.state(), 3); |
| 113 | } |
| 114 | { |
| 115 | std::unique_ptr<int[], |
| 116 | boost::alloc_deleter<int[3], creator<> > > result = |
| 117 | boost::allocate_unique<int[3]>(alloc: creator<>(4)); |
| 118 | point<int> ptr = boost::get_allocator_pointer(p: result); |
| 119 | BOOST_TEST_EQ(ptr.state(), 4); |
| 120 | } |
| 121 | { |
| 122 | std::unique_ptr<int[][2], |
| 123 | boost::alloc_deleter<int[][2], creator<> > > result = |
| 124 | boost::allocate_unique<int[][2]>(alloc: creator<>(5), size: 2); |
| 125 | point<int[2]> ptr = boost::get_allocator_pointer(p: result); |
| 126 | BOOST_TEST_EQ(ptr.state(), 5); |
| 127 | } |
| 128 | { |
| 129 | std::unique_ptr<int[][2], |
| 130 | boost::alloc_deleter<int[2][2], creator<> > > result = |
| 131 | boost::allocate_unique<int[2][2]>(alloc: creator<>(6)); |
| 132 | point<int[2]> ptr = boost::get_allocator_pointer(p: result); |
| 133 | BOOST_TEST_EQ(ptr.state(), 6); |
| 134 | } |
| 135 | { |
| 136 | std::unique_ptr<const int[], |
| 137 | boost::alloc_deleter<const int[], creator<> > > result = |
| 138 | boost::allocate_unique<const int[]>(alloc: creator<>(7), size: 3); |
| 139 | point<int> ptr = boost::get_allocator_pointer(p: result); |
| 140 | BOOST_TEST_EQ(ptr.state(), 7); |
| 141 | } |
| 142 | { |
| 143 | std::unique_ptr<const int[], |
| 144 | boost::alloc_deleter<const int[3], creator<> > > result = |
| 145 | boost::allocate_unique<const int[3]>(alloc: creator<>(8)); |
| 146 | point<int> ptr = boost::get_allocator_pointer(p: result); |
| 147 | BOOST_TEST_EQ(ptr.state(), 8); |
| 148 | } |
| 149 | { |
| 150 | std::unique_ptr<const int[][2], |
| 151 | boost::alloc_deleter<const int[][2], creator<> > > result = |
| 152 | boost::allocate_unique<const int[][2]>(alloc: creator<>(9), size: 2); |
| 153 | point<int[2]> ptr = boost::get_allocator_pointer(p: result); |
| 154 | BOOST_TEST_EQ(ptr.state(), 9); |
| 155 | } |
| 156 | { |
| 157 | std::unique_ptr<const int[][2], |
| 158 | boost::alloc_deleter<const int[2][2], creator<> > > result = |
| 159 | boost::allocate_unique<const int[2][2]>(alloc: creator<>(10)); |
| 160 | point<int[2]> ptr = boost::get_allocator_pointer(p: result); |
| 161 | BOOST_TEST_EQ(ptr.state(), 10); |
| 162 | } |
| 163 | { |
| 164 | std::unique_ptr<int, |
| 165 | boost::alloc_deleter<int, |
| 166 | boost::noinit_adaptor<creator<> > > > result = |
| 167 | boost::allocate_unique_noinit<int>(alloc: creator<>(11)); |
| 168 | point<int> ptr = boost::get_allocator_pointer(p: result); |
| 169 | BOOST_TEST_EQ(ptr.state(), 11); |
| 170 | } |
| 171 | { |
| 172 | std::unique_ptr<const int, |
| 173 | boost::alloc_deleter<const int, |
| 174 | boost::noinit_adaptor<creator<> > > > result = |
| 175 | boost::allocate_unique_noinit<const int>(alloc: creator<>(12)); |
| 176 | point<int> ptr = boost::get_allocator_pointer(p: result); |
| 177 | BOOST_TEST_EQ(ptr.state(), 12); |
| 178 | } |
| 179 | { |
| 180 | std::unique_ptr<int[], |
| 181 | boost::alloc_deleter<int[], |
| 182 | boost::noinit_adaptor<creator<> > > > result = |
| 183 | boost::allocate_unique_noinit<int[]>(alloc: creator<>(13), size: 3); |
| 184 | point<int> ptr = boost::get_allocator_pointer(p: result); |
| 185 | BOOST_TEST_EQ(ptr.state(), 13); |
| 186 | } |
| 187 | { |
| 188 | std::unique_ptr<int[], |
| 189 | boost::alloc_deleter<int[3], |
| 190 | boost::noinit_adaptor<creator<> > > > result = |
| 191 | boost::allocate_unique_noinit<int[3]>(alloc: creator<>(14)); |
| 192 | point<int> ptr = boost::get_allocator_pointer(p: result); |
| 193 | BOOST_TEST_EQ(ptr.state(), 14); |
| 194 | } |
| 195 | { |
| 196 | std::unique_ptr<int[][2], |
| 197 | boost::alloc_deleter<int[][2], |
| 198 | boost::noinit_adaptor<creator<> > > > result = |
| 199 | boost::allocate_unique_noinit<int[][2]>(alloc: creator<>(15), size: 2); |
| 200 | point<int[2]> ptr = boost::get_allocator_pointer(p: result); |
| 201 | BOOST_TEST_EQ(ptr.state(), 15); |
| 202 | } |
| 203 | { |
| 204 | std::unique_ptr<int[][2], |
| 205 | boost::alloc_deleter<int[2][2], |
| 206 | boost::noinit_adaptor<creator<> > > > result = |
| 207 | boost::allocate_unique_noinit<int[2][2]>(alloc: creator<>(16)); |
| 208 | point<int[2]> ptr = boost::get_allocator_pointer(p: result); |
| 209 | BOOST_TEST_EQ(ptr.state(), 16); |
| 210 | } |
| 211 | { |
| 212 | std::unique_ptr<const int[], |
| 213 | boost::alloc_deleter<const int[], |
| 214 | boost::noinit_adaptor<creator<> > > > result = |
| 215 | boost::allocate_unique_noinit<const int[]>(alloc: creator<>(17), size: 3); |
| 216 | point<int> ptr = boost::get_allocator_pointer(p: result); |
| 217 | BOOST_TEST_EQ(ptr.state(), 17); |
| 218 | } |
| 219 | { |
| 220 | std::unique_ptr<const int[], |
| 221 | boost::alloc_deleter<const int[3], |
| 222 | boost::noinit_adaptor<creator<> > > > result = |
| 223 | boost::allocate_unique_noinit<const int[3]>(alloc: creator<>(18)); |
| 224 | point<int> ptr = boost::get_allocator_pointer(p: result); |
| 225 | BOOST_TEST_EQ(ptr.state(), 18); |
| 226 | } |
| 227 | { |
| 228 | std::unique_ptr<const int[][2], |
| 229 | boost::alloc_deleter<const int[][2], |
| 230 | boost::noinit_adaptor<creator<> > > > result = |
| 231 | boost::allocate_unique_noinit<const int[][2]>(alloc: creator<>(19), size: 2); |
| 232 | point<int[2]> ptr = boost::get_allocator_pointer(p: result); |
| 233 | BOOST_TEST_EQ(ptr.state(), 19); |
| 234 | } |
| 235 | { |
| 236 | std::unique_ptr<const int[][2], |
| 237 | boost::alloc_deleter<const int[2][2], |
| 238 | boost::noinit_adaptor<creator<> > > > result = |
| 239 | boost::allocate_unique_noinit<const int[2][2]>(alloc: creator<>(20)); |
| 240 | point<int[2]> ptr = boost::get_allocator_pointer(p: result); |
| 241 | BOOST_TEST_EQ(ptr.state(), 20); |
| 242 | } |
| 243 | return boost::report_errors(); |
| 244 | } |
| 245 | #else |
| 246 | int main() |
| 247 | { |
| 248 | return 0; |
| 249 | } |
| 250 | #endif |
| 251 | |