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