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