Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
make_integer_sequence.hpp
Go to the documentation of this file.
1// Copyright 2026, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_INTEGER_SEQUENCES_MAKE_INTEGER_SEQUENCE_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_INTEGER_SEQUENCES_MAKE_INTEGER_SEQUENCE_HPP_
7
8// IWYU pragma: private, include "arene/base/integer_sequences.hpp"
9// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
10
11#include "arene/base/stdlib_choice/cstddef.hpp"
12#include "arene/base/stdlib_choice/integer_sequence.hpp"
13#include "arene/base/stdlib_choice/numeric_limits.hpp"
14
15namespace arene {
16namespace base {
17
18namespace make_integer_sequence_detail {
19
20// parasoft-begin-suppress CERT_C-EXP37-a "False positive: The rule does not mention naming all parameters"
21/// @brief Helper for creating an integer_sequence starting from a particular value
22/// @tparam Type the integer type
23/// @tparam Begin the starting value of the sequence
24/// @tparam Is the values to increment by
25/// @return The integer sequence starting at @c Begin
26template <typename Type, Type Begin, std::size_t... Is>
27auto impl(std::index_sequence<Is...>) -> std::integer_sequence<Type, (Begin + Type{Is})...>;
28// parasoft-end-suppress CERT_C-EXP37-a
29
30/// @brief Helper for checking if the specified @c Begin and @c Size are valid for a given @c Type
31/// @tparam Type the integer type
32/// @tparam Begin the starting value of the sequence
33/// @tparam Size The size of the desired sequence
34template <typename Type, Type Begin, Type Size, bool = (Size > Type{})>
35extern constexpr bool is_valid_range_v{Size == Type{}};
36
37/// @brief Helper for checking if the specified @c Begin and @c Size are valid for a given @c Type
38/// @tparam Type the integer type
39/// @tparam Begin the starting value of the sequence
40/// @tparam Size The size of the desired sequence
41template <typename Type, Type Begin, Type Size>
42extern constexpr bool is_valid_range_v<Type, Begin, Size, true>{
43 Begin <= (std::numeric_limits<Type>::max() - (Size - 1))
44};
45
46/// @brief Implementation of make_integer_sequence_from
47/// @tparam Type the integer type
48/// @tparam Begin the starting value of the sequence
49/// @tparam Size the size of the sequence
50template <typename Type, Type Begin, Type Size, bool = is_valid_range_v<Type, Begin, Size>>
51struct make_from_impl {
52 static_assert(Size >= Type{}, "Size must be non-negative");
53
54 /// @brief The maximum value of @c Type
55 static constexpr auto max = std::numeric_limits<Type>::max();
56
57 static_assert(
58 (Size == 0) || (Begin <= (max - (Size - 1))),
59 "Sequence with the given Begin and Size would overflow the representable range of Type"
60 );
61
62 /// @brief The resulting integer sequence type
63 using type = decltype(make_integer_sequence_detail::impl<Type, Begin>(std::make_index_sequence<Size>{}));
64};
65
66/// @brief Implementation of make_integer_sequence_from (precondition not satisfied)
67/// @tparam Type the integer type
68/// @tparam Begin the starting value of the sequence
69/// @tparam Size the size of the sequence
70template <typename Type, Type Begin, Type Size>
71struct make_from_impl<Type, Begin, Size, false> {
72 static_assert(Size >= Type{}, "Size must be non-negative");
73
74 /// @brief The maximum value of @c Type
75 static constexpr auto max = std::numeric_limits<Type>::max();
76
77 static_assert(
78 (Size == 0) || (Begin <= (max - (Size - 1))),
79 "Sequence with the given Begin and Size would overflow the representable range of Type"
80 );
81};
82
83} // namespace make_integer_sequence_detail
84
85/// @brief Create an integer_sequence starting from a particular value
86/// @tparam Type the integer type
87/// @tparam Begin the starting value of the sequence
88/// @tparam Size the size of the sequence
89/// @pre Requires that the sequence does not overflow the maximum value representable by @c Type
90/// @pre Requires <c> Size >= 0 </c>
91template <typename Type, Type Begin, Type Size>
92using make_integer_sequence_from = typename make_integer_sequence_detail::make_from_impl<Type, Begin, Size>::type;
93
94namespace make_integer_sequence_detail {
95
96/// @brief Implementation of make_integer_sequence_between
97/// @tparam Type the integer type
98/// @tparam Begin the starting value of the sequence
99/// @tparam End the ending value of the sequence (not included)
100template <typename Type, Type Begin, Type End, bool = Begin <= End>
101struct make_between_impl {
102 /// @brief The resulting integer sequence type
103 using type = make_integer_sequence_from<Type, Begin, Type{End - Begin}>;
104};
105
106/// @brief Implementation of make_integer_sequence_between (precondition not satisfied)
107/// @tparam Type the integer type
108/// @tparam Begin the starting value of the sequence
109/// @tparam End the ending value of the sequence (not included)
110template <typename Type, Type Begin, Type End>
111struct make_between_impl<Type, Begin, End, false> {
112 static_assert(Begin <= End, "Precondition Begin <= End not satisfied");
113};
114
115} // namespace make_integer_sequence_detail
116
117/// @brief Create an integer_sequence between two values [Begin, End)
118/// @tparam Type the integer type
119/// @tparam Begin the starting value of the sequence
120/// @tparam End the ending value of the sequence (not included)
121/// @pre Requires that <tt>End >= Begin</tt>
122template <typename Type, Type Begin, Type End>
123using make_integer_sequence_between = typename make_integer_sequence_detail::make_between_impl<Type, Begin, End>::type;
124
125/// @brief A helper alias template for <tt>make_integer_sequence_from<size_t, Begin, Size></tt>
126/// @tparam Begin the starting value of the sequence
127/// @tparam Size the size of the sequence
128/// @pre Requires that the sequence does not overflow the maximum value representable by @c size_t
129template <std::size_t Begin, std::size_t Size>
131
132/// @brief A helper alias template for <tt>make_integer_sequence_between<size_t, Begin, End></tt>
133/// @tparam Begin the starting value of the sequence
134/// @tparam End the ending value of the sequence (not included)
135/// @pre Requires that <tt>End >= Begin</tt>
136template <std::size_t Begin, std::size_t End>
138
139} // namespace base
140} // namespace arene
141
142#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_INTEGER_SEQUENCES_MAKE_INTEGER_SEQUENCE_HPP_
static constexpr auto max() noexcept -> T
The maximum finite value.
Definition numeric_limits.hpp:230
Definition accumulate.hpp:37
make_integer_sequence_from< std::size_t, Begin, Size > make_index_sequence_from
A helper alias template for make_integer_sequence_from<size_t, Begin, Size>
Definition make_integer_sequence.hpp:130
typename make_integer_sequence_detail::make_between_impl< Type, Begin, End >::type make_integer_sequence_between
Create an integer_sequence between two values [Begin, End)
Definition make_integer_sequence.hpp:123
typename make_integer_sequence_detail::make_from_impl< Type, Begin, Size >::type make_integer_sequence_from
Create an integer_sequence starting from a particular value.
Definition make_integer_sequence.hpp:92
make_integer_sequence_between< std::size_t, Begin, End > make_index_sequence_between
A helper alias template for make_integer_sequence_between<size_t, Begin, End>
Definition make_integer_sequence.hpp:137
Definition accumulate.hpp:36
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