Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
sequential_values.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_ARENE_BASE_INTEGER_SEQUENCES_SEQUENTIAL_VALUES_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_INTEGER_SEQUENCES_SEQUENTIAL_VALUES_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// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
15#include "arene/base/stdlib_choice/cstddef.hpp"
16#include "arene/base/stdlib_choice/enable_if.hpp"
17#include "arene/base/stdlib_choice/integer_sequence.hpp"
18
19// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
20
21namespace arene {
22namespace base {
23
24namespace sequential_values_detail {
25
26/// @brief The maximum number of elements supported by @c sequential_values and @c make_index_sequence_array
27/// On certain platforms and compilers, there is a limit to the number of elements that can be expanded in an
28/// initializer list. For the currently supported set of compilers, the lower bound of this is 2^16-1 in clang-17.
29static constexpr std::size_t max_number_of_elements{65535U};
30
31/// @brief Tests if a sequential values count is valid
32/// @tparam NumElements the number of elements desired
33/// @return true if <c> NumElements <= max_number_of_elements </c>
34/// @return false otherwise
35template <std::size_t NumElements>
36extern constexpr bool is_valid_size_v = NumElements <= max_number_of_elements;
37
38/// @brief Create an array holding the values in the provided index sequence.
39/// @tparam ElementType The type of each element
40/// @tparam Indices The values to put in the array
41/// @return An array holding the values, The array bounds is the number of indices in the sequence.
42// parasoft-begin-suppress CERT_C-EXP37-a "False positive: The rule does not mention naming all parameters"
43template <
44 typename ElementType,
45 ElementType... Indices,
46 constraints<std::enable_if_t<sequential_values_detail::is_valid_size_v<sizeof...(Indices)>>> = nullptr>
47constexpr auto make_index_sequence_array(std::integer_sequence<ElementType, Indices...>)
48 -> array<ElementType, sizeof...(Indices)> {
49 // parasoft-begin-suppress AUTOSAR-M8_5_2-a-2 "False positive: correct initialization"
50 return {Indices...};
51 // parasoft-end-suppress AUTOSAR-M8_5_2-a-2
52}
53// parasoft-end-suppress CERT_C-EXP37-a
54
55} // namespace sequential_values_detail
56
57/// @brief An array holding a sequence of integer values starting at Begin, incrementing by 1 each time.
58/// @tparam ElementType The type of each element
59/// @tparam Begin The first element in the sequence.
60/// @tparam Count The number of elements in the sequence.
61template <
62 typename ElementType,
63 ElementType Begin,
64 std::size_t Count,
66extern constexpr auto sequential_values_from = sequential_values_detail::make_index_sequence_array<ElementType>(
67 make_integer_sequence_from<ElementType, Begin, static_cast<ElementType>(Count)>{}
68);
69
70/// @brief An array holding a sequence of integer values starting at Begin, ending at End and incrementing by 1 each
71/// time.
72/// @tparam ElementType The type of each element
73/// @tparam Begin The first element in the sequence.
74/// @tparam End The last element in the sequence.
75template <
76 typename ElementType,
77 ElementType Begin,
78 ElementType End,
79 constraints<std::enable_if_t<sequential_values_detail::is_valid_size_v<std::size_t{End - Begin}>>> = nullptr>
80extern constexpr auto sequential_values_between =
82
83/// @brief An array holding a sequence of integer values starting at 0, incrementing by 1 each time.
84/// @tparam ElementType The type of each element
85/// @tparam Count The number of elements in the sequence.
86template <
87 typename ElementType,
88 std::size_t Count,
91
92} // namespace base
93} // namespace arene
94
95#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_INTEGER_SEQUENCES_SEQUENTIAL_VALUES_HPP_
Definition accumulate.hpp:37
constexpr auto sequential_values_between
An array holding a sequence of integer values starting at Begin, ending at End and incrementing by 1 ...
constexpr auto sequential_values
An array holding a sequence of integer values starting at 0, incrementing by 1 each time.
constexpr auto sequential_values_from
An array holding a sequence of integer values starting at Begin, incrementing by 1 each time.
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
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
Definition accumulate.hpp:36
::size_t size_t
A type suitable for holding the size of an object.
Definition cstddef.hpp:29
typename enable_if< Condition, Type >::type enable_if_t
Type alias to support constraining templates. Is an alias for the specified type if the Condition is ...
Definition enable_if.hpp:41