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