xref: /freebsd/contrib/llvm-project/libcxx/include/__ranges/counted.h (revision 5b56413d04e608379c9a306373554a8e4d321bc0)
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_COUNTED_H
11 #define _LIBCPP___RANGES_COUNTED_H
12 
13 #include <__concepts/convertible_to.h>
14 #include <__config>
15 #include <__iterator/concepts.h>
16 #include <__iterator/counted_iterator.h>
17 #include <__iterator/default_sentinel.h>
18 #include <__iterator/incrementable_traits.h>
19 #include <__iterator/iterator_traits.h>
20 #include <__memory/pointer_traits.h>
21 #include <__ranges/subrange.h>
22 #include <__type_traits/decay.h>
23 #include <__utility/forward.h>
24 #include <__utility/move.h>
25 #include <cstddef>
26 #include <span>
27 
28 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
29 #  pragma GCC system_header
30 #endif
31 
32 _LIBCPP_PUSH_MACROS
33 #include <__undef_macros>
34 
35 _LIBCPP_BEGIN_NAMESPACE_STD
36 
37 #if _LIBCPP_STD_VER >= 20
38 
39 namespace ranges::views {
40 
41 namespace __counted {
42 
43 struct __fn {
44   template <contiguous_iterator _It>
45   _LIBCPP_HIDE_FROM_ABI static constexpr auto
46   __go(_It __it,
47        iter_difference_t<_It> __count) noexcept(noexcept(span(std::to_address(__it), static_cast<size_t>(__count))))
48   // Deliberately omit return-type SFINAE, because to_address is not SFINAE-friendly
49   {
50     return span(std::to_address(__it), static_cast<size_t>(__count));
51   }
52 
53   template <random_access_iterator _It>
54   _LIBCPP_HIDE_FROM_ABI static constexpr auto
55   __go(_It __it, iter_difference_t<_It> __count) noexcept(noexcept(subrange(__it, __it + __count)))
56       -> decltype(subrange(__it, __it + __count)) {
57     return subrange(__it, __it + __count);
58   }
59 
60   template <class _It>
61   _LIBCPP_HIDE_FROM_ABI static constexpr auto __go(_It __it, iter_difference_t<_It> __count) noexcept(
62       noexcept(subrange(counted_iterator(std::move(__it), __count), default_sentinel)))
63       -> decltype(subrange(counted_iterator(std::move(__it), __count), default_sentinel)) {
64     return subrange(counted_iterator(std::move(__it), __count), default_sentinel);
65   }
66 
67   template <class _It, convertible_to<iter_difference_t<_It>> _Diff>
68     requires input_or_output_iterator<decay_t<_It>>
69   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_It&& __it, _Diff&& __count) const
70       noexcept(noexcept(__go(std::forward<_It>(__it), std::forward<_Diff>(__count))))
71           -> decltype(__go(std::forward<_It>(__it), std::forward<_Diff>(__count))) {
72     return __go(std::forward<_It>(__it), std::forward<_Diff>(__count));
73   }
74 };
75 
76 } // namespace __counted
77 
78 inline namespace __cpo {
79 inline constexpr auto counted = __counted::__fn{};
80 } // namespace __cpo
81 
82 } // namespace ranges::views
83 
84 #endif // _LIBCPP_STD_VER >= 20
85 
86 _LIBCPP_END_NAMESPACE_STD
87 
88 _LIBCPP_POP_MACROS
89 
90 #endif // _LIBCPP___RANGES_COUNTED_H
91