blob: b7aba12e21fda9d88d47499eb6c2c9a7bd36e98b [file] [log] [blame]
Marshall Clow98760c12014-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 Hinnantbc8d3f92010-05-11 19:42:16 +000010#ifndef ALLOCATORS_H
11#define ALLOCATORS_H
12
13#include <type_traits>
14#include <utility>
15
Marshall Clow7b193f72015-06-03 19:56:43 +000016#include "test_macros.h"
17
Howard Hinnant73d21a42010-09-04 23:28:19 +000018#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000019
20template <class T>
21class A1
22{
23 int id_;
24public:
Marshall Clow7b193f72015-06-03 19:56:43 +000025 explicit A1(int id = 0) TEST_NOEXCEPT : id_(id) {}
Howard Hinnantbc8d3f92010-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 Clow7b193f72015-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;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000038
39 template <class U>
Marshall Clow7b193f72015-06-03 19:56:43 +000040 A1(const A1<U>& a) TEST_NOEXCEPT : id_(a.id()) {copy_called = true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000041 template <class U>
Marshall Clow7b193f72015-06-03 19:56:43 +000042 A1(A1<U>&& a) TEST_NOEXCEPT : id_(a.id()) {move_called = true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000043
44 T* allocate(std::size_t n)
45 {
46 allocate_called = true;
47 return (T*)n;
48 }
49
50 void deallocate(T* p, std::size_t n)
51 {
52 deallocate_called = std::pair<T*, std::size_t>(p, n);
53 }
54
55 std::size_t max_size() const {return id_;}
56};
57
58template <class T> bool A1<T>::copy_called = false;
59template <class T> bool A1<T>::move_called = false;
60template <class T> bool A1<T>::allocate_called = false;
61template <class T> std::pair<T*, std::size_t> A1<T>::deallocate_called;
62
63template <class T, class U>
64inline
65bool operator==(const A1<T>& x, const A1<U>& y)
66{
67 return x.id() == y.id();
68}
69
70template <class T, class U>
71inline
72bool operator!=(const A1<T>& x, const A1<U>& y)
73{
74 return !(x == y);
75}
76
77template <class T>
78class A2
79{
80 int id_;
81public:
Marshall Clow7b193f72015-06-03 19:56:43 +000082 explicit A2(int id = 0) TEST_NOEXCEPT : id_(id) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000083
84 typedef T value_type;
85
86 typedef unsigned size_type;
87 typedef int difference_type;
88
89 typedef std::true_type propagate_on_container_move_assignment;
90
91 int id() const {return id_;}
92
93 static bool copy_called;
94 static bool move_called;
95 static bool allocate_called;
96
Marshall Clow7b193f72015-06-03 19:56:43 +000097 A2(const A2& a) TEST_NOEXCEPT : id_(a.id()) {copy_called = true;}
98 A2(A2&& a) TEST_NOEXCEPT : id_(a.id()) {move_called = true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000099
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700100 T* allocate(std::size_t n, const void* hint)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000101 {
102 allocate_called = true;
103 return (T*)hint;
104 }
105};
106
107template <class T> bool A2<T>::copy_called = false;
108template <class T> bool A2<T>::move_called = false;
109template <class T> bool A2<T>::allocate_called = false;
110
111template <class T, class U>
112inline
113bool operator==(const A2<T>& x, const A2<U>& y)
114{
115 return x.id() == y.id();
116}
117
118template <class T, class U>
119inline
120bool operator!=(const A2<T>& x, const A2<U>& y)
121{
122 return !(x == y);
123}
124
125template <class T>
126class A3
127{
128 int id_;
129public:
Marshall Clow7b193f72015-06-03 19:56:43 +0000130 explicit A3(int id = 0) TEST_NOEXCEPT : id_(id) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000131
132 typedef T value_type;
133
134 typedef std::true_type propagate_on_container_copy_assignment;
135 typedef std::true_type propagate_on_container_swap;
136
137 int id() const {return id_;}
138
139 static bool copy_called;
140 static bool move_called;
141 static bool constructed;
142 static bool destroy_called;
143
Marshall Clow7b193f72015-06-03 19:56:43 +0000144 A3(const A3& a) TEST_NOEXCEPT : id_(a.id()) {copy_called = true;}
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700145 A3(A3&& a) TEST_NOEXCEPT: id_(a.id()) {move_called = true;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000146
147 template <class U, class ...Args>
148 void construct(U* p, Args&& ...args)
149 {
150 ::new (p) U(std::forward<Args>(args)...);
151 constructed = true;
152 }
153
154 template <class U>
155 void destroy(U* p)
156 {
157 p->~U();
158 destroy_called = true;
159 }
160
161 A3 select_on_container_copy_construction() const {return A3(-1);}
162};
163
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000164template <class T> bool A3<T>::copy_called = false;
165template <class T> bool A3<T>::move_called = false;
166template <class T> bool A3<T>::constructed = false;
167template <class T> bool A3<T>::destroy_called = false;
168
169template <class T, class U>
170inline
171bool operator==(const A3<T>& x, const A3<U>& y)
172{
173 return x.id() == y.id();
174}
175
176template <class T, class U>
177inline
178bool operator!=(const A3<T>& x, const A3<U>& y)
179{
180 return !(x == y);
181}
182
Howard Hinnant73d21a42010-09-04 23:28:19 +0000183#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000184
Howard Hinnantc52f43e2010-08-22 00:59:46 +0000185#endif // ALLOCATORS_H