Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
integer_sequence.hpp
Go to the documentation of this file.
1// Copyright 2024, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_INTEGER_SEQUENCE_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_INTEGER_SEQUENCE_HPP_
7
8// parasoft-begin-suppress CERT_CPP-DCL58-a-2 "Part of a standard library implementation"
9// parasoft-begin-suppress AUTOSAR-A17_6_1-a-2 "Part of a standard library implementation"
10
11// IWYU pragma: private, include <utility>
12// IWYU pragma: friend "stdlib_detail/.*"
13
20
21namespace std {
22
23/// @brief Implements a compile-time sequence of integers
24/// @tparam Type an integer type to use for the sequence of elements
25/// @tparam Ints a non-type parameter pack of elements defining the sequence
26///
27/// Represents a compile-time sequence of integers of type @c Type. When used as
28/// an argument to a function template, the parameter pack defining the sequence
29/// can be deduced and used in a pack expansion.
30///
31template <typename Type, Type... Ints>
33 public:
34 static_assert(is_integral_v<Type>, "'Type' must be an integer.");
35
36 /// @brief The sequence integer type
37 // parasoft-begin-suppress AUTOSAR-A2_10_1-d "False positive: standard type aliases do not hide anything"
38 using value_type = Type;
39 // parasoft-end-suppress AUTOSAR-A2_10_1-d
40
41 // parasoft-begin-suppress AUTOSAR-A2_10_1-d "False positive: 'size' does not hide anything"
42 /// @brief Returns the number of elements in the sequence
43 /// @return @c sizeof...(Ints)
44 static constexpr auto size() noexcept -> size_t { return sizeof...(Ints); }
45 // parasoft-end-suppress AUTOSAR-A2_10_1-d
46};
47
49
50/// @brief helper for quickly expanding the elements in a sequence
51/// @tparam T integer type
52/// @tparam DoDouble if true, double the number of elements in the sequence
53///
54/// Helper type used to reduce the number of template instantiations for a large
55/// integer sequence.
56///
57template <class T, bool DoDouble>
59
60/// @brief helper for constructing an integer sequence from @c 0 to <tt>N - 1</tt>
61/// @tparam T integer type
62/// @tparam N remaining number of sequence elements to generate
63/// @tparam Is current sequence
64///
65/// Primary template for expanding the sequence of elements.
66///
67template <typename T, size_t N, T... Is>
68class make_sequence_impl : public fast_expand_sequence<T, (N >= sizeof...(Is))>::template type<N, Is...> {};
69
70/// @brief recursive base case specialization
71/// @tparam T integer type
72/// @tparam Is sequence of elements
73///
74/// If @c N is @c 0, construction of the sequence is complete
75///
76template <typename T>
77class make_sequence_impl<T, 0> {
78 public:
79 /// @brief completed integer sequence type
81};
82
83/// @brief recursive base case specialization
84/// @tparam T integer type
85/// @tparam Is sequence of elements
86///
87/// If @c N is @c 0, construction of the sequence is complete
88///
89template <typename T, T... Is>
90class make_sequence_impl<T, 0, Is...> {
91 public:
92 /// @brief completed integer sequence type
93 using type = integer_sequence<T, Is...>;
94};
95
96/// @brief initial make sequence specialization
97/// @tparam T integer type
98/// @tparam N remaining number of sequence elements to generate
99///
100/// If the sequence @c Is is empty, reduce remaining by 1 and create a sequence
101/// with the zero element.
102///
103template <typename T, size_t N>
104class make_sequence_impl<T, N> : public make_sequence_impl<T, N - 1, T{}> {};
105
106/// @brief specialization if the sequence cannot be doubled
107/// @tparam T integer type
108/// @tparam DoDouble false for this primary template
109///
110template <class T, bool DoDouble>
112 public:
113 // parasoft-begin-suppress CERT_C-EXP37-a-3 "False positive: The rule does not mention naming all parameters"
114
115 /// @brief finishes the sequence since it can no longer be doubled
116 /// @tparam Is sequence head
117 /// @tparam Js sequence tail
118 /// @return helper type containing the completed sequence
119 ///
120 template <T... Is, T... Js>
122 -> make_sequence_impl<T, 0, Is..., sizeof...(Is) + Js...>;
123
124 // parasoft-end-suppress CERT_C-EXP37-a-3
125
126 /// @brief finish the current sequence
127 /// @tparam N remaining number of sequence elements to generate
128 /// @tparam Is current sequence
129 ///
130 template <size_t N, T... Is>
131 using type = decltype(finish_sequence<Is...>(typename make_sequence_impl<T, N>::type{}));
132};
133
134/// @brief specialization if the sequence can be doubled
135/// @tparam T integer type
136///
137template <class T>
138class fast_expand_sequence<T, true> {
139 public:
140 /// @brief double the current sequence
141 /// @tparam N remaining number of sequence elements to generate
142 /// @tparam Is current sequence
143 ///
144 template <size_t N, T... Is>
145 using type = make_sequence_impl<T, N - sizeof...(Is), Is..., sizeof...(Is) + Is...>;
146};
147
148/// @brief Helper variable used to determine if an integer value is negative
149/// @tparam Type integer type
150/// @tparam Value integer value to check
151///
152/// The primary template is always @c false.
153///
154template <typename Type, Type Value, typename = void>
155extern constexpr bool is_negative_v = false;
156
157/// @brief Helper variable used to used to determine if an integer value is negative
158/// @tparam Type integer type
159/// @tparam Value integer value to check
160///
161/// Template specialization for signed integer types.
162///
163/// The value is defined to be <tt>int64_t{Value} < 0</tt>. An explicit
164/// promotion to @c int64_t is used to avoid warnings of implicit conversions of
165/// smaller types (e.g. @c bool) to @c int.
166///
167template <typename Type, Type Value>
168extern constexpr bool is_negative_v<Type, Value, enable_if_t<is_signed_v<Type>>> = int64_t{Value} < 0;
169
170/// @brief Helper type to check that the requested sequence size is non-negative
171/// @tparam Type integer type
172/// @tparam Size sequence size
173///
174/// The primary template is the successful case where the size is non-negative.
175///
176template <typename Type, Type Size, bool = is_negative_v<Type, Size>>
177class make_integer_sequence : public make_sequence_impl<Type, Size> {};
178
179/// @brief Helper type to check that the requested sequence size is non-negative
180/// @tparam Type integer type
181/// @tparam Size sequence size
182///
183/// Template specialization instantiated if @c Size is a negative value.
184///
185template <typename Type, Type Size>
186class make_integer_sequence<Type, Size, true> {
187 public:
188 // parasoft-begin-suppress CERT_C-PRE31-c-3 "False positive: static_assert is
189 // a compile-time assert and can have no side-effects"
190 static_assert(!is_negative_v<Type, Size>, "'Size' must be non-negative.");
191 // parasoft-end-suppress CERT_C-PRE31-c-3
192
193 /// @brief an unused type alias provided to reduce compiler error messages
194 ///
196};
197
198} // namespace integer_sequence_detail
199
200// parasoft-begin-suppress AUTOSAR-A2_7_3-a "False positive: Declaration *is* preceeded by the @brief tag."
201/// @brief Helper alias template to simplify creation of @c std::integer_sequence types
202/// with <tt>0</tt>, <tt>1</tt>, ..., <tt>N - 1</tt> as @c Ints.
203/// @tparam Type an integer type to use for the sequence of elements
204/// @tparam Size size of the sequence
205///
206/// If @c N is negative the program is ill-formed. The alias template
207/// @c make_integer_sequence denotes a specialization of @c integer_sequence
208/// with @c N template non-type arguments. The type
209/// @c make_integer_sequence<T, N> denotes the type
210/// <tt>integer_sequence<T, 0, 1, ..., N-1></tt>.
211template <typename Type, Type Size>
213// parasoft-end-suppress AUTOSAR-A2_7_3-a "False positive: Declaration *is* preceeded by the @brief tag."
214
215/// @brief A helper alias template for <tt>integer_sequence<size_t, Ints></tt>
216/// @tparam Ints a non-type parameter pack of elements defining the sequence
217template <size_t... Ints>
219
220// parasoft-begin-suppress AUTOSAR-A2_7_3-a "False positive: there is an @brief tag."
221/// @brief A helper alias template for <tt>make_integer_sequence<size_t, Size></tt>
222/// @tparam Size size of the sequence
223template <size_t Size>
225// parasoft-end-suppress AUTOSAR-A2_7_3-a "False positive: there is an @brief tag."
226
227/// @brief A helper alias template convert any type parameter pack into an index sequence of the same length
228/// @tparam Types parameter pack of types
229template <typename... Types>
230using index_sequence_for = make_index_sequence<sizeof...(Types)>;
231
232} // namespace std
233
234#endif // INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_INTEGER_SEQUENCE_HPP_
make_sequence_impl< T, N - sizeof...(Is), Is..., sizeof...(Is)+Is... > type
double the current sequence
Definition integer_sequence.hpp:145
helper for quickly expanding the elements in a sequence
Definition integer_sequence.hpp:111
static constexpr auto finish_sequence(integer_sequence< T, Js... >) -> make_sequence_impl< T, 0, Is..., sizeof...(Is)+Js... >
finishes the sequence since it can no longer be doubled
typedef type
finish the current sequence
Definition integer_sequence.hpp:131
integer_sequence< Type, 0 > type
an unused type alias provided to reduce compiler error messages
Definition integer_sequence.hpp:195
Helper type to check that the requested sequence size is non-negative.
Definition integer_sequence.hpp:177
integer_sequence< T, Is... > type
completed integer sequence type
Definition integer_sequence.hpp:93
integer_sequence< T > type
completed integer sequence type
Definition integer_sequence.hpp:80
helper for constructing an integer sequence from 0 to N - 1
Definition integer_sequence.hpp:68
Implements a compile-time sequence of integers.
Definition integer_sequence.hpp:32
static constexpr auto size() noexcept -> size_t
Returns the number of elements in the sequence.
Definition integer_sequence.hpp:44
T value_type
Definition integer_sequence.hpp:38
Definition integer_sequence.hpp:48
constexpr bool is_negative_v
Helper variable used to determine if an integer value is negative.
STL namespace.
make_integer_sequence< size_t, Size > make_index_sequence
A helper alias template for make_integer_sequence<size_t, Size>
Definition integer_sequence.hpp:224
integer_sequence< size_t, Ints... > index_sequence
A helper alias template for integer_sequence<size_t, Ints>
Definition integer_sequence.hpp:218
::size_t size_t
A type suitable for holding the size of an object.
Definition cstddef.hpp:29
constexpr bool is_integral_v
A type trait to check if a type is integral. The value is true if Type is an integral type,...
::int64_t int64_t
A 64-bit signed integer type.
Definition cstdint.hpp:71
make_index_sequence< sizeof...(Types)> index_sequence_for
A helper alias template convert any type parameter pack into an index sequence of the same length.
Definition integer_sequence.hpp:230
typename integer_sequence_detail::make_integer_sequence< Type, Size >::type make_integer_sequence
Helper alias template to simplify creation of std::integer_sequence types with 0, 1,...
Definition integer_sequence.hpp:212