Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:14:02

0001 // -*- C++ -*-
0002 //===----------------------------------------------------------------------===//
0003 //
0004 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0005 // See https://llvm.org/LICENSE.txt for license information.
0006 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0007 //
0008 //===----------------------------------------------------------------------===//
0009 
0010 #ifndef _LIBCPP___RANGES_RANGE_ADAPTOR_H
0011 #define _LIBCPP___RANGES_RANGE_ADAPTOR_H
0012 
0013 #include <__concepts/constructible.h>
0014 #include <__concepts/derived_from.h>
0015 #include <__concepts/invocable.h>
0016 #include <__concepts/same_as.h>
0017 #include <__config>
0018 #include <__functional/compose.h>
0019 #include <__functional/invoke.h>
0020 #include <__ranges/concepts.h>
0021 #include <__type_traits/decay.h>
0022 #include <__type_traits/invoke.h>
0023 #include <__type_traits/is_class.h>
0024 #include <__type_traits/is_nothrow_constructible.h>
0025 #include <__type_traits/remove_cv.h>
0026 #include <__type_traits/remove_cvref.h>
0027 #include <__utility/forward.h>
0028 #include <__utility/move.h>
0029 
0030 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0031 #  pragma GCC system_header
0032 #endif
0033 
0034 _LIBCPP_PUSH_MACROS
0035 #include <__undef_macros>
0036 
0037 _LIBCPP_BEGIN_NAMESPACE_STD
0038 
0039 #if _LIBCPP_STD_VER >= 20
0040 
0041 namespace ranges {
0042 
0043 // CRTP base that one can derive from in order to be considered a range adaptor closure
0044 // by the library. When deriving from this class, a pipe operator will be provided to
0045 // make the following hold:
0046 // - `x | f` is equivalent to `f(x)`
0047 // - `f1 | f2` is an adaptor closure `g` such that `g(x)` is equivalent to `f2(f1(x))`
0048 template <class _Tp>
0049   requires is_class_v<_Tp> && same_as<_Tp, remove_cv_t<_Tp>>
0050 struct __range_adaptor_closure {};
0051 
0052 // Type that wraps an arbitrary function object and makes it into a range adaptor closure,
0053 // i.e. something that can be called via the `x | f` notation.
0054 template <class _Fn>
0055 struct __pipeable : _Fn, __range_adaptor_closure<__pipeable<_Fn>> {
0056   _LIBCPP_HIDE_FROM_ABI constexpr explicit __pipeable(_Fn&& __f) : _Fn(std::move(__f)) {}
0057 };
0058 _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(__pipeable);
0059 
0060 template <class _Tp>
0061 _Tp __derived_from_range_adaptor_closure(__range_adaptor_closure<_Tp>*);
0062 
0063 template <class _Tp>
0064 concept _RangeAdaptorClosure = !ranges::range<remove_cvref_t<_Tp>> && requires {
0065   // Ensure that `remove_cvref_t<_Tp>` is derived from `__range_adaptor_closure<remove_cvref_t<_Tp>>` and isn't derived
0066   // from `__range_adaptor_closure<U>` for any other type `U`.
0067   { ranges::__derived_from_range_adaptor_closure((remove_cvref_t<_Tp>*)nullptr) } -> same_as<remove_cvref_t<_Tp>>;
0068 };
0069 
0070 template <ranges::range _Range, _RangeAdaptorClosure _Closure>
0071   requires invocable<_Closure, _Range>
0072 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto)
0073 operator|(_Range&& __range, _Closure&& __closure) noexcept(is_nothrow_invocable_v<_Closure, _Range>) {
0074   return std::invoke(std::forward<_Closure>(__closure), std::forward<_Range>(__range));
0075 }
0076 
0077 template <_RangeAdaptorClosure _Closure, _RangeAdaptorClosure _OtherClosure>
0078   requires constructible_from<decay_t<_Closure>, _Closure> && constructible_from<decay_t<_OtherClosure>, _OtherClosure>
0079 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator|(_Closure&& __c1, _OtherClosure&& __c2) noexcept(
0080     is_nothrow_constructible_v<decay_t<_Closure>, _Closure> &&
0081     is_nothrow_constructible_v<decay_t<_OtherClosure>, _OtherClosure>) {
0082   return __pipeable(std::__compose(std::forward<_OtherClosure>(__c2), std::forward<_Closure>(__c1)));
0083 }
0084 
0085 #  if _LIBCPP_STD_VER >= 23
0086 template <class _Tp>
0087   requires is_class_v<_Tp> && same_as<_Tp, remove_cv_t<_Tp>>
0088 class _LIBCPP_NO_SPECIALIZATIONS range_adaptor_closure : public __range_adaptor_closure<_Tp> {};
0089 #  endif // _LIBCPP_STD_VER >= 23
0090 
0091 } // namespace ranges
0092 
0093 #endif // _LIBCPP_STD_VER >= 20
0094 
0095 _LIBCPP_END_NAMESPACE_STD
0096 
0097 _LIBCPP_POP_MACROS
0098 
0099 #endif // _LIBCPP___RANGES_RANGE_ADAPTOR_H