xref: /freebsd-src/contrib/llvm-project/libcxx/include/__ranges/range_adaptor.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1349cc55cSDimitry Andric // -*- C++ -*-
2349cc55cSDimitry Andric //===----------------------------------------------------------------------===//
3349cc55cSDimitry Andric //
4349cc55cSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5349cc55cSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
6349cc55cSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7349cc55cSDimitry Andric //
8349cc55cSDimitry Andric //===----------------------------------------------------------------------===//
9349cc55cSDimitry Andric 
10349cc55cSDimitry Andric #ifndef _LIBCPP___RANGES_RANGE_ADAPTOR_H
11349cc55cSDimitry Andric #define _LIBCPP___RANGES_RANGE_ADAPTOR_H
12349cc55cSDimitry Andric 
13bdd1243dSDimitry Andric #include <__concepts/constructible.h>
14bdd1243dSDimitry Andric #include <__concepts/derived_from.h>
15bdd1243dSDimitry Andric #include <__concepts/invocable.h>
16bdd1243dSDimitry Andric #include <__concepts/same_as.h>
17349cc55cSDimitry Andric #include <__config>
18349cc55cSDimitry Andric #include <__functional/compose.h>
19349cc55cSDimitry Andric #include <__functional/invoke.h>
20349cc55cSDimitry Andric #include <__ranges/concepts.h>
2106c3fb27SDimitry Andric #include <__type_traits/decay.h>
22*0fca6ea1SDimitry Andric #include <__type_traits/is_class.h>
2306c3fb27SDimitry Andric #include <__type_traits/is_nothrow_constructible.h>
2406c3fb27SDimitry Andric #include <__type_traits/remove_cvref.h>
25349cc55cSDimitry Andric #include <__utility/forward.h>
26349cc55cSDimitry Andric #include <__utility/move.h>
27349cc55cSDimitry Andric 
28349cc55cSDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
29349cc55cSDimitry Andric #  pragma GCC system_header
30349cc55cSDimitry Andric #endif
31349cc55cSDimitry Andric 
3206c3fb27SDimitry Andric _LIBCPP_PUSH_MACROS
3306c3fb27SDimitry Andric #include <__undef_macros>
3406c3fb27SDimitry Andric 
35349cc55cSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
36349cc55cSDimitry Andric 
3706c3fb27SDimitry Andric #if _LIBCPP_STD_VER >= 20
38349cc55cSDimitry Andric 
39*0fca6ea1SDimitry Andric namespace ranges {
40*0fca6ea1SDimitry Andric 
41349cc55cSDimitry Andric // CRTP base that one can derive from in order to be considered a range adaptor closure
42349cc55cSDimitry Andric // by the library. When deriving from this class, a pipe operator will be provided to
43349cc55cSDimitry Andric // make the following hold:
44349cc55cSDimitry Andric // - `x | f` is equivalent to `f(x)`
45349cc55cSDimitry Andric // - `f1 | f2` is an adaptor closure `g` such that `g(x)` is equivalent to `f2(f1(x))`
46349cc55cSDimitry Andric template <class _Tp>
47*0fca6ea1SDimitry Andric   requires is_class_v<_Tp> && same_as<_Tp, remove_cv_t<_Tp>>
48349cc55cSDimitry Andric struct __range_adaptor_closure;
49349cc55cSDimitry Andric 
50349cc55cSDimitry Andric // Type that wraps an arbitrary function object and makes it into a range adaptor closure,
51349cc55cSDimitry Andric // i.e. something that can be called via the `x | f` notation.
52349cc55cSDimitry Andric template <class _Fn>
53349cc55cSDimitry Andric struct __range_adaptor_closure_t : _Fn, __range_adaptor_closure<__range_adaptor_closure_t<_Fn>> {
5406c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr explicit __range_adaptor_closure_t(_Fn&& __f) : _Fn(std::move(__f)) {}
55349cc55cSDimitry Andric };
56bdd1243dSDimitry Andric _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(__range_adaptor_closure_t);
57349cc55cSDimitry Andric 
58349cc55cSDimitry Andric template <class _Tp>
59*0fca6ea1SDimitry Andric _Tp __derived_from_range_adaptor_closure(__range_adaptor_closure<_Tp>*);
60349cc55cSDimitry Andric 
61349cc55cSDimitry Andric template <class _Tp>
62*0fca6ea1SDimitry Andric concept _RangeAdaptorClosure = !ranges::range<remove_cvref_t<_Tp>> && requires {
63*0fca6ea1SDimitry Andric   // Ensure that `remove_cvref_t<_Tp>` is derived from `__range_adaptor_closure<remove_cvref_t<_Tp>>` and isn't derived
64*0fca6ea1SDimitry Andric   // from `__range_adaptor_closure<U>` for any other type `U`.
65*0fca6ea1SDimitry Andric   { ranges::__derived_from_range_adaptor_closure((remove_cvref_t<_Tp>*)nullptr) } -> same_as<remove_cvref_t<_Tp>>;
66*0fca6ea1SDimitry Andric };
67*0fca6ea1SDimitry Andric 
68*0fca6ea1SDimitry Andric template <ranges::range _Range, _RangeAdaptorClosure _Closure>
69*0fca6ea1SDimitry Andric   requires invocable<_Closure, _Range>
70*0fca6ea1SDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto)
71*0fca6ea1SDimitry Andric operator|(_Range&& __range, _Closure&& __closure) noexcept(is_nothrow_invocable_v<_Closure, _Range>) {
72*0fca6ea1SDimitry Andric   return std::invoke(std::forward<_Closure>(__closure), std::forward<_Range>(__range));
73cb14a3feSDimitry Andric }
74349cc55cSDimitry Andric 
75349cc55cSDimitry Andric template <_RangeAdaptorClosure _Closure, _RangeAdaptorClosure _OtherClosure>
76*0fca6ea1SDimitry Andric   requires constructible_from<decay_t<_Closure>, _Closure> && constructible_from<decay_t<_OtherClosure>, _OtherClosure>
77*0fca6ea1SDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator|(_Closure&& __c1, _OtherClosure&& __c2) noexcept(
78cb14a3feSDimitry Andric     is_nothrow_constructible_v<decay_t<_Closure>, _Closure> &&
79cb14a3feSDimitry Andric     is_nothrow_constructible_v<decay_t<_OtherClosure>, _OtherClosure>) {
80cb14a3feSDimitry Andric   return __range_adaptor_closure_t(std::__compose(std::forward<_OtherClosure>(__c2), std::forward<_Closure>(__c1)));
81cb14a3feSDimitry Andric }
82*0fca6ea1SDimitry Andric 
83*0fca6ea1SDimitry Andric template <class _Tp>
84*0fca6ea1SDimitry Andric   requires is_class_v<_Tp> && same_as<_Tp, remove_cv_t<_Tp>>
85*0fca6ea1SDimitry Andric struct __range_adaptor_closure {};
86*0fca6ea1SDimitry Andric 
87*0fca6ea1SDimitry Andric #  if _LIBCPP_STD_VER >= 23
88*0fca6ea1SDimitry Andric template <class _Tp>
89*0fca6ea1SDimitry Andric   requires is_class_v<_Tp> && same_as<_Tp, remove_cv_t<_Tp>>
90*0fca6ea1SDimitry Andric class range_adaptor_closure : public __range_adaptor_closure<_Tp> {};
91*0fca6ea1SDimitry Andric #  endif // _LIBCPP_STD_VER >= 23
92*0fca6ea1SDimitry Andric 
93*0fca6ea1SDimitry Andric } // namespace ranges
94349cc55cSDimitry Andric 
9506c3fb27SDimitry Andric #endif // _LIBCPP_STD_VER >= 20
96349cc55cSDimitry Andric 
97349cc55cSDimitry Andric _LIBCPP_END_NAMESPACE_STD
98349cc55cSDimitry Andric 
9906c3fb27SDimitry Andric _LIBCPP_POP_MACROS
10006c3fb27SDimitry Andric 
101349cc55cSDimitry Andric #endif // _LIBCPP___RANGES_RANGE_ADAPTOR_H
102