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_FACTORIES_RANGE_IOTA_VIEW_TYPES_H 10 #define TEST_STD_RANGES_RANGE_FACTORIES_RANGE_IOTA_VIEW_TYPES_H 11 12 #include "test_macros.h" 13 14 struct SomeInt { 15 using difference_type = int; 16 17 int value_; value_SomeInt18 constexpr explicit SomeInt(int value = 0) : value_(value) {} 19 20 auto operator<=>(const SomeInt&) const = default; 21 22 friend constexpr SomeInt& operator+=(SomeInt &lhs, const SomeInt& rhs) { 23 lhs.value_ += rhs.value_; return lhs; 24 } 25 friend constexpr SomeInt& operator-=(SomeInt &lhs, const SomeInt& rhs) { 26 lhs.value_ -= rhs.value_; return lhs; 27 } 28 29 friend constexpr SomeInt& operator+=(SomeInt &lhs, difference_type rhs) { 30 lhs.value_ += rhs; return lhs; 31 } 32 friend constexpr SomeInt& operator-=(SomeInt &lhs, difference_type rhs) { 33 lhs.value_ -= rhs; return lhs; 34 } 35 36 friend constexpr SomeInt operator+(SomeInt lhs, SomeInt rhs) { 37 return SomeInt{lhs.value_ + rhs.value_}; 38 } 39 friend constexpr int operator-(SomeInt lhs, SomeInt rhs) { 40 return lhs.value_ - rhs.value_; 41 } 42 43 friend constexpr SomeInt operator+(SomeInt lhs, difference_type rhs) { 44 return SomeInt{lhs.value_ + rhs}; 45 } 46 friend constexpr int operator-(SomeInt lhs, difference_type rhs) { 47 return lhs.value_ - rhs; 48 } 49 50 friend constexpr SomeInt operator+(difference_type lhs, SomeInt rhs) { 51 return SomeInt{lhs + rhs.value_}; 52 } 53 friend constexpr int operator-(difference_type lhs, SomeInt rhs) { 54 return lhs - rhs.value_; 55 } 56 57 constexpr SomeInt& operator++() { ++value_; return *this; } 58 constexpr SomeInt operator++(int) { auto tmp = *this; ++value_; return tmp; } 59 constexpr SomeInt& operator--() { --value_; return *this; } 60 constexpr SomeInt operator--(int) { auto tmp = *this; --value_; return tmp; } 61 }; 62 63 template<class T> 64 struct IntComparableWith { 65 using difference_type = std::iter_difference_t<T>; 66 67 T value_; value_IntComparableWith68 constexpr explicit IntComparableWith(T value = T()) : value_(value) {} 69 70 friend constexpr bool operator==(IntComparableWith lhs, IntComparableWith rhs) { 71 return lhs.value_ == rhs.value_; 72 } 73 friend constexpr bool operator==(IntComparableWith lhs, T rhs) { 74 return lhs.value_ == rhs; 75 } 76 friend constexpr bool operator==(T lhs, IntComparableWith rhs) { 77 return lhs == rhs.value_; 78 } 79 80 friend constexpr IntComparableWith operator+(IntComparableWith lhs, IntComparableWith rhs) { 81 return IntComparableWith{lhs.value_ + rhs.value_}; 82 } 83 friend constexpr difference_type operator-(IntComparableWith lhs, IntComparableWith rhs) { 84 return lhs.value_ - rhs.value_; 85 } 86 87 constexpr IntComparableWith& operator++() { ++value_; return *this; } 88 constexpr IntComparableWith operator++(int) { auto tmp = *this; ++value_; return tmp; } 89 constexpr IntComparableWith operator--() { --value_; return *this; } 90 }; 91 template <class T> 92 IntComparableWith(T) -> IntComparableWith<T>; 93 94 template<class T> 95 struct IntSentinelWith { 96 using difference_type = std::iter_difference_t<T>; 97 98 T value_; value_IntSentinelWith99 constexpr explicit IntSentinelWith(T value = T()) : value_(value) {} 100 101 friend constexpr bool operator==(IntSentinelWith lhs, IntSentinelWith rhs) { 102 return lhs.value_ == rhs.value_; 103 } 104 friend constexpr bool operator==(IntSentinelWith lhs, T rhs) { 105 return lhs.value_ == rhs; 106 } 107 friend constexpr bool operator==(T lhs, IntSentinelWith rhs) { 108 return lhs == rhs.value_; 109 } 110 111 friend constexpr IntSentinelWith operator+(IntSentinelWith lhs, IntSentinelWith rhs) { 112 return IntSentinelWith{lhs.value_ + rhs.value_}; 113 } 114 friend constexpr difference_type operator-(IntSentinelWith lhs, IntSentinelWith rhs) { 115 return lhs.value_ - rhs.value_; 116 } 117 friend constexpr difference_type operator-(IntSentinelWith lhs, T rhs) { 118 return lhs.value_ - rhs; 119 } 120 friend constexpr difference_type operator-(T lhs, IntSentinelWith rhs) { 121 return lhs - rhs.value_; 122 } 123 124 constexpr IntSentinelWith& operator++() { ++value_; return *this; } 125 constexpr IntSentinelWith operator++(int) { auto tmp = *this; ++value_; return tmp; } 126 constexpr IntSentinelWith operator--() { --value_; return *this; } 127 }; 128 template <class T> 129 IntSentinelWith(T) -> IntSentinelWith<T>; 130 131 struct NotIncrementable { 132 using difference_type = int; 133 134 int value_; value_NotIncrementable135 constexpr explicit NotIncrementable(int value = 0) : value_(value) {} 136 137 bool operator==(const NotIncrementable&) const = default; 138 139 friend constexpr NotIncrementable& operator+=(NotIncrementable &lhs, const NotIncrementable& rhs) { 140 lhs.value_ += rhs.value_; return lhs; 141 } 142 friend constexpr NotIncrementable& operator-=(NotIncrementable &lhs, const NotIncrementable& rhs) { 143 lhs.value_ -= rhs.value_; return lhs; 144 } 145 146 friend constexpr NotIncrementable operator+(NotIncrementable lhs, NotIncrementable rhs) { 147 return NotIncrementable{lhs.value_ + rhs.value_}; 148 } 149 friend constexpr int operator-(NotIncrementable lhs, NotIncrementable rhs) { 150 return lhs.value_ - rhs.value_; 151 } 152 153 constexpr NotIncrementable& operator++() { ++value_; return *this; } 154 constexpr void operator++(int) { ++value_; } 155 constexpr NotIncrementable& operator--() { --value_; return *this; } 156 }; 157 static_assert(!std::incrementable<NotIncrementable>); 158 159 struct NotDecrementable { 160 using difference_type = int; 161 162 int value_; value_NotDecrementable163 constexpr explicit NotDecrementable(int value = 0) : value_(value) {} 164 165 bool operator==(const NotDecrementable&) const = default; 166 167 friend constexpr NotDecrementable& operator+=(NotDecrementable &lhs, const NotDecrementable& rhs) { 168 lhs.value_ += rhs.value_; return lhs; 169 } 170 friend constexpr NotDecrementable& operator-=(NotDecrementable &lhs, const NotDecrementable& rhs) { 171 lhs.value_ -= rhs.value_; return lhs; 172 } 173 174 friend constexpr NotDecrementable operator+(NotDecrementable lhs, NotDecrementable rhs) { 175 return NotDecrementable{lhs.value_ + rhs.value_}; 176 } 177 friend constexpr int operator-(NotDecrementable lhs, NotDecrementable rhs) { 178 return lhs.value_ - rhs.value_; 179 } 180 181 constexpr NotDecrementable& operator++() { ++value_; return *this; } 182 constexpr void operator++(int) { ++value_; } 183 }; 184 185 enum CtorKind { DefaultTo42, ValueCtor }; 186 187 template<CtorKind CK> 188 struct Int42 { 189 using difference_type = int; 190 191 int value_; Int42Int42192 constexpr explicit Int42(int value) : value_(value) {} Int42Int42193 constexpr explicit Int42() requires (CK == DefaultTo42) 194 : value_(42) {} 195 196 bool operator==(const Int42&) const = default; 197 198 friend constexpr Int42& operator+=(Int42 &lhs, const Int42& rhs) { 199 lhs.value_ += rhs.value_; return lhs; 200 } 201 friend constexpr Int42& operator-=(Int42 &lhs, const Int42& rhs) { 202 lhs.value_ -= rhs.value_; return lhs; 203 } 204 205 friend constexpr Int42 operator+(Int42 lhs, Int42 rhs) { 206 return Int42{lhs.value_ + rhs.value_}; 207 } 208 friend constexpr int operator-(Int42 lhs, Int42 rhs) { 209 return lhs.value_ - rhs.value_; 210 } 211 212 constexpr Int42& operator++() { ++value_; return *this; } 213 constexpr void operator++(int) { ++value_; } 214 }; 215 216 #endif // TEST_STD_RANGES_RANGE_FACTORIES_RANGE_IOTA_VIEW_TYPES_H 217