| 1 |
// -*- C++ -*- |
| 2 |
//===----------------------------------------------------------------------===// |
| 3 |
// |
| 4 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 |
// See https://llvm.org/LICENSE.txt for license information. |
| 6 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 |
// |
| 8 |
//===----------------------------------------------------------------------===// |
| 9 |
|
| 10 |
#ifndef _LIBCPP___RANGES_RANGE_ADAPTOR_H |
| 11 |
#define _LIBCPP___RANGES_RANGE_ADAPTOR_H |
| 12 |
|
| 13 |
#include <__concepts/constructible.h> |
| 14 |
#include <__concepts/derived_from.h> |
| 15 |
#include <__concepts/invocable.h> |
| 16 |
#include <__concepts/same_as.h> |
| 17 |
#include <__config> |
| 18 |
#include <__functional/compose.h> |
| 19 |
#include <__functional/invoke.h> |
| 20 |
#include <__ranges/concepts.h> |
| 21 |
#include <__type_traits/decay.h> |
| 22 |
#include <__type_traits/is_class.h> |
| 23 |
#include <__type_traits/is_nothrow_constructible.h> |
| 24 |
#include <__type_traits/remove_cvref.h> |
| 25 |
#include <__utility/forward.h> |
| 26 |
#include <__utility/move.h> |
| 27 |
|
| 28 |
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 29 |
# pragma GCC system_header |
| 30 |
#endif |
| 31 |
|
| 32 |
_LIBCPP_PUSH_MACROS |
| 33 |
#include <__undef_macros> |
| 34 |
|
| 35 |
_LIBCPP_BEGIN_NAMESPACE_STD |
| 36 |
|
| 37 |
#if _LIBCPP_STD_VER >= 20 |
| 38 |
|
| 39 |
namespace ranges { |
| 40 |
|
| 41 |
// CRTP base that one can derive from in order to be considered a range adaptor closure |
| 42 |
// by the library. When deriving from this class, a pipe operator will be provided to |
| 43 |
// make the following hold: |
| 44 |
// - `x | f` is equivalent to `f(x)` |
| 45 |
// - `f1 | f2` is an adaptor closure `g` such that `g(x)` is equivalent to `f2(f1(x))` |
| 46 |
template <class _Tp> |
| 47 |
requires is_class_v<_Tp> && same_as<_Tp, remove_cv_t<_Tp>> |
| 48 |
struct __range_adaptor_closure; |
| 49 |
|
| 50 |
// Type that wraps an arbitrary function object and makes it into a range adaptor closure, |
| 51 |
// i.e. something that can be called via the `x | f` notation. |
| 52 |
template <class _Fn> |
| 53 |
struct __range_adaptor_closure_t : _Fn, __range_adaptor_closure<__range_adaptor_closure_t<_Fn>> { |
| 54 |
_LIBCPP_HIDE_FROM_ABI constexpr explicit __range_adaptor_closure_t(_Fn&& __f) : _Fn(std::move(__f)) {} |
| 55 |
}; |
| 56 |
_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(__range_adaptor_closure_t); |
| 57 |
|
| 58 |
template <class _Tp> |
| 59 |
_Tp __derived_from_range_adaptor_closure(__range_adaptor_closure<_Tp>*); |
| 60 |
|
| 61 |
template <class _Tp> |
| 62 |
concept _RangeAdaptorClosure = !ranges::range<remove_cvref_t<_Tp>> && requires { |
| 63 |
// Ensure that `remove_cvref_t<_Tp>` is derived from `__range_adaptor_closure<remove_cvref_t<_Tp>>` and isn't derived |
| 64 |
// from `__range_adaptor_closure<U>` for any other type `U`. |
| 65 |
{ ranges::__derived_from_range_adaptor_closure((remove_cvref_t<_Tp>*)nullptr) } -> same_as<remove_cvref_t<_Tp>>; |
| 66 |
}; |
| 67 |
|
| 68 |
template <ranges::range _Range, _RangeAdaptorClosure _Closure> |
| 69 |
requires invocable<_Closure, _Range> |
| 70 |
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) |
| 71 |
operator|(_Range&& __range, _Closure&& __closure) noexcept(is_nothrow_invocable_v<_Closure, _Range>) { |
| 72 |
return std::invoke(std::forward<_Closure>(__closure), std::forward<_Range>(__range)); |
| 73 |
} |
| 74 |
|
| 75 |
template <_RangeAdaptorClosure _Closure, _RangeAdaptorClosure _OtherClosure> |
| 76 |
requires constructible_from<decay_t<_Closure>, _Closure> && constructible_from<decay_t<_OtherClosure>, _OtherClosure> |
| 77 |
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator|(_Closure&& __c1, _OtherClosure&& __c2) noexcept( |
| 78 |
is_nothrow_constructible_v<decay_t<_Closure>, _Closure> && |
| 79 |
is_nothrow_constructible_v<decay_t<_OtherClosure>, _OtherClosure>) { |
| 80 |
return __range_adaptor_closure_t(std::__compose(std::forward<_OtherClosure>(__c2), std::forward<_Closure>(__c1))); |
| 81 |
} |
| 82 |
|
| 83 |
template <class _Tp> |
| 84 |
requires is_class_v<_Tp> && same_as<_Tp, remove_cv_t<_Tp>> |
| 85 |
struct __range_adaptor_closure {}; |
| 86 |
|
| 87 |
# if _LIBCPP_STD_VER >= 23 |
| 88 |
template <class _Tp> |
| 89 |
requires is_class_v<_Tp> && same_as<_Tp, remove_cv_t<_Tp>> |
| 90 |
class range_adaptor_closure : public __range_adaptor_closure<_Tp> {}; |
| 91 |
# endif // _LIBCPP_STD_VER >= 23 |
| 92 |
|
| 93 |
} // namespace ranges |
| 94 |
|
| 95 |
#endif // _LIBCPP_STD_VER >= 20 |
| 96 |
|
| 97 |
_LIBCPP_END_NAMESPACE_STD |
| 98 |
|
| 99 |
_LIBCPP_POP_MACROS |
| 100 |
|
| 101 |
#endif // _LIBCPP___RANGES_RANGE_ADAPTOR_H |