xref: /freebsd/contrib/llvm-project/libcxx/include/__ranges/range_adaptor.h (revision 02e9120893770924227138ba49df1edb3896112a)
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_nothrow_constructible.h>
23 #include <__type_traits/remove_cvref.h>
24 #include <__utility/forward.h>
25 #include <__utility/move.h>
26 
27 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
28 #  pragma GCC system_header
29 #endif
30 
31 _LIBCPP_PUSH_MACROS
32 #include <__undef_macros>
33 
34 _LIBCPP_BEGIN_NAMESPACE_STD
35 
36 #if _LIBCPP_STD_VER >= 20
37 
38 // CRTP base that one can derive from in order to be considered a range adaptor closure
39 // by the library. When deriving from this class, a pipe operator will be provided to
40 // make the following hold:
41 // - `x | f` is equivalent to `f(x)`
42 // - `f1 | f2` is an adaptor closure `g` such that `g(x)` is equivalent to `f2(f1(x))`
43 template <class _Tp>
44 struct __range_adaptor_closure;
45 
46 // Type that wraps an arbitrary function object and makes it into a range adaptor closure,
47 // i.e. something that can be called via the `x | f` notation.
48 template <class _Fn>
49 struct __range_adaptor_closure_t : _Fn, __range_adaptor_closure<__range_adaptor_closure_t<_Fn>> {
50     _LIBCPP_HIDE_FROM_ABI constexpr explicit __range_adaptor_closure_t(_Fn&& __f) : _Fn(std::move(__f)) { }
51 };
52 _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(__range_adaptor_closure_t);
53 
54 template <class _Tp>
55 concept _RangeAdaptorClosure = derived_from<remove_cvref_t<_Tp>, __range_adaptor_closure<remove_cvref_t<_Tp>>>;
56 
57 template <class _Tp>
58 struct __range_adaptor_closure {
59     template <ranges::viewable_range _View, _RangeAdaptorClosure _Closure>
60         requires same_as<_Tp, remove_cvref_t<_Closure>> &&
61                  invocable<_Closure, _View>
62     [[nodiscard]] _LIBCPP_HIDE_FROM_ABI
63     friend constexpr decltype(auto) operator|(_View&& __view, _Closure&& __closure)
64         noexcept(is_nothrow_invocable_v<_Closure, _View>)
65     { return std::invoke(std::forward<_Closure>(__closure), std::forward<_View>(__view)); }
66 
67     template <_RangeAdaptorClosure _Closure, _RangeAdaptorClosure _OtherClosure>
68         requires same_as<_Tp, remove_cvref_t<_Closure>> &&
69                  constructible_from<decay_t<_Closure>, _Closure> &&
70                  constructible_from<decay_t<_OtherClosure>, _OtherClosure>
71     [[nodiscard]] _LIBCPP_HIDE_FROM_ABI
72     friend constexpr auto operator|(_Closure&& __c1, _OtherClosure&& __c2)
73         noexcept(is_nothrow_constructible_v<decay_t<_Closure>, _Closure> &&
74                  is_nothrow_constructible_v<decay_t<_OtherClosure>, _OtherClosure>)
75     { return __range_adaptor_closure_t(std::__compose(std::forward<_OtherClosure>(__c2), std::forward<_Closure>(__c1))); }
76 };
77 
78 #endif // _LIBCPP_STD_VER >= 20
79 
80 _LIBCPP_END_NAMESPACE_STD
81 
82 _LIBCPP_POP_MACROS
83 
84 #endif // _LIBCPP___RANGES_RANGE_ADAPTOR_H
85