Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
make_subrange.hpp
Go to the documentation of this file.
1// parasoft-begin-suppress AUTOSAR-A2_8_1-a-2 "False positive: also defines arene::base::make_subrange"
2
3// Copyright 2024, Toyota Motor Corporation
4//
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_UTILITY_MAKE_SUBRANGE_HPP_
7#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_UTILITY_MAKE_SUBRANGE_HPP_
8
9// IWYU pragma: private, include "arene/base/utility.hpp"
10// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
11
12// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
14#include "arene/base/ranges/detail/range_interface.hpp"
16#include "arene/base/stdlib_choice/enable_if.hpp"
17#include "arene/base/stdlib_choice/integral_constant.hpp"
18#include "arene/base/stdlib_choice/is_copy_constructible.hpp"
19#include "arene/base/stdlib_choice/is_move_constructible.hpp"
20#include "arene/base/stdlib_choice/iterator_traits.hpp"
21#include "arene/base/stdlib_choice/move.hpp"
24// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
25
26// parasoft-begin-suppress AUTOSAR-M2_10_1-a "Similar names permitted by M2-10-1 Permit #1"
27
28namespace arene {
29namespace base {
30
31namespace make_subrange_detail {
32
33///
34/// @brief Implementation of a simple range view around an iterator and a sentinel. Morally similar to @c std::subrange.
35///
36/// @tparam Iterator The type of the position iterator for the range
37/// @tparam Sentinel The type of the sentinel for the range
38///
39template <typename Iterator, typename Sentinel>
40class iterator_range : public ranges::detail::range_interface<iterator_range<Iterator, Sentinel>> {
41 public:
42 /// @brief The iterator type of the range
43 using iterator_type = remove_cvref_t<Iterator>;
44 /// @brief The sentinel type of the range
45 using sentinel_type = remove_cvref_t<Sentinel>;
46 /// @brief The value type of the range
47 using value_type = typename std::iterator_traits<iterator_type>::value_type;
48
49 /// @brief The iterator type of the range
50 using iterator = iterator_type;
51 /// @brief The const iterator type of the range
52 using const_iterator = iterator_type;
53
54 /// @brief Construct an iterator_range from an iterator/sentinel pair
55 ///
56 /// @param itr The itr
57 /// @param sent The sent
58 constexpr iterator_range(iterator_type itr, sentinel_type sent) noexcept(
61 )
62 : ranges::detail::range_interface<iterator_range>{this},
63 iterator_(std::move(itr)),
64 sentinel_(std::move(sent)) {}
65
66 // parasoft-begin-suppress AUTOSAR-A2_10_1-d "False positive: these standard range API names do not hide anything"
67 /// @brief Obtain the iterator for the start of the range
68 /// @return The iterator
69 constexpr auto begin() const noexcept(std::is_nothrow_copy_constructible<iterator_type>::value) -> iterator_type {
70 return iterator_;
71 }
72 /// @brief Obtain the sentinel for the end of the range
73 /// @return The sentinel
74 constexpr auto end() const noexcept(std::is_nothrow_copy_constructible<sentinel_type>::value) -> sentinel_type {
75 return sentinel_;
76 }
77 // parasoft-end-suppress AUTOSAR-A2_10_1-d
78
79 private:
80 /// @brief The iterator representing the start of the subrange
81 iterator_type iterator_;
82 /// @brief The sentinel representing the end of the subrange
83 sentinel_type sentinel_;
84};
85} // namespace make_subrange_detail
86
87// parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
88/// @brief Combine an iterator and sentinel into a range type that can be used with range-based @c for loops.
89/// @tparam Iterator The type of the position iterator for the range
90/// @tparam Sentinel The type of the sentinel for the range
91/// @param iterator The iterator
92/// @param sentinel The sentinel
93/// @return A range object for the iterator/sentinel pair
94template <
95 typename Iterator,
96 typename Sentinel,
98constexpr auto make_subrange(Iterator iterator, Sentinel sentinel) noexcept(
101) -> make_subrange_detail::iterator_range<Iterator, Sentinel> {
102 return make_subrange_detail::iterator_range<Iterator, Sentinel>{std::move(iterator), std::move(sentinel)};
103}
104// parasoft-end-suppress AUTOSAR-M3_3_2-a-2
105
106namespace ranges {
107/// @brief Indicate that the iterator_range type is a borrowed range
108/// @tparam Iterator The type of the position iterator for the range
109/// @tparam Sentinel The type of the sentinel for the range
110template <typename Iterator, typename Sentinel>
111class enable_borrowed_range<make_subrange_detail::iterator_range<Iterator, Sentinel>> : public std::true_type {};
112} // namespace ranges
113
114} // namespace base
115} // namespace arene
116
117#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_UTILITY_MAKE_SUBRANGE_HPP_
static constexpr value_type value
Definition integral_constant.hpp:34
Definition accumulate.hpp:37
constexpr auto make_subrange(Iterator iterator, Sentinel sentinel) noexcept(std::is_nothrow_move_constructible< Iterator >::value &&//std::is_nothrow_move_constructible< Sentinel >::value) -> make_subrange_detail::iterator_range< Iterator, Sentinel >
Combine an iterator and sentinel into a range type that can be used with range-based for loops.
Definition make_subrange.hpp:98
typename constraints_impl::constraints< EnableIfs... >::type constraints
Helper used to express template constraints in a consistent way using std::enable_if_t.
Definition constraints.hpp:51
remove_cv_t< remove_reference_t< T > > remove_cvref_t
Backport of std::remove_cvref_t.
Definition remove_cvref.hpp:35
Definition accumulate.hpp:36
constexpr auto move(T &&value) noexcept -> remove_reference_t< T > &&
Cast a value to an rvalue.
Definition move.hpp:30
bool_constant< true > true_type
An alias for bool_constant<true>
Definition integral_constant.hpp:76