Eric Fiselier | 257fd69 | 2016-05-07 01:04:55 +0000 | [diff] [blame] | 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 | // <experimental/memory_resource> |
| 13 | |
| 14 | // template <class T> class polymorphic_allocator |
| 15 | |
| 16 | // template <class P1, class P2, class U1, class U2> |
| 17 | // void polymorphic_allocator<T>::construct(pair<P1, P2>*, pair<U1, U2> &&) |
| 18 | |
| 19 | #include <experimental/memory_resource> |
| 20 | #include <type_traits> |
| 21 | #include <utility> |
| 22 | #include <tuple> |
| 23 | #include <cassert> |
| 24 | #include <cstdlib> |
| 25 | #include "uses_alloc_types.hpp" |
| 26 | |
| 27 | namespace ex = std::experimental::pmr; |
| 28 | |
| 29 | |
| 30 | |
| 31 | template <class UA1, class UA2, class TT, class UU> |
| 32 | bool doTest(UsesAllocatorType TExpect, UsesAllocatorType UExpect, |
| 33 | std::pair<TT, UU>&& p) |
| 34 | { |
| 35 | using P = std::pair<UA1, UA2>; |
| 36 | TestResource R; |
| 37 | ex::memory_resource * M = &R; |
| 38 | ex::polymorphic_allocator<P> A(M); |
| 39 | P * ptr = A.allocate(2); |
| 40 | P * ptr2 = ptr + 1; |
| 41 | |
| 42 | // UNDER TEST // |
| 43 | A.construct(ptr, std::move(p)); |
| 44 | |
| 45 | A.construct(ptr2, std::piecewise_construct, |
| 46 | std::forward_as_tuple(std::forward<TT>(p.first)), |
| 47 | std::forward_as_tuple(std::forward<UU>(p.second))); |
| 48 | // ------- // |
| 49 | |
| 50 | bool tres = checkConstruct<TT&&>(ptr->first, TExpect, M) && |
| 51 | checkConstructionEquiv(ptr->first, ptr2->first); |
| 52 | |
| 53 | bool ures = checkConstruct<UU&&>(ptr->second, UExpect, M) && |
| 54 | checkConstructionEquiv(ptr->second, ptr2->second); |
| 55 | |
| 56 | A.destroy(ptr); |
| 57 | A.destroy(ptr2); |
| 58 | A.deallocate(ptr, 2); |
| 59 | return tres && ures; |
| 60 | } |
| 61 | |
| 62 | template <class Alloc, class TT, class UU> |
| 63 | void test_pmr_uses_allocator(std::pair<TT, UU>&& p) |
| 64 | { |
| 65 | { |
| 66 | using T = NotUsesAllocator<Alloc, 1>; |
| 67 | using U = NotUsesAllocator<Alloc, 1>; |
| 68 | assert((doTest<T, U>(UA_None, UA_None, std::move(p)))); |
| 69 | } |
| 70 | { |
| 71 | using T = UsesAllocatorV1<Alloc, 1>; |
| 72 | using U = UsesAllocatorV2<Alloc, 1>; |
| 73 | assert((doTest<T, U>(UA_AllocArg, UA_AllocLast, std::move(p)))); |
| 74 | } |
| 75 | { |
| 76 | using T = UsesAllocatorV2<Alloc, 1>; |
| 77 | using U = UsesAllocatorV3<Alloc, 1>; |
| 78 | assert((doTest<T, U>(UA_AllocLast, UA_AllocArg, std::move(p)))); |
| 79 | } |
| 80 | { |
| 81 | using T = UsesAllocatorV3<Alloc, 1>; |
| 82 | using U = NotUsesAllocator<Alloc, 1>; |
| 83 | assert((doTest<T, U>(UA_AllocArg, UA_None, std::move(p)))); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | int main() |
| 88 | { |
| 89 | using ERT = std::experimental::erased_type; |
| 90 | using PMR = ex::memory_resource*; |
| 91 | using PMA = ex::polymorphic_allocator<char>; |
| 92 | { |
| 93 | int x = 42; |
| 94 | int y = 42; |
| 95 | std::pair<int&, int&&> p(x, std::move(y)); |
| 96 | test_pmr_uses_allocator<ERT>(std::move(p)); |
| 97 | test_pmr_uses_allocator<PMR>(std::move(p)); |
| 98 | test_pmr_uses_allocator<PMA>(std::move(p)); |
| 99 | } |
| 100 | { |
| 101 | int x = 42; |
| 102 | int y = 42; |
| 103 | std::pair<int&&, int&> p(std::move(x), y); |
| 104 | test_pmr_uses_allocator<ERT>(std::move(p)); |
| 105 | test_pmr_uses_allocator<PMR>(std::move(p)); |
| 106 | test_pmr_uses_allocator<PMA>(std::move(p)); |
| 107 | } |
| 108 | } |