blob: eae51909ed69780a01d94bfba033d446368e739e [file] [log] [blame]
Marshall Clow354d39c2014-01-16 16:58:45 +00001//===----------------------------------------------------------------------===//
2//
Chandler Carruth57b08b02019-01-19 10:56:40 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Marshall Clow354d39c2014-01-16 16:58:45 +00006//
7//===----------------------------------------------------------------------===//
8
Howard Hinnant3e519522010-05-11 19:42:16 +00009#ifndef ALLOCATORS_H
10#define ALLOCATORS_H
11
12#include <type_traits>
13#include <utility>
14
Marshall Clowcbf166a2015-06-03 19:56:43 +000015#include "test_macros.h"
16
Eric Fiselier9adebed2017-04-19 01:02:49 +000017#if TEST_STD_VER >= 11
Howard Hinnant3e519522010-05-11 19:42:16 +000018
19template <class T>
20class A1
21{
22 int id_;
23public:
Marshall Clowcbf166a2015-06-03 19:56:43 +000024 explicit A1(int id = 0) TEST_NOEXCEPT : id_(id) {}
Howard Hinnant3e519522010-05-11 19:42:16 +000025
26 typedef T value_type;
27
28 int id() const {return id_;}
29
30 static bool copy_called;
31 static bool move_called;
32 static bool allocate_called;
33 static std::pair<T*, std::size_t> deallocate_called;
34
Marshall Clowcbf166a2015-06-03 19:56:43 +000035 A1(const A1& a) TEST_NOEXCEPT : id_(a.id()) {copy_called = true;}
36 A1(A1&& a) TEST_NOEXCEPT : id_(a.id()) {move_called = true;}
Marshall Clow05e9cb52015-10-06 20:30:56 +000037 A1& operator=(const A1& a) TEST_NOEXCEPT { id_ = a.id(); copy_called = true; return *this;}
38 A1& operator=(A1&& a) TEST_NOEXCEPT { id_ = a.id(); move_called = true; return *this;}
Howard Hinnant3e519522010-05-11 19:42:16 +000039
40 template <class U>
Marshall Clowcbf166a2015-06-03 19:56:43 +000041 A1(const A1<U>& a) TEST_NOEXCEPT : id_(a.id()) {copy_called = true;}
Howard Hinnant3e519522010-05-11 19:42:16 +000042 template <class U>
Marshall Clowcbf166a2015-06-03 19:56:43 +000043 A1(A1<U>&& a) TEST_NOEXCEPT : id_(a.id()) {move_called = true;}
Howard Hinnant3e519522010-05-11 19:42:16 +000044
45 T* allocate(std::size_t n)
46 {
47 allocate_called = true;
48 return (T*)n;
49 }
50
51 void deallocate(T* p, std::size_t n)
52 {
53 deallocate_called = std::pair<T*, std::size_t>(p, n);
54 }
55
56 std::size_t max_size() const {return id_;}
57};
58
59template <class T> bool A1<T>::copy_called = false;
60template <class T> bool A1<T>::move_called = false;
61template <class T> bool A1<T>::allocate_called = false;
62template <class T> std::pair<T*, std::size_t> A1<T>::deallocate_called;
63
64template <class T, class U>
65inline
66bool operator==(const A1<T>& x, const A1<U>& y)
67{
68 return x.id() == y.id();
69}
70
71template <class T, class U>
72inline
73bool operator!=(const A1<T>& x, const A1<U>& y)
74{
75 return !(x == y);
76}
77
78template <class T>
79class A2
80{
81 int id_;
82public:
Marshall Clowcbf166a2015-06-03 19:56:43 +000083 explicit A2(int id = 0) TEST_NOEXCEPT : id_(id) {}
Howard Hinnant3e519522010-05-11 19:42:16 +000084
85 typedef T value_type;
86
87 typedef unsigned size_type;
88 typedef int difference_type;
89
90 typedef std::true_type propagate_on_container_move_assignment;
91
92 int id() const {return id_;}
93
94 static bool copy_called;
95 static bool move_called;
96 static bool allocate_called;
97
Marshall Clowcbf166a2015-06-03 19:56:43 +000098 A2(const A2& a) TEST_NOEXCEPT : id_(a.id()) {copy_called = true;}
99 A2(A2&& a) TEST_NOEXCEPT : id_(a.id()) {move_called = true;}
Marshall Clow05e9cb52015-10-06 20:30:56 +0000100 A2& operator=(const A2& a) TEST_NOEXCEPT { id_ = a.id(); copy_called = true; return *this;}
101 A2& operator=(A2&& a) TEST_NOEXCEPT { id_ = a.id(); move_called = true; return *this;}
Howard Hinnant3e519522010-05-11 19:42:16 +0000102
Eric Fiselier7626f772016-04-28 03:17:56 +0000103 T* allocate(std::size_t, const void* hint)
Howard Hinnant3e519522010-05-11 19:42:16 +0000104 {
105 allocate_called = true;
Marshall Clow8b086e38782017-06-14 20:00:36 +0000106 return (T*) const_cast<void *>(hint);
Howard Hinnant3e519522010-05-11 19:42:16 +0000107 }
108};
109
110template <class T> bool A2<T>::copy_called = false;
111template <class T> bool A2<T>::move_called = false;
112template <class T> bool A2<T>::allocate_called = false;
113
114template <class T, class U>
115inline
116bool operator==(const A2<T>& x, const A2<U>& y)
117{
118 return x.id() == y.id();
119}
120
121template <class T, class U>
122inline
123bool operator!=(const A2<T>& x, const A2<U>& y)
124{
125 return !(x == y);
126}
127
128template <class T>
129class A3
130{
131 int id_;
132public:
Marshall Clowcbf166a2015-06-03 19:56:43 +0000133 explicit A3(int id = 0) TEST_NOEXCEPT : id_(id) {}
Howard Hinnant3e519522010-05-11 19:42:16 +0000134
135 typedef T value_type;
136
137 typedef std::true_type propagate_on_container_copy_assignment;
138 typedef std::true_type propagate_on_container_swap;
139
140 int id() const {return id_;}
141
142 static bool copy_called;
143 static bool move_called;
144 static bool constructed;
145 static bool destroy_called;
146
Marshall Clowcbf166a2015-06-03 19:56:43 +0000147 A3(const A3& a) TEST_NOEXCEPT : id_(a.id()) {copy_called = true;}
Marshall Clow05e9cb52015-10-06 20:30:56 +0000148 A3(A3&& a) TEST_NOEXCEPT : id_(a.id()) {move_called = true;}
149 A3& operator=(const A3& a) TEST_NOEXCEPT { id_ = a.id(); copy_called = true; return *this;}
150 A3& operator=(A3&& a) TEST_NOEXCEPT { id_ = a.id(); move_called = true; return *this;}
Howard Hinnant3e519522010-05-11 19:42:16 +0000151
152 template <class U, class ...Args>
153 void construct(U* p, Args&& ...args)
154 {
155 ::new (p) U(std::forward<Args>(args)...);
156 constructed = true;
157 }
158
159 template <class U>
160 void destroy(U* p)
161 {
162 p->~U();
163 destroy_called = true;
164 }
165
166 A3 select_on_container_copy_construction() const {return A3(-1);}
167};
168
Howard Hinnant3e519522010-05-11 19:42:16 +0000169template <class T> bool A3<T>::copy_called = false;
170template <class T> bool A3<T>::move_called = false;
171template <class T> bool A3<T>::constructed = false;
172template <class T> bool A3<T>::destroy_called = false;
173
174template <class T, class U>
175inline
176bool operator==(const A3<T>& x, const A3<U>& y)
177{
178 return x.id() == y.id();
179}
180
181template <class T, class U>
182inline
183bool operator!=(const A3<T>& x, const A3<U>& y)
184{
185 return !(x == y);
186}
187
Eric Fiselier9adebed2017-04-19 01:02:49 +0000188#endif // TEST_STD_VER >= 11
Howard Hinnant3e519522010-05-11 19:42:16 +0000189
Howard Hinnant94b2dd02010-08-22 00:59:46 +0000190#endif // ALLOCATORS_H