Brian Silverman | fad8f55 | 2018-08-04 23:36:19 -0700 | [diff] [blame^] | 1 | /////////////////////////////////////////////////////////////////////////////// |
| 2 | // |
| 3 | // (C) Copyright Ion Gaztanaga 2005-2013. Distributed under the Boost |
| 4 | // Software License, Version 1.0. (See accompanying file |
| 5 | // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | // |
| 7 | // See http://www.boost.org/libs/container for documentation. |
| 8 | // |
| 9 | /////////////////////////////////////////////////////////////////////////////// |
| 10 | |
| 11 | #ifndef BOOST_CONTAINER_DUMMY_TEST_ALLOCATOR_HPP |
| 12 | #define BOOST_CONTAINER_DUMMY_TEST_ALLOCATOR_HPP |
| 13 | |
| 14 | #ifndef BOOST_CONFIG_HPP |
| 15 | # include <boost/config.hpp> |
| 16 | #endif |
| 17 | |
| 18 | #if defined(BOOST_HAS_PRAGMA_ONCE) |
| 19 | # pragma once |
| 20 | #endif |
| 21 | |
| 22 | #include <boost/container/detail/config_begin.hpp> |
| 23 | #include <boost/container/detail/workaround.hpp> |
| 24 | #include <boost/container/container_fwd.hpp> |
| 25 | |
| 26 | #include <boost/container/throw_exception.hpp> |
| 27 | |
| 28 | #include <boost/container/detail/addressof.hpp> |
| 29 | #include <boost/container/detail/allocation_type.hpp> |
| 30 | #include <boost/container/detail/mpl.hpp> |
| 31 | #include <boost/container/detail/multiallocation_chain.hpp> |
| 32 | #include <boost/container/detail/type_traits.hpp> |
| 33 | #include <boost/container/detail/version_type.hpp> |
| 34 | |
| 35 | #include <boost/move/utility_core.hpp> |
| 36 | #include <boost/move/adl_move_swap.hpp> |
| 37 | |
| 38 | #include <boost/assert.hpp> |
| 39 | |
| 40 | #include <memory> |
| 41 | #include <algorithm> |
| 42 | #include <cstddef> |
| 43 | #include <cassert> |
| 44 | |
| 45 | namespace boost { |
| 46 | namespace container { |
| 47 | namespace test { |
| 48 | |
| 49 | //Very simple version 1 allocator |
| 50 | template<class T> |
| 51 | class simple_allocator |
| 52 | { |
| 53 | public: |
| 54 | typedef T value_type; |
| 55 | |
| 56 | simple_allocator() |
| 57 | {} |
| 58 | |
| 59 | template<class U> |
| 60 | simple_allocator(const simple_allocator<U> &) |
| 61 | {} |
| 62 | |
| 63 | T* allocate(std::size_t n) |
| 64 | { return (T*)::new char[sizeof(T)*n]; } |
| 65 | |
| 66 | void deallocate(T*p, std::size_t) |
| 67 | { delete[] ((char*)p);} |
| 68 | |
| 69 | friend bool operator==(const simple_allocator &, const simple_allocator &) |
| 70 | { return true; } |
| 71 | |
| 72 | friend bool operator!=(const simple_allocator &, const simple_allocator &) |
| 73 | { return false; } |
| 74 | }; |
| 75 | |
| 76 | template< class T |
| 77 | , bool PropagateOnContCopyAssign |
| 78 | , bool PropagateOnContMoveAssign |
| 79 | , bool PropagateOnContSwap |
| 80 | , bool CopyOnPropagateOnContSwap |
| 81 | > |
| 82 | class propagation_test_allocator |
| 83 | { |
| 84 | BOOST_COPYABLE_AND_MOVABLE(propagation_test_allocator) |
| 85 | |
| 86 | public: |
| 87 | typedef T value_type; |
| 88 | typedef boost::container::dtl::bool_<PropagateOnContCopyAssign> |
| 89 | propagate_on_container_copy_assignment; |
| 90 | typedef boost::container::dtl::bool_<PropagateOnContMoveAssign> |
| 91 | propagate_on_container_move_assignment; |
| 92 | typedef boost::container::dtl::bool_<PropagateOnContSwap> |
| 93 | propagate_on_container_swap; |
| 94 | |
| 95 | template<class T2> |
| 96 | struct rebind |
| 97 | { typedef propagation_test_allocator |
| 98 | < T2 |
| 99 | , PropagateOnContCopyAssign |
| 100 | , PropagateOnContMoveAssign |
| 101 | , PropagateOnContSwap |
| 102 | , CopyOnPropagateOnContSwap> other; |
| 103 | }; |
| 104 | |
| 105 | propagation_test_allocator select_on_container_copy_construction() const |
| 106 | { return CopyOnPropagateOnContSwap ? propagation_test_allocator(*this) : propagation_test_allocator(); } |
| 107 | |
| 108 | explicit propagation_test_allocator() |
| 109 | : id_(++unique_id_) |
| 110 | , ctr_copies_(0) |
| 111 | , ctr_moves_(0) |
| 112 | , assign_copies_(0) |
| 113 | , assign_moves_(0) |
| 114 | , swaps_(0) |
| 115 | {} |
| 116 | |
| 117 | propagation_test_allocator(const propagation_test_allocator &x) |
| 118 | : id_(x.id_) |
| 119 | , ctr_copies_(x.ctr_copies_+1) |
| 120 | , ctr_moves_(x.ctr_moves_) |
| 121 | , assign_copies_(x.assign_copies_) |
| 122 | , assign_moves_(x.assign_moves_) |
| 123 | , swaps_(x.swaps_) |
| 124 | {} |
| 125 | |
| 126 | template<class U> |
| 127 | propagation_test_allocator(const propagation_test_allocator |
| 128 | < U |
| 129 | , PropagateOnContCopyAssign |
| 130 | , PropagateOnContMoveAssign |
| 131 | , PropagateOnContSwap |
| 132 | , CopyOnPropagateOnContSwap> &x) |
| 133 | : id_(x.id_) |
| 134 | , ctr_copies_(x.ctr_copies_+1) |
| 135 | , ctr_moves_(0) |
| 136 | , assign_copies_(0) |
| 137 | , assign_moves_(0) |
| 138 | , swaps_(0) |
| 139 | {} |
| 140 | |
| 141 | propagation_test_allocator(BOOST_RV_REF(propagation_test_allocator) x) |
| 142 | : id_(x.id_) |
| 143 | , ctr_copies_(x.ctr_copies_) |
| 144 | , ctr_moves_(x.ctr_moves_ + 1) |
| 145 | , assign_copies_(x.assign_copies_) |
| 146 | , assign_moves_(x.assign_moves_) |
| 147 | , swaps_(x.swaps_) |
| 148 | {} |
| 149 | |
| 150 | propagation_test_allocator &operator=(BOOST_COPY_ASSIGN_REF(propagation_test_allocator) x) |
| 151 | { |
| 152 | id_ = x.id_; |
| 153 | ctr_copies_ = x.ctr_copies_; |
| 154 | ctr_moves_ = x.ctr_moves_; |
| 155 | assign_copies_ = x.assign_copies_+1; |
| 156 | assign_moves_ = x.assign_moves_; |
| 157 | swaps_ = x.swaps_; |
| 158 | return *this; |
| 159 | } |
| 160 | |
| 161 | propagation_test_allocator &operator=(BOOST_RV_REF(propagation_test_allocator) x) |
| 162 | { |
| 163 | id_ = x.id_; |
| 164 | ctr_copies_ = x.ctr_copies_; |
| 165 | ctr_moves_ = x.ctr_moves_; |
| 166 | assign_copies_ = x.assign_copies_; |
| 167 | assign_moves_ = x.assign_moves_+1; |
| 168 | swaps_ = x.swaps_; |
| 169 | return *this; |
| 170 | } |
| 171 | |
| 172 | static void reset_unique_id(unsigned id = 0) |
| 173 | { unique_id_ = id; } |
| 174 | |
| 175 | T* allocate(std::size_t n) |
| 176 | { return (T*)::new char[sizeof(T)*n]; } |
| 177 | |
| 178 | void deallocate(T*p, std::size_t) |
| 179 | { delete[] ((char*)p);} |
| 180 | |
| 181 | friend bool operator==(const propagation_test_allocator &, const propagation_test_allocator &) |
| 182 | { return true; } |
| 183 | |
| 184 | friend bool operator!=(const propagation_test_allocator &, const propagation_test_allocator &) |
| 185 | { return false; } |
| 186 | |
| 187 | void swap(propagation_test_allocator &r) |
| 188 | { |
| 189 | ++this->swaps_; ++r.swaps_; |
| 190 | boost::adl_move_swap(this->id_, r.id_); |
| 191 | boost::adl_move_swap(this->ctr_copies_, r.ctr_copies_); |
| 192 | boost::adl_move_swap(this->ctr_moves_, r.ctr_moves_); |
| 193 | boost::adl_move_swap(this->assign_copies_, r.assign_copies_); |
| 194 | boost::adl_move_swap(this->assign_moves_, r.assign_moves_); |
| 195 | boost::adl_move_swap(this->swaps_, r.swaps_); |
| 196 | } |
| 197 | |
| 198 | friend void swap(propagation_test_allocator &l, propagation_test_allocator &r) |
| 199 | { |
| 200 | l.swap(r); |
| 201 | } |
| 202 | |
| 203 | unsigned int id_; |
| 204 | unsigned int ctr_copies_; |
| 205 | unsigned int ctr_moves_; |
| 206 | unsigned int assign_copies_; |
| 207 | unsigned int assign_moves_; |
| 208 | unsigned int swaps_; |
| 209 | static unsigned unique_id_; |
| 210 | }; |
| 211 | |
| 212 | template< class T |
| 213 | , bool PropagateOnContCopyAssign |
| 214 | , bool PropagateOnContMoveAssign |
| 215 | , bool PropagateOnContSwap |
| 216 | , bool CopyOnPropagateOnContSwap |
| 217 | > |
| 218 | unsigned int propagation_test_allocator< T |
| 219 | , PropagateOnContCopyAssign |
| 220 | , PropagateOnContMoveAssign |
| 221 | , PropagateOnContSwap |
| 222 | , CopyOnPropagateOnContSwap>::unique_id_ = 0; |
| 223 | |
| 224 | |
| 225 | } //namespace test { |
| 226 | } //namespace container { |
| 227 | } //namespace boost { |
| 228 | |
| 229 | #include <boost/container/detail/config_end.hpp> |
| 230 | |
| 231 | #endif //BOOST_CONTAINER_DUMMY_TEST_ALLOCATOR_HPP |
| 232 | |