Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
index_of.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_TYPE_TRAITS_INDEX_OF_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TYPE_TRAITS_INDEX_OF_HPP_
7
8// IWYU pragma: private, include "arene/base/type_traits.hpp"
9// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
10
11// parasoft-begin-suppress AUTOSAR-A7_1_5-a-2 "Trailing return syntax permitted by A7-1-5 Permit #1 v1.0.0"
12
13#include "arene/base/stdlib_choice/cstddef.hpp"
14#include "arene/base/stdlib_choice/initializer_list.hpp"
15#include "arene/base/stdlib_choice/integral_constant.hpp"
16#include "arene/base/stdlib_choice/is_same.hpp"
17#include "arene/base/type_traits/conditional.hpp"
18
19namespace arene {
20namespace base {
21namespace index_of_detail {
22
23/// @brief Function to find the first true element in the list
24/// @param values The list of elements to search
25/// @return The index, or @c values.size() if not found
26// parasoft-begin-suppress AUTOSAR-A2_7_3 "False positive: documented"
27constexpr auto index_of_first_true(std::initializer_list<bool> values) noexcept -> std::size_t {
28 std::size_t idx{0U};
29 // parasoft-begin-suppress CERT_C-INT36-b-3 "False positive: const bool* not converted to integer"
30 for (bool const value : values) {
31 // parasoft-end-suppress CERT_C-INT36-b-3
32 if (value) {
33 return idx;
34 }
35 ++idx;
36 }
37 return idx;
38}
39// parasoft-end-suppress AUTOSAR-A2_7_3
40
41/// @brief Function to find the last true element in the list
42/// @param values The list of elements to search
43/// @return The index, or @c values.size() if not found
44// parasoft-begin-suppress AUTOSAR-A2_7_3 "False positive: documented"
45constexpr auto index_of_last_true(std::initializer_list<bool> const values) noexcept -> std::size_t {
46 // std::initializer_list has no operator[] but is contiguous so we can use its begin() pointer to index into it.
47 bool const* const ptr{values.begin()};
48 std::size_t idx{values.size()};
49 while (idx != 0U) {
50 --idx;
51 // parasoft-begin-suppress AUTOSAR-M5_0_15-a "Either indexing or pointer arithmetic is required to iterate backward"
52 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
53 if (ptr[idx]) {
54 return idx;
55 }
56 // parasoft-end-suppress AUTOSAR-M5_0_15-a
57 }
58 return values.size();
59}
60// parasoft-end-suppress AUTOSAR-A2_7_3
61
62/// @brief Template meta-function for querying the 0-based index of @c T within
63/// a list of types, @c Ts... . It returns @c sizeof...(Ts) if the type is not
64/// in the list.
65///
66/// @tparam T The type to search for.
67/// @tparam Ts The list of types to search in.
68template <typename T, typename... Ts>
69constexpr std::size_t type_index_of_v = index_of_first_true({std::is_same<T, Ts>::value...});
70
71/// @brief Template meta-function for querying the 0-based index of @c T within
72/// a list of types, @c Ts... , starting from the end and searching backwards.
73/// It returns @c sizeof...(Ts) if the type is not in the list.
74///
75/// @tparam T The type to search for.
76/// @tparam Ts The list of types to search in.
77template <typename T, typename... Ts>
78constexpr std::size_t last_type_index_of_v = index_of_last_true({std::is_same<T, Ts>::value...});
79
80/// @brief Base class with no @c type member for @c index_of
81// parasoft-begin-suppress AUTOSAR-A2_7_3 "False positive: documented"
82class empty_index_of_base {};
83// parasoft-end-suppress AUTOSAR-A2_7_3
84
85} // namespace index_of_detail
86
87/// @brief Template meta-function for querying the 0-based index of a type in a list of types.
88///
89/// @tparam T The type to search for.
90/// @tparam Ts The list of types to search in.
91///
92/// If @c T is found within @c Ts... then this type inherits from <c>std::integral_constant<std::size_t, Idx></c> where
93/// @c Idx is the zero-based index of the first occurrence of @c T in @c Ts...
94///
95/// Access the result using the @c value member of this type. This type will not have a @c value member if @c T is not
96/// one of the types listed in @c Ts....
97// parasoft-begin-suppress AUTOSAR-A2_7_3 "False positive: documented"
98template <typename T, typename... Ts>
104// parasoft-end-suppress AUTOSAR-A2_7_3
105
106/// @brief Template meta-function for querying the 0-based index of 'T' within
107/// a list of types, 'Ts...'.
108///
109/// The instantiation will be ill-formed if T is not a member of Ts...
110///
111/// @tparam T The type to search for.
112/// @tparam Ts The list of types to search in.
113template <typename T, typename... Ts>
114extern constexpr auto index_of_v = index_of<T, Ts...>::value;
115
116/// @brief Template meta-function for querying the last 0-based index of a type in a list of types.
117///
118/// @tparam T The type to search for.
119/// @tparam Ts The list of types to search in.
120///
121/// If @c T is found within @c Ts... then this type inherits from <c>std::integral_constant<std::size_t, Idx></c> where
122/// @c Idx is the zero-based index of the last occurrence of @c T in @c Ts...
123///
124/// Access the result using the @c value member of this type. This type will not have a @c value member if @c T is not
125/// one of the types listed in @c Ts....
126// parasoft-begin-suppress AUTOSAR-A2_7_3 "False positive: documented"
127template <typename T, typename... Ts>
133// parasoft-end-suppress AUTOSAR-A2_7_3
134
135/// @brief Template meta-function for querying the last 0-based index of 'T' within a list of types, 'Ts...'.
136///
137/// The instantiation will be ill-formed if T is not a member of Ts...
138///
139/// @tparam T The type to search for.
140/// @tparam Ts The list of types to search in.
141template <typename T, typename... Ts>
142extern constexpr auto last_index_of_v = last_index_of<T, Ts...>::value;
143
144} // namespace base
145} // namespace arene
146
147#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TYPE_TRAITS_INDEX_OF_HPP_
Template meta-function for querying the 0-based index of a type in a list of types.
Definition index_of.hpp:103
Template meta-function for querying the last 0-based index of a type in a list of types.
Definition index_of.hpp:132
Definition array_exceptions_disabled.cpp:11
constexpr auto last_index_of_v
Template meta-function for querying the last 0-based index of 'T' within a list of types,...
constexpr auto index_of_v
Template meta-function for querying the 0-based index of 'T' within a list of types,...
Copyright 2026, Toyota Motor Corporation.
Definition array_exceptions_disabled.cpp:10