blob: 00e9a0c13ce72421e8ac4b556612810d31e612e1 [file] [log] [blame]
Marshall Clow354d39c2014-01-16 16:58:45 +00001//===----------------------------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Howard Hinnant3e519522010-05-11 19:42:16 +000010#ifndef ALLOCATORS_H
11#define ALLOCATORS_H
12
13#include <type_traits>
14#include <utility>
15
Marshall Clowcbf166a2015-06-03 19:56:43 +000016#include "test_macros.h"
17
Eric Fiselier9adebed2017-04-19 01:02:49 +000018#if TEST_STD_VER >= 11
Howard Hinnant3e519522010-05-11 19:42:16 +000019
20template <class T>
21class A1
22{
23 int id_;
24public:
Marshall Clowcbf166a2015-06-03 19:56:43 +000025 explicit A1(int id = 0) TEST_NOEXCEPT : id_(id) {}
Howard Hinnant3e519522010-05-11 19:42:16 +000026
27 typedef T value_type;
28
29 int id() const {return id_;}
30
31 static bool copy_called;
32 static bool move_called;
33 static bool allocate_called;
34 static std::pair<T*, std::size_t> deallocate_called;
35
Marshall Clowcbf166a2015-06-03 19:56:43 +000036 A1(const A1& a) TEST_NOEXCEPT : id_(a.id()) {copy_called = true;}
37 A1(A1&& a) TEST_NOEXCEPT : id_(a.id()) {move_called = true;}
Marshall Clow05e9cb52015-10-06 20:30:56 +000038 A1& operator=(const A1& a) TEST_NOEXCEPT { id_ = a.id(); copy_called = true; return *this;}
39 A1& operator=(A1&& a) TEST_NOEXCEPT { id_ = a.id(); move_called = true; return *this;}
Howard Hinnant3e519522010-05-11 19:42:16 +000040
41 template <class U>
Marshall Clowcbf166a2015-06-03 19:56:43 +000042 A1(const A1<U>& a) TEST_NOEXCEPT : id_(a.id()) {copy_called = true;}
Howard Hinnant3e519522010-05-11 19:42:16 +000043 template <class U>
Marshall Clowcbf166a2015-06-03 19:56:43 +000044 A1(A1<U>&& a) TEST_NOEXCEPT : id_(a.id()) {move_called = true;}
Howard Hinnant3e519522010-05-11 19:42:16 +000045
46 T* allocate(std::size_t n)
47 {
48 allocate_called = true;
49 return (T*)n;
50 }
51
52 void deallocate(T* p, std::size_t n)
53 {
54 deallocate_called = std::pair<T*, std::size_t>(p, n);
55 }
56
57 std::size_t max_size() const {return id_;}
58};
59
60template <class T> bool A1<T>::copy_called = false;
61template <class T> bool A1<T>::move_called = false;
62template <class T> bool A1<T>::allocate_called = false;
63template <class T> std::pair<T*, std::size_t> A1<T>::deallocate_called;
64
65template <class T, class U>
66inline
67bool operator==(const A1<T>& x, const A1<U>& y)
68{
69 return x.id() == y.id();
70}
71
72template <class T, class U>
73inline
74bool operator!=(const A1<T>& x, const A1<U>& y)
75{
76 return !(x == y);
77}
78
79template <class T>
80class A2
81{
82 int id_;
83public:
Marshall Clowcbf166a2015-06-03 19:56:43 +000084 explicit A2(int id = 0) TEST_NOEXCEPT : id_(id) {}
Howard Hinnant3e519522010-05-11 19:42:16 +000085
86 typedef T value_type;
87
88 typedef unsigned size_type;
89 typedef int difference_type;
90
91 typedef std::true_type propagate_on_container_move_assignment;
92
93 int id() const {return id_;}
94
95 static bool copy_called;
96 static bool move_called;
97 static bool allocate_called;
98
Marshall Clowcbf166a2015-06-03 19:56:43 +000099 A2(const A2& a) TEST_NOEXCEPT : id_(a.id()) {copy_called = true;}
100 A2(A2&& a) TEST_NOEXCEPT : id_(a.id()) {move_called = true;}
Marshall Clow05e9cb52015-10-06 20:30:56 +0000101 A2& operator=(const A2& a) TEST_NOEXCEPT { id_ = a.id(); copy_called = true; return *this;}
102 A2& operator=(A2&& a) TEST_NOEXCEPT { id_ = a.id(); move_called = true; return *this;}
Howard Hinnant3e519522010-05-11 19:42:16 +0000103
Eric Fiselier7626f772016-04-28 03:17:56 +0000104 T* allocate(std::size_t, const void* hint)
Howard Hinnant3e519522010-05-11 19:42:16 +0000105 {
106 allocate_called = true;
Marshall Clow8b086e38782017-06-14 20:00:36 +0000107 return (T*) const_cast<void *>(hint);
Howard Hinnant3e519522010-05-11 19:42:16 +0000108 }
109};
110
111template <class T> bool A2<T>::copy_called = false;
112template <class T> bool A2<T>::move_called = false;
113template <class T> bool A2<T>::allocate_called = false;
114
115template <class T, class U>
116inline
117bool operator==(const A2<T>& x, const A2<U>& y)
118{
119 return x.id() == y.id();
120}
121
122template <class T, class U>
123inline
124bool operator!=(const A2<T>& x, const A2<U>& y)
125{
126 return !(x == y);
127}
128
129template <class T>
130class A3
131{
132 int id_;
133public:
Marshall Clowcbf166a2015-06-03 19:56:43 +0000134 explicit A3(int id = 0) TEST_NOEXCEPT : id_(id) {}
Howard Hinnant3e519522010-05-11 19:42:16 +0000135
136 typedef T value_type;
137
138 typedef std::true_type propagate_on_container_copy_assignment;
139 typedef std::true_type propagate_on_container_swap;
140
141 int id() const {return id_;}
142
143 static bool copy_called;
144 static bool move_called;
145 static bool constructed;
146 static bool destroy_called;
147
Marshall Clowcbf166a2015-06-03 19:56:43 +0000148 A3(const A3& a) TEST_NOEXCEPT : id_(a.id()) {copy_called = true;}
Marshall Clow05e9cb52015-10-06 20:30:56 +0000149 A3(A3&& a) TEST_NOEXCEPT : id_(a.id()) {move_called = true;}
150 A3& operator=(const A3& a) TEST_NOEXCEPT { id_ = a.id(); copy_called = true; return *this;}
151 A3& operator=(A3&& a) TEST_NOEXCEPT { id_ = a.id(); move_called = true; return *this;}
Howard Hinnant3e519522010-05-11 19:42:16 +0000152
153 template <class U, class ...Args>
154 void construct(U* p, Args&& ...args)
155 {
156 ::new (p) U(std::forward<Args>(args)...);
157 constructed = true;
158 }
159
160 template <class U>
161 void destroy(U* p)
162 {
163 p->~U();
164 destroy_called = true;
165 }
166
167 A3 select_on_container_copy_construction() const {return A3(-1);}
168};
169
Howard Hinnant3e519522010-05-11 19:42:16 +0000170template <class T> bool A3<T>::copy_called = false;
171template <class T> bool A3<T>::move_called = false;
172template <class T> bool A3<T>::constructed = false;
173template <class T> bool A3<T>::destroy_called = false;
174
175template <class T, class U>
176inline
177bool operator==(const A3<T>& x, const A3<U>& y)
178{
179 return x.id() == y.id();
180}
181
182template <class T, class U>
183inline
184bool operator!=(const A3<T>& x, const A3<U>& y)
185{
186 return !(x == y);
187}
188
Eric Fiselier9adebed2017-04-19 01:02:49 +0000189#endif // TEST_STD_VER >= 11
Howard Hinnant3e519522010-05-11 19:42:16 +0000190
Howard Hinnant94b2dd02010-08-22 00:59:46 +0000191#endif // ALLOCATORS_H