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