Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | #ifndef ALLOCATORS_H |
| 2 | #define ALLOCATORS_H |
| 3 | |
| 4 | #include <type_traits> |
| 5 | #include <utility> |
| 6 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 7 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 8 | |
| 9 | template <class T> |
| 10 | class A1 |
| 11 | { |
| 12 | int id_; |
| 13 | public: |
| 14 | explicit A1(int id = 0) : id_(id) {} |
| 15 | |
| 16 | typedef T value_type; |
| 17 | |
| 18 | int id() const {return id_;} |
| 19 | |
| 20 | static bool copy_called; |
| 21 | static bool move_called; |
| 22 | static bool allocate_called; |
| 23 | static std::pair<T*, std::size_t> deallocate_called; |
| 24 | |
| 25 | A1(const A1& a) : id_(a.id()) {copy_called = true;} |
| 26 | A1(A1&& a) : id_(a.id()) {move_called = true;} |
| 27 | |
| 28 | template <class U> |
| 29 | A1(const A1<U>& a) : id_(a.id()) {copy_called = true;} |
| 30 | template <class U> |
| 31 | A1(A1<U>&& a) : id_(a.id()) {move_called = true;} |
| 32 | |
| 33 | T* allocate(std::size_t n) |
| 34 | { |
| 35 | allocate_called = true; |
| 36 | return (T*)n; |
| 37 | } |
| 38 | |
| 39 | void deallocate(T* p, std::size_t n) |
| 40 | { |
| 41 | deallocate_called = std::pair<T*, std::size_t>(p, n); |
| 42 | } |
| 43 | |
| 44 | std::size_t max_size() const {return id_;} |
| 45 | }; |
| 46 | |
| 47 | template <class T> bool A1<T>::copy_called = false; |
| 48 | template <class T> bool A1<T>::move_called = false; |
| 49 | template <class T> bool A1<T>::allocate_called = false; |
| 50 | template <class T> std::pair<T*, std::size_t> A1<T>::deallocate_called; |
| 51 | |
| 52 | template <class T, class U> |
| 53 | inline |
| 54 | bool operator==(const A1<T>& x, const A1<U>& y) |
| 55 | { |
| 56 | return x.id() == y.id(); |
| 57 | } |
| 58 | |
| 59 | template <class T, class U> |
| 60 | inline |
| 61 | bool operator!=(const A1<T>& x, const A1<U>& y) |
| 62 | { |
| 63 | return !(x == y); |
| 64 | } |
| 65 | |
| 66 | template <class T> |
| 67 | class A2 |
| 68 | { |
| 69 | int id_; |
| 70 | public: |
| 71 | explicit A2(int id = 0) : id_(id) {} |
| 72 | |
| 73 | typedef T value_type; |
| 74 | |
| 75 | typedef unsigned size_type; |
| 76 | typedef int difference_type; |
| 77 | |
| 78 | typedef std::true_type propagate_on_container_move_assignment; |
| 79 | |
| 80 | int id() const {return id_;} |
| 81 | |
| 82 | static bool copy_called; |
| 83 | static bool move_called; |
| 84 | static bool allocate_called; |
| 85 | |
| 86 | A2(const A2& a) : id_(a.id()) {copy_called = true;} |
| 87 | A2(A2&& a) : id_(a.id()) {move_called = true;} |
| 88 | |
| 89 | T* allocate(std::size_t n, const void* hint) |
| 90 | { |
| 91 | allocate_called = true; |
| 92 | return (T*)hint; |
| 93 | } |
| 94 | }; |
| 95 | |
| 96 | template <class T> bool A2<T>::copy_called = false; |
| 97 | template <class T> bool A2<T>::move_called = false; |
| 98 | template <class T> bool A2<T>::allocate_called = false; |
| 99 | |
| 100 | template <class T, class U> |
| 101 | inline |
| 102 | bool operator==(const A2<T>& x, const A2<U>& y) |
| 103 | { |
| 104 | return x.id() == y.id(); |
| 105 | } |
| 106 | |
| 107 | template <class T, class U> |
| 108 | inline |
| 109 | bool operator!=(const A2<T>& x, const A2<U>& y) |
| 110 | { |
| 111 | return !(x == y); |
| 112 | } |
| 113 | |
| 114 | template <class T> |
| 115 | class A3 |
| 116 | { |
| 117 | int id_; |
| 118 | public: |
| 119 | explicit A3(int id = 0) : id_(id) {} |
| 120 | |
| 121 | typedef T value_type; |
| 122 | |
| 123 | typedef std::true_type propagate_on_container_copy_assignment; |
| 124 | typedef std::true_type propagate_on_container_swap; |
| 125 | |
| 126 | int id() const {return id_;} |
| 127 | |
| 128 | static bool copy_called; |
| 129 | static bool move_called; |
| 130 | static bool constructed; |
| 131 | static bool destroy_called; |
| 132 | |
| 133 | A3(const A3& a) : id_(a.id()) {copy_called = true;} |
| 134 | A3(A3&& a) : id_(a.id()) {move_called = true;} |
| 135 | |
| 136 | template <class U, class ...Args> |
| 137 | void construct(U* p, Args&& ...args) |
| 138 | { |
| 139 | ::new (p) U(std::forward<Args>(args)...); |
| 140 | constructed = true; |
| 141 | } |
| 142 | |
| 143 | template <class U> |
| 144 | void destroy(U* p) |
| 145 | { |
| 146 | p->~U(); |
| 147 | destroy_called = true; |
| 148 | } |
| 149 | |
| 150 | A3 select_on_container_copy_construction() const {return A3(-1);} |
| 151 | }; |
| 152 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 153 | template <class T> bool A3<T>::copy_called = false; |
| 154 | template <class T> bool A3<T>::move_called = false; |
| 155 | template <class T> bool A3<T>::constructed = false; |
| 156 | template <class T> bool A3<T>::destroy_called = false; |
| 157 | |
| 158 | template <class T, class U> |
| 159 | inline |
| 160 | bool operator==(const A3<T>& x, const A3<U>& y) |
| 161 | { |
| 162 | return x.id() == y.id(); |
| 163 | } |
| 164 | |
| 165 | template <class T, class U> |
| 166 | inline |
| 167 | bool operator!=(const A3<T>& x, const A3<U>& y) |
| 168 | { |
| 169 | return !(x == y); |
| 170 | } |
| 171 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 172 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 173 | |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 174 | #endif // ALLOCATORS_H |