allocators.h
4.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef ALLOCATORS_H
#define ALLOCATORS_H
#include <type_traits>
#include <utility>
#include "test_macros.h"
#if TEST_STD_VER >= 11
template <class T>
class A1
{
int id_;
public:
explicit A1(int id = 0) TEST_NOEXCEPT : id_(id) {}
typedef T value_type;
int id() const {return id_;}
static bool copy_called;
static bool move_called;
static bool allocate_called;
static std::pair<T*, std::size_t> deallocate_called;
A1(const A1& a) TEST_NOEXCEPT : id_(a.id()) {copy_called = true;}
A1(A1&& a) TEST_NOEXCEPT : id_(a.id()) {move_called = true;}
A1& operator=(const A1& a) TEST_NOEXCEPT { id_ = a.id(); copy_called = true; return *this;}
A1& operator=(A1&& a) TEST_NOEXCEPT { id_ = a.id(); move_called = true; return *this;}
template <class U>
A1(const A1<U>& a) TEST_NOEXCEPT : id_(a.id()) {copy_called = true;}
template <class U>
A1(A1<U>&& a) TEST_NOEXCEPT : id_(a.id()) {move_called = true;}
T* allocate(std::size_t n)
{
allocate_called = true;
return (T*)n;
}
void deallocate(T* p, std::size_t n)
{
deallocate_called = std::pair<T*, std::size_t>(p, n);
}
std::size_t max_size() const {return id_;}
};
template <class T> bool A1<T>::copy_called = false;
template <class T> bool A1<T>::move_called = false;
template <class T> bool A1<T>::allocate_called = false;
template <class T> std::pair<T*, std::size_t> A1<T>::deallocate_called;
template <class T, class U>
inline
bool operator==(const A1<T>& x, const A1<U>& y)
{
return x.id() == y.id();
}
template <class T, class U>
inline
bool operator!=(const A1<T>& x, const A1<U>& y)
{
return !(x == y);
}
template <class T>
class A2
{
int id_;
public:
explicit A2(int id = 0) TEST_NOEXCEPT : id_(id) {}
typedef T value_type;
typedef unsigned size_type;
typedef int difference_type;
typedef std::true_type propagate_on_container_move_assignment;
int id() const {return id_;}
static bool copy_called;
static bool move_called;
static bool allocate_called;
A2(const A2& a) TEST_NOEXCEPT : id_(a.id()) {copy_called = true;}
A2(A2&& a) TEST_NOEXCEPT : id_(a.id()) {move_called = true;}
A2& operator=(const A2& a) TEST_NOEXCEPT { id_ = a.id(); copy_called = true; return *this;}
A2& operator=(A2&& a) TEST_NOEXCEPT { id_ = a.id(); move_called = true; return *this;}
T* allocate(std::size_t, const void* hint)
{
allocate_called = true;
return (T*) const_cast<void *>(hint);
}
};
template <class T> bool A2<T>::copy_called = false;
template <class T> bool A2<T>::move_called = false;
template <class T> bool A2<T>::allocate_called = false;
template <class T, class U>
inline
bool operator==(const A2<T>& x, const A2<U>& y)
{
return x.id() == y.id();
}
template <class T, class U>
inline
bool operator!=(const A2<T>& x, const A2<U>& y)
{
return !(x == y);
}
template <class T>
class A3
{
int id_;
public:
explicit A3(int id = 0) TEST_NOEXCEPT : id_(id) {}
typedef T value_type;
typedef std::true_type propagate_on_container_copy_assignment;
typedef std::true_type propagate_on_container_swap;
int id() const {return id_;}
static bool copy_called;
static bool move_called;
static bool constructed;
static bool destroy_called;
A3(const A3& a) TEST_NOEXCEPT : id_(a.id()) {copy_called = true;}
A3(A3&& a) TEST_NOEXCEPT : id_(a.id()) {move_called = true;}
A3& operator=(const A3& a) TEST_NOEXCEPT { id_ = a.id(); copy_called = true; return *this;}
A3& operator=(A3&& a) TEST_NOEXCEPT { id_ = a.id(); move_called = true; return *this;}
template <class U, class ...Args>
void construct(U* p, Args&& ...args)
{
::new (p) U(std::forward<Args>(args)...);
constructed = true;
}
template <class U>
void destroy(U* p)
{
p->~U();
destroy_called = true;
}
A3 select_on_container_copy_construction() const {return A3(-1);}
};
template <class T> bool A3<T>::copy_called = false;
template <class T> bool A3<T>::move_called = false;
template <class T> bool A3<T>::constructed = false;
template <class T> bool A3<T>::destroy_called = false;
template <class T, class U>
inline
bool operator==(const A3<T>& x, const A3<U>& y)
{
return x.id() == y.id();
}
template <class T, class U>
inline
bool operator!=(const A3<T>& x, const A3<U>& y)
{
return !(x == y);
}
#endif // TEST_STD_VER >= 11
#endif // ALLOCATORS_H