1 //===----------------------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef TEST_STD_RANGES_RANGE_ADAPTORS_RANGE_ELEMENTS_TYPES_H 10 #define TEST_STD_RANGES_RANGE_ADAPTORS_RANGE_ELEMENTS_TYPES_H 11 12 #include <array> 13 #include <functional> 14 #include <ranges> 15 #include <tuple> 16 17 #include "test_macros.h" 18 #include "test_iterators.h" 19 #include "test_range.h" 20 21 template <class T> 22 struct BufferView : std::ranges::view_base { 23 T* buffer_; 24 std::size_t size_; 25 26 template <std::size_t N> BufferViewBufferView27 constexpr BufferView(T (&b)[N]) : buffer_(b), size_(N) {} 28 29 template <std::size_t N> BufferViewBufferView30 constexpr BufferView(std::array<T, N>& arr) : buffer_(arr.data()), size_(N) {} 31 }; 32 33 using TupleBufferView = BufferView<std::tuple<int>>; 34 35 template <bool Simple> 36 struct Common : TupleBufferView { 37 using TupleBufferView::TupleBufferView; 38 beginCommon39 constexpr std::tuple<int>* begin() 40 requires(!Simple) 41 { 42 return buffer_; 43 } beginCommon44 constexpr const std::tuple<int>* begin() const { return buffer_; } endCommon45 constexpr std::tuple<int>* end() 46 requires(!Simple) 47 { 48 return buffer_ + size_; 49 } endCommon50 constexpr const std::tuple<int>* end() const { return buffer_ + size_; } 51 }; 52 using SimpleCommon = Common<true>; 53 using NonSimpleCommon = Common<false>; 54 55 using SimpleCommonRandomAccessSized = SimpleCommon; 56 using NonSimpleCommonRandomAccessSized = NonSimpleCommon; 57 58 static_assert(std::ranges::common_range<Common<true>>); 59 static_assert(std::ranges::random_access_range<SimpleCommon>); 60 static_assert(std::ranges::sized_range<SimpleCommon>); 61 static_assert(simple_view<SimpleCommon>); 62 static_assert(!simple_view<NonSimpleCommon>); 63 64 template <bool Simple> 65 struct NonCommon : TupleBufferView { 66 using TupleBufferView::TupleBufferView; beginNonCommon67 constexpr std::tuple<int>* begin() 68 requires(!Simple) 69 { 70 return buffer_; 71 } beginNonCommon72 constexpr const std::tuple<int>* begin() const { return buffer_; } endNonCommon73 constexpr sentinel_wrapper<std::tuple<int>*> end() 74 requires(!Simple) 75 { 76 return sentinel_wrapper<std::tuple<int>*>(buffer_ + size_); 77 } endNonCommon78 constexpr sentinel_wrapper<const std::tuple<int>*> end() const { 79 return sentinel_wrapper<const std::tuple<int>*>(buffer_ + size_); 80 } 81 }; 82 83 using SimpleNonCommon = NonCommon<true>; 84 using NonSimpleNonCommon = NonCommon<false>; 85 86 static_assert(!std::ranges::common_range<SimpleNonCommon>); 87 static_assert(std::ranges::random_access_range<SimpleNonCommon>); 88 static_assert(!std::ranges::sized_range<SimpleNonCommon>); 89 static_assert(simple_view<SimpleNonCommon>); 90 static_assert(!simple_view<NonSimpleNonCommon>); 91 92 template <class Derived> 93 struct IterBase { 94 using iterator_concept = std::random_access_iterator_tag; 95 using value_type = std::tuple<int>; 96 using difference_type = std::intptr_t; 97 98 constexpr std::tuple<int> operator*() const { return std::tuple<int>(5); } 99 100 constexpr Derived& operator++() { return *this; } 101 constexpr void operator++(int) {} 102 103 friend constexpr bool operator==(const IterBase&, const IterBase&) = default; 104 }; 105 106 #endif // TEST_STD_RANGES_RANGE_ADAPTORS_RANGE_ELEMENTS_TYPES_H 107