| 1 |
// Range v3 library |
| 2 |
// |
| 3 |
// Copyright Eric Niebler 2018-present |
| 4 |
// |
| 5 |
// Use, modification and distribution is subject to the |
| 6 |
// Boost Software License, Version 1.0. (See accompanying |
| 7 |
// file LICENSE_1_0.txt or copy at |
| 8 |
// http://www.boost.org/LICENSE_1_0.txt) |
| 9 |
// |
| 10 |
// Project home: https://github.com/ericniebler/range-v3 |
| 11 |
// |
| 12 |
|
| 13 |
#ifndef RANGES_V3_STD_ITERATOR |
| 14 |
#define RANGES_V3_STD_ITERATOR |
| 15 |
|
| 16 |
#if defined(__GNUC__) |
| 17 |
#include_next <iterator> |
| 18 |
#elif defined(_MSC_VER) |
| 19 |
#include <../include/iterator> // HACKHACK |
| 20 |
#else |
| 21 |
#error "Cannot use range-v3 STL deep integration on this platform." |
| 22 |
#endif |
| 23 |
|
| 24 |
#if RANGES_DEEP_STL_INTEGRATION |
| 25 |
|
| 26 |
#include <range/v3/detail/config.hpp> |
| 27 |
#include <std/detail/associated_types.hpp> |
| 28 |
|
| 29 |
#include <range/v3/detail/prologue.hpp> |
| 30 |
|
| 31 |
namespace ranges |
| 32 |
{ |
| 33 |
template<typename I> |
| 34 |
struct incrementable_traits; |
| 35 |
|
| 36 |
template<typename I> |
| 37 |
struct indirectly_readable_traits; |
| 38 |
|
| 39 |
/// \cond |
| 40 |
namespace detail |
| 41 |
{ |
| 42 |
template<typename I> |
| 43 |
typename incrementable_traits<I>::difference_type cpp17_difference_type_(int); |
| 44 |
template<typename I> |
| 45 |
void cpp17_difference_type_(long); |
| 46 |
|
| 47 |
template<typename I> |
| 48 |
typename I::pointer cpp17_pointer_type_(int); |
| 49 |
template<typename I> |
| 50 |
auto cpp17_pointer_type_(long, I *pi = nullptr) -> decltype(pi->operator->()); |
| 51 |
template<typename I> |
| 52 |
void cpp17_pointer_type_(...); |
| 53 |
|
| 54 |
template<typename I> |
| 55 |
typename I::reference cpp17_reference_type_(int); |
| 56 |
template<typename I> |
| 57 |
auto cpp17_reference_type_(long, I *pi = nullptr) -> decltype(**pi); |
| 58 |
|
| 59 |
template<typename I> |
| 60 |
auto cpp17_iterator_category_4_(long) |
| 61 |
{ |
| 62 |
return std::bidirectional_iterator_tag{}; |
| 63 |
} |
| 64 |
// Satisfies Cpp17RandomAccessIterator? |
| 65 |
template<typename I> |
| 66 |
auto cpp17_iterator_category_4_( |
| 67 |
int, |
| 68 |
I *pi = nullptr, |
| 69 |
typename incrementable_traits<I>::difference_type d = 0, |
| 70 |
always_< |
| 71 |
void, |
| 72 |
int[RANGES_IS_SAME(decltype(*pi += d), I &)], |
| 73 |
int[RANGES_IS_SAME(decltype(*pi -= d), I &)], |
| 74 |
int[RANGES_IS_SAME(decltype(*pi + d), I)], |
| 75 |
int[RANGES_IS_SAME(decltype(*pi - d), I)], |
| 76 |
int[RANGES_IS_SAME(decltype(d + *pi), I)], |
| 77 |
int[RANGES_IS_SAME(decltype(*pi - *pi), decltype(d))], |
| 78 |
int[RANGES_IS_SAME(decltype((*pi)[d]), decltype(**pi))], |
| 79 |
decltype(*pi < *pi ? true : false), |
| 80 |
decltype(*pi > *pi ? true : false), |
| 81 |
decltype(*pi <= *pi ? true : false), |
| 82 |
decltype(*pi >= *pi ? true : false) |
| 83 |
> * = nullptr) |
| 84 |
{ |
| 85 |
return std::random_access_iterator_tag{}; |
| 86 |
} |
| 87 |
|
| 88 |
template<typename I> |
| 89 |
auto cpp17_iterator_category_3_(long) |
| 90 |
{ |
| 91 |
return std::forward_iterator_tag{}; |
| 92 |
} |
| 93 |
// Satisfies Cpp17BidirectionalIterator? |
| 94 |
template<typename I> |
| 95 |
auto cpp17_iterator_category_3_( |
| 96 |
int, |
| 97 |
I *pi = nullptr, |
| 98 |
void (*fn)(I const &) = nullptr, |
| 99 |
always_< |
| 100 |
void, |
| 101 |
decltype(fn((*pi)--)), // i-- convertible to I const & |
| 102 |
int[RANGES_IS_SAME(decltype(--*pi), I &)], // --i has type I & |
| 103 |
// *i has the same type as *i-- |
| 104 |
int[RANGES_IS_SAME(decltype(**pi), decltype(*(*pi)--))] |
| 105 |
> * = nullptr) |
| 106 |
{ |
| 107 |
return cpp17_iterator_category_4_<I>(0); |
| 108 |
} |
| 109 |
|
| 110 |
template<typename I> |
| 111 |
auto cpp17_iterator_category_2_(long) |
| 112 |
{ |
| 113 |
return std::input_iterator_tag{}; |
| 114 |
} |
| 115 |
// Satisfies Cpp17ForwardIterator? |
| 116 |
template<typename I> |
| 117 |
auto cpp17_iterator_category_2_( |
| 118 |
int, |
| 119 |
I *pi = nullptr, |
| 120 |
void (*fn)(I const &) = nullptr, |
| 121 |
typename indirectly_readable_traits<I>::value_type *pv = nullptr, |
| 122 |
typename indirectly_readable_traits<I>::value_type const *pcv = nullptr, |
| 123 |
always_< |
| 124 |
void, |
| 125 |
decltype(I{}), // Default constructible |
| 126 |
decltype(fn((*pi)++)), // i++ convertible to I const & |
| 127 |
// *i has the same type as *i++ |
| 128 |
int[RANGES_IS_SAME(decltype(**pi), decltype(*(*pi)++))], |
| 129 |
// *i is a real reference to value_type |
| 130 |
#ifdef RANGES_WORKAROUND_MSVC_793042 |
| 131 |
enable_if_t<RANGES_IS_SAME(decltype(**pi), decltype(*pv)) || |
| 132 |
RANGES_IS_SAME(decltype(**pi), decltype(*pcv)) || |
| 133 |
RANGES_IS_SAME(decltype(**pi), typename indirectly_readable_traits<I>::value_type &&) || |
| 134 |
RANGES_IS_SAME(decltype(**pi), typename indirectly_readable_traits<I>::value_type const &&)> |
| 135 |
#else // ^^^ workaround / no workaround vvv |
| 136 |
int[RANGES_IS_SAME(decltype(**pi), decltype(*pv)) || |
| 137 |
RANGES_IS_SAME(decltype(**pi), decltype(*pcv)) || |
| 138 |
RANGES_IS_SAME(decltype(**pi), typename indirectly_readable_traits<I>::value_type &&) || |
| 139 |
RANGES_IS_SAME(decltype(**pi), typename indirectly_readable_traits<I>::value_type const &&)] |
| 140 |
#endif // RANGES_WORKAROUND_MSVC_793042 |
| 141 |
> * = nullptr) |
| 142 |
{ |
| 143 |
return cpp17_iterator_category_3_<I>(0); |
| 144 |
} |
| 145 |
|
| 146 |
template<typename I> |
| 147 |
using cpp17_readable_iterator_category_t = |
| 148 |
decltype(detail::cpp17_iterator_category_2_<I>(0)); |
| 149 |
|
| 150 |
template<typename I> |
| 151 |
auto cpp17_iterator_category_(long) |
| 152 |
{ |
| 153 |
return cpp17_iterator_category_2_<I>(0); |
| 154 |
} |
| 155 |
// Explicitly declares its category? |
| 156 |
template<typename I> |
| 157 |
typename I::iterator_category cpp17_iterator_category_(int) |
| 158 |
{ |
| 159 |
return {}; |
| 160 |
} |
| 161 |
|
| 162 |
template<typename I> |
| 163 |
auto std_iterator_traits_impl_2_(long) |
| 164 |
{ |
| 165 |
return std_output_iterator_traits< |
| 166 |
decltype(detail::cpp17_difference_type_<I>(0))>{}; |
| 167 |
} |
| 168 |
// Satisfies Cpp17InputIterator? |
| 169 |
template<typename I> |
| 170 |
auto std_iterator_traits_impl_2_( |
| 171 |
int, |
| 172 |
I *pi = nullptr, |
| 173 |
typename incrementable_traits<I>::difference_type d = 0, |
| 174 |
typename indirectly_readable_traits<I>::value_type const *pcv = nullptr, |
| 175 |
always_< |
| 176 |
void, |
| 177 |
int[decltype(d)(-1) < decltype(d)(0)], // signed difference type |
| 178 |
decltype(decltype(*pcv)(**pi)), // sensible reference/value type |
| 179 |
decltype(decltype(*pcv)(*(*pi)++)), // sensible post-increment result |
| 180 |
decltype(*pi == *pi ? true : false), // equality comparable |
| 181 |
decltype(*pi != *pi ? true : false) // " " |
| 182 |
> * = nullptr) |
| 183 |
{ |
| 184 |
using D = typename incrementable_traits<I>::difference_type; |
| 185 |
struct yes_traits |
| 186 |
{ |
| 187 |
using difference_type = D; |
| 188 |
using value_type = typename indirectly_readable_traits<I>::value_type; |
| 189 |
using reference = decltype(cpp17_reference_type_<I>(0)); |
| 190 |
using pointer = decltype(cpp17_pointer_type_<I>(0)); |
| 191 |
using iterator_category = decltype(cpp17_iterator_category_<I>(0)); |
| 192 |
}; |
| 193 |
struct no_traits |
| 194 |
{}; |
| 195 |
return conditional_t<is_integral_<D>(0), yes_traits, no_traits>{}; |
| 196 |
} |
| 197 |
|
| 198 |
template<typename I> |
| 199 |
nil_ std_iterator_traits_impl_(long) |
| 200 |
{ |
| 201 |
return {}; |
| 202 |
} |
| 203 |
// Satisfies Cpp17Iterator? |
| 204 |
template<typename I> |
| 205 |
auto std_iterator_traits_impl_( |
| 206 |
int, |
| 207 |
I *pi = nullptr, |
| 208 |
void (*nv)(...) = nullptr, |
| 209 |
always_< |
| 210 |
void, |
| 211 |
decltype(nv(**pi)), |
| 212 |
int[RANGES_IS_SAME(decltype(++*pi), I &)], |
| 213 |
decltype(nv(*(*pi)++)) |
| 214 |
> * = nullptr) |
| 215 |
{ |
| 216 |
return std_iterator_traits_impl_2_<I>(0); |
| 217 |
} |
| 218 |
|
| 219 |
template<typename T> |
| 220 |
constexpr bool has_iterator_typedefs_impl_( |
| 221 |
int, |
| 222 |
always_< |
| 223 |
void, |
| 224 |
typename T::difference_type, |
| 225 |
typename T::value_type, |
| 226 |
typename T::pointer, |
| 227 |
typename T::reference, |
| 228 |
typename T::iterator_category |
| 229 |
> * = nullptr) |
| 230 |
{ |
| 231 |
return true; |
| 232 |
} |
| 233 |
template<typename T> |
| 234 |
constexpr bool has_iterator_typedefs_impl_(long) |
| 235 |
{ |
| 236 |
return false; |
| 237 |
} |
| 238 |
} |
| 239 |
/// \endcond |
| 240 |
} // namespace ranges |
| 241 |
|
| 242 |
// Hijack the primary std::iterator_traits template from each of the 3 major |
| 243 |
// standard library implementations |
| 244 |
RANGES_BEGIN_NAMESPACE_STD |
| 245 |
RANGES_BEGIN_NAMESPACE_VERSION |
| 246 |
#if defined(__GLIBCXX__) |
| 247 |
template<typename I> |
| 248 |
struct __iterator_traits< |
| 249 |
I, |
| 250 |
::ranges::detail::enable_if_t<!::ranges::detail::has_iterator_typedefs_impl_<I>(0)>> |
| 251 |
: decltype(::ranges::detail::std_iterator_traits_impl_<I>(0)) |
| 252 |
{}; |
| 253 |
#elif defined(_LIBCPP_VERSION) |
| 254 |
template<typename I> |
| 255 |
struct __iterator_traits<I, false> // doesn't have I::iterator_category |
| 256 |
: decltype(::ranges::detail::std_iterator_traits_impl_<I>(0)) |
| 257 |
{}; |
| 258 |
#elif defined(_MSVC_STL_VERSION) |
| 259 |
template<typename I> |
| 260 |
struct _Iterator_traits_base< |
| 261 |
I, |
| 262 |
#ifdef RANGES_WORKAROUND_MSVC_792338 |
| 263 |
::ranges::detail::enable_if_t<decltype(bool_constant< |
| 264 |
!::ranges::detail::has_iterator_typedefs_impl_<I>(0)>{})::value>> |
| 265 |
#else // ^^^ workaround / no workaround vvv |
| 266 |
::ranges::detail::enable_if_t<!::ranges::detail::has_iterator_typedefs_impl_<I>(0)>> |
| 267 |
#endif // RANGES_WORKAROUND_MSVC_792338 |
| 268 |
: decltype(::ranges::detail::std_iterator_traits_impl_<I>(0)) |
| 269 |
{}; |
| 270 |
#endif |
| 271 |
RANGES_END_NAMESPACE_VERSION |
| 272 |
RANGES_END_NAMESPACE_STD |
| 273 |
|
| 274 |
#include <range/v3/detail/epilogue.hpp> |
| 275 |
|
| 276 |
#endif // RANGES_DEEP_STL_INTEGRATION |
| 277 |
|
| 278 |
#endif // RANGES_V3_STD_ITERATOR |