xref: /openbsd-src/gnu/llvm/libcxx/include/__algorithm/ranges_for_each.h (revision 4bdff4bed0e3d54e55670334c7d0077db4170f86)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef _LIBCPP___ALGORITHM_RANGES_FOR_EACH_H
10 #define _LIBCPP___ALGORITHM_RANGES_FOR_EACH_H
11 
12 #include <__algorithm/in_fun_result.h>
13 #include <__config>
14 #include <__functional/identity.h>
15 #include <__functional/invoke.h>
16 #include <__iterator/concepts.h>
17 #include <__iterator/projected.h>
18 #include <__ranges/access.h>
19 #include <__ranges/concepts.h>
20 #include <__ranges/dangling.h>
21 #include <__utility/move.h>
22 
23 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
24 #  pragma GCC system_header
25 #endif
26 
27 #if _LIBCPP_STD_VER > 17
28 
29 _LIBCPP_BEGIN_NAMESPACE_STD
30 
31 namespace ranges {
32 
33 template <class _Iter, class _Func>
34 using for_each_result = in_fun_result<_Iter, _Func>;
35 
36 namespace __for_each {
37 struct __fn {
38 private:
39   template <class _Iter, class _Sent, class _Proj, class _Func>
40   _LIBCPP_HIDE_FROM_ABI constexpr static
__for_each_impl__fn41   for_each_result<_Iter, _Func> __for_each_impl(_Iter __first, _Sent __last, _Func& __func, _Proj& __proj) {
42     for (; __first != __last; ++__first)
43       std::invoke(__func, std::invoke(__proj, *__first));
44     return {std::move(__first), std::move(__func)};
45   }
46 
47 public:
48   template <input_iterator _Iter, sentinel_for<_Iter> _Sent,
49             class _Proj = identity,
50             indirectly_unary_invocable<projected<_Iter, _Proj>> _Func>
51   _LIBCPP_HIDE_FROM_ABI constexpr
operator__fn52   for_each_result<_Iter, _Func> operator()(_Iter __first, _Sent __last, _Func __func, _Proj __proj = {}) const {
53     return __for_each_impl(std::move(__first), std::move(__last), __func, __proj);
54   }
55 
56   template <input_range _Range,
57             class _Proj = identity,
58             indirectly_unary_invocable<projected<iterator_t<_Range>, _Proj>> _Func>
59   _LIBCPP_HIDE_FROM_ABI constexpr
operator__fn60   for_each_result<borrowed_iterator_t<_Range>, _Func> operator()(_Range&& __range,
61                                                                  _Func __func,
62                                                                  _Proj __proj = {}) const {
63     return __for_each_impl(ranges::begin(__range), ranges::end(__range), __func, __proj);
64   }
65 
66 };
67 } // namespace __for_each
68 
69 inline namespace __cpo {
70   inline constexpr auto for_each = __for_each::__fn{};
71 } // namespace __cpo
72 } // namespace ranges
73 
74 _LIBCPP_END_NAMESPACE_STD
75 
76 #endif // _LIBCPP_STD_VER > 17
77 
78 #endif // _LIBCPP___ALGORITHM_RANGES_FOR_EACH_H
79