| 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/core/allocator_traits.hpp> |
| 9 | #include <boost/core/lightweight_test_trait.hpp> |
| 10 | |
| 11 | template<class T> |
| 12 | class creator { |
| 13 | public: |
| 14 | typedef T value_type; |
| 15 | typedef T* pointer; |
| 16 | typedef const T* const_pointer; |
| 17 | typedef void* void_pointer; |
| 18 | typedef const void* const_void_pointer; |
| 19 | typedef std::size_t size_type; |
| 20 | typedef std::ptrdiff_t difference_type; |
| 21 | |
| 22 | template<class U> |
| 23 | struct rebind { |
| 24 | typedef creator<U> other; |
| 25 | }; |
| 26 | |
| 27 | explicit creator(int value) BOOST_NOEXCEPT |
| 28 | : state_(value) { } |
| 29 | |
| 30 | template<class U> |
| 31 | creator(const creator<U>& other) BOOST_NOEXCEPT |
| 32 | : state_(other.state) { } |
| 33 | |
| 34 | std::size_t max_size() const BOOST_NOEXCEPT { |
| 35 | return static_cast<std::size_t>(-1) / sizeof(T); |
| 36 | } |
| 37 | |
| 38 | T* allocate(std::size_t n, const void* = 0) { |
| 39 | return static_cast<T*>(::operator new(sizeof(T) * n)); |
| 40 | } |
| 41 | |
| 42 | void deallocate(T* p, std::size_t) { |
| 43 | ::operator delete(p); |
| 44 | } |
| 45 | |
| 46 | int state() const { |
| 47 | return state_; |
| 48 | } |
| 49 | |
| 50 | private: |
| 51 | int state_; |
| 52 | }; |
| 53 | |
| 54 | template<class T, class U> |
| 55 | inline bool operator==(const creator<T>& lhs, |
| 56 | const creator<U>& rhs) BOOST_NOEXCEPT |
| 57 | { |
| 58 | return lhs.state() == rhs.state(); |
| 59 | } |
| 60 | |
| 61 | template<class T, class U> |
| 62 | inline bool operator!=(const creator<T>& lhs, |
| 63 | const creator<U>& rhs) BOOST_NOEXCEPT |
| 64 | { |
| 65 | return !(lhs == rhs); |
| 66 | } |
| 67 | |
| 68 | int main() |
| 69 | { |
| 70 | BOOST_TEST_TRAIT_SAME(creator<int>, |
| 71 | boost::allocator_traits<creator<int> >::allocator_type); |
| 72 | BOOST_TEST_TRAIT_SAME(int, |
| 73 | boost::allocator_traits<creator<int> >::value_type); |
| 74 | BOOST_TEST_TRAIT_SAME(int*, |
| 75 | boost::allocator_traits<creator<int> >::pointer); |
| 76 | BOOST_TEST_TRAIT_SAME(const int*, |
| 77 | boost::allocator_traits<creator<int> >::const_pointer); |
| 78 | BOOST_TEST_TRAIT_SAME(void*, |
| 79 | boost::allocator_traits<creator<int> >::void_pointer); |
| 80 | BOOST_TEST_TRAIT_SAME(const void*, |
| 81 | boost::allocator_traits<creator<int> >::const_void_pointer); |
| 82 | BOOST_TEST_TRAIT_SAME(std::ptrdiff_t, |
| 83 | boost::allocator_traits<creator<int> >::difference_type); |
| 84 | BOOST_TEST_TRAIT_SAME(std::size_t, |
| 85 | boost::allocator_traits<creator<int> >::size_type); |
| 86 | BOOST_TEST_TRAIT_FALSE((boost::allocator_traits<creator<int> >:: |
| 87 | propagate_on_container_copy_assignment)); |
| 88 | BOOST_TEST_TRAIT_FALSE((boost::allocator_traits<creator<int> >:: |
| 89 | propagate_on_container_move_assignment)); |
| 90 | BOOST_TEST_TRAIT_FALSE((boost::allocator_traits<creator<int> >:: |
| 91 | propagate_on_container_swap)); |
| 92 | BOOST_TEST_TRAIT_FALSE((boost::allocator_traits<creator<int> >:: |
| 93 | is_always_equal)); |
| 94 | BOOST_TEST_TRAIT_SAME(creator<char>, |
| 95 | boost::allocator_traits<creator<int> >:: |
| 96 | rebind_traits<char>::allocator_type); |
| 97 | creator<int> a(1); |
| 98 | int* p1 = boost::allocator_traits<creator<int> >::allocate(a, n: 1); |
| 99 | if (BOOST_TEST(p1)) { |
| 100 | boost::allocator_traits<creator<int> >::construct(a, p: p1, v: 5); |
| 101 | BOOST_TEST_EQ(*p1, 5); |
| 102 | boost::allocator_traits<creator<int> >::destroy(a, p: p1); |
| 103 | boost::allocator_traits<creator<int> >::deallocate(a, p: p1, n: 1); |
| 104 | } |
| 105 | int* p2 = boost::allocator_traits<creator<int> >::allocate(a, n: 1, h: 0); |
| 106 | if (BOOST_TEST(p2)) { |
| 107 | boost::allocator_traits<creator<int> >::deallocate(a, p: p2, n: 1); |
| 108 | } |
| 109 | BOOST_TEST_EQ(boost::allocator_traits<creator<int> >::max_size(a), |
| 110 | static_cast<std::size_t>(-1) / sizeof(int)); |
| 111 | BOOST_TEST(boost::allocator_traits<creator<int> >:: |
| 112 | select_on_container_copy_construction(a) == a); |
| 113 | return boost::report_errors(); |
| 114 | } |
| 115 | |