1 //===----------------------------------------------------------------------===// 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 10 // UNSUPPORTED: c++98, c++03 11 12 // <memory> 13 14 // template <class OuterAlloc, class... InnerAllocs> 15 // class scoped_allocator_adaptor 16 17 // template <class T, class... Args> void construct(T* p, Args&&... args); 18 19 #include <scoped_allocator> 20 #include <cassert> 21 #include <string> 22 23 #include "allocators.h" 24 25 struct B 26 { 27 static bool constructed; 28 29 typedef A1<B> allocator_type; 30 31 explicit B(std::allocator_arg_t, const allocator_type& a, int i) 32 { 33 assert(a.id() == 5); 34 assert(i == 6); 35 constructed = true; 36 } 37 }; 38 39 bool B::constructed = false; 40 41 struct C 42 { 43 static bool constructed; 44 45 typedef std::scoped_allocator_adaptor<A2<C>> allocator_type; 46 47 explicit C(std::allocator_arg_t, const allocator_type& a, int i) 48 { 49 assert(a.id() == 7); 50 assert(i == 8); 51 constructed = true; 52 } 53 }; 54 55 bool C::constructed = false; 56 57 struct D 58 { 59 static bool constructed; 60 61 typedef std::scoped_allocator_adaptor<A2<D>> allocator_type; 62 63 explicit D(int i, int j, const allocator_type& a) 64 { 65 assert(i == 1); 66 assert(j == 2); 67 assert(a.id() == 3); 68 constructed = true; 69 } 70 }; 71 72 bool D::constructed = false; 73 74 struct E 75 { 76 static bool constructed; 77 78 typedef std::scoped_allocator_adaptor<A1<E>> allocator_type; 79 80 explicit E(int i, int j, const allocator_type& a) 81 { 82 assert(i == 1); 83 assert(j == 2); 84 assert(a.id() == 50); 85 constructed = true; 86 } 87 }; 88 89 bool E::constructed = false; 90 91 struct F 92 { 93 static bool constructed; 94 95 typedef std::scoped_allocator_adaptor<A2<F>> allocator_type; 96 97 explicit F(int i, int j) 98 { 99 assert(i == 1); 100 assert(j == 2); 101 } 102 103 explicit F(int i, int j, const allocator_type& a) 104 { 105 assert(i == 1); 106 assert(j == 2); 107 assert(a.id() == 50); 108 constructed = true; 109 } 110 }; 111 112 bool F::constructed = false; 113 114 int main() 115 { 116 117 { 118 typedef std::scoped_allocator_adaptor<A1<std::string>> A; 119 A a; 120 char buf[100]; 121 typedef std::string S; 122 S* s = (S*)buf; 123 a.construct(s, 4, 'c'); 124 assert(*s == "cccc"); 125 s->~S(); 126 } 127 128 { 129 typedef std::scoped_allocator_adaptor<A1<B>> A; 130 A a(A1<B>(5)); 131 char buf[100]; 132 typedef B S; 133 S* s = (S*)buf; 134 a.construct(s, 6); 135 assert(S::constructed); 136 s->~S(); 137 } 138 139 { 140 typedef std::scoped_allocator_adaptor<A1<int>, A2<C>> A; 141 A a(A1<int>(5), A2<C>(7)); 142 char buf[100]; 143 typedef C S; 144 S* s = (S*)buf; 145 a.construct(s, 8); 146 assert(S::constructed); 147 s->~S(); 148 } 149 150 { 151 typedef std::scoped_allocator_adaptor<A1<int>, A2<D>> A; 152 A a(A1<int>(5), A2<D>(3)); 153 char buf[100]; 154 typedef D S; 155 S* s = (S*)buf; 156 a.construct(s, 1, 2); 157 assert(S::constructed); 158 s->~S(); 159 } 160 161 { 162 typedef std::scoped_allocator_adaptor<A3<E>, A2<E>> K; 163 typedef std::scoped_allocator_adaptor<K, A1<E>> A; 164 A a(K(), A1<E>(50)); 165 char buf[100]; 166 typedef E S; 167 S* s = (S*)buf; 168 A3<E>::constructed = false; 169 a.construct(s, 1, 2); 170 assert(S::constructed); 171 assert(A3<E>::constructed); 172 s->~S(); 173 } 174 175 { 176 typedef std::scoped_allocator_adaptor<A3<F>, A2<F>> K; 177 typedef std::scoped_allocator_adaptor<K, A1<F>> A; 178 A a(K(), A1<F>(50)); 179 char buf[100]; 180 typedef F S; 181 S* s = (S*)buf; 182 A3<F>::constructed = false; 183 a.construct(s, 1, 2); 184 assert(!S::constructed); 185 assert(A3<F>::constructed); 186 s->~S(); 187 } 188 } 189