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