Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
size.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_LIST_SIZE_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TYPE_LIST_SIZE_HPP_
7
8// IWYU pragma: private, include "arene/base/type_list.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"
12#include "arene/base/compiler_support/attributes.hpp"
13#include "arene/base/constraints/constraints.hpp"
14#include "arene/base/stdlib_choice/cstddef.hpp"
15#include "arene/base/stdlib_choice/enable_if.hpp"
16#include "arene/base/stdlib_choice/integral_constant.hpp"
17#include "arene/base/stdlib_choice/tuple_size.hpp"
18#include "arene/base/type_list/type_list.hpp"
19// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
20
21// parasoft-begin-suppress AUTOSAR-A14_7_2-a-2 "False positive: 'type_list' is defined in type_list.hpp included above"
22/// @brief Specialize @c tuple_size for @c type_list
23template <class... Tn>
24class std::tuple_size<arene::base::type_list<Tn...>> : public std::integral_constant<std::size_t, sizeof...(Tn)> {};
25// parasoft-end-suppress AUTOSAR-A14_7_2-a-2
26
27namespace arene {
28namespace base {
29
30namespace type_lists {
31
32/// @cond INTERNAL
33
34namespace size_detail {
35
36/// @brief Check for specialization of @c std::tuple_size<>.
37/// @tparam T The type to check.
38template <class T, class = constraints<>>
39class has_tuple_size_impl : public std::false_type {};
40
41/// @brief Check for specialization of @c std::tuple_size<>.
42/// @tparam T The type to check.
43template <class T>
44class has_tuple_size_impl<T, constraints<decltype(std::tuple_size<T>::value)>> : public std::true_type {};
45
46/// @brief Get the count of elements in a type-list.
47/// @tparam T The type-list for which to get the count of elements.
48template <class T, class = constraints<>>
49class size_impl;
50
51/// @brief Get the count of elements in a type-list.
52/// @tparam L0 The type-list template
53/// @tparam Tn The types in the list
54template <template <class...> class L0, class... Tn>
55class size_impl<
56 L0<Tn...>,
57 constraints< //
58 std::enable_if_t<!has_tuple_size_impl<L0<Tn...>>::value>>>
59 : public std::integral_constant<std::size_t, sizeof...(Tn)> {};
60
61/// @brief Get the count of elements in a type-list that has a @c tuple_size specialization.
62/// @tparam T The type-list for which to get the count of elements.
63template <class T>
64class size_impl<
65 T,
66 constraints< //
67 std::enable_if_t<has_tuple_size_impl<T>::value>>> : public std::tuple_size<T> {};
68
69} // namespace size_detail
70
71/// @endcond
72
73/// @brief Get the size of the type list.
74///
75/// @tparam L0 An instantiation of a type-list that holds the types to count
76/// @pre @c L0 is a type-list
77/// @return The size of the type list.
78template <class L0>
79// parasoft-begin-suppress AUTOSAR-M3_3_2-a "False positive: inline function used in multiple translation units"
80constexpr auto size() -> std::size_t {
81 return arene::base::type_lists::size_detail::size_impl<L0>::value;
82}
83// parasoft-end-suppress AUTOSAR-M3_3_2-a
84
85/// @brief Get the size of the type list.
86///
87/// @tparam L0 An instantiation of a type-list that holds the types to count
88/// @pre @c L0 is a type-list
89/// @return The size of the type list.
90template <class L0>
91ARENE_MAYBE_UNUSED extern constexpr std::size_t size_v{size<L0>()};
92
93// parasoft-begin-suppress AUTOSAR-M17_0_2-a-2 "False positive: size_t not reserved in this context"
94// parasoft-begin-suppress CERT_CPP-DCL51-f-3 "False positive: size_t not reserved in this context"
95/// @brief Get the size of the type list.
96///
97/// @tparam L0 An instantiation of a type-list that holds the types to count
98/// @pre @c L0 is a type-list
99/// @return The size of the type list.
100template <class L0>
102// parasoft-end-suppress AUTOSAR-M17_0_2-a-2
103// parasoft-end-suppress CERT_CPP-DCL51-f-3
104
105/// @brief Get whether the type list is empty.
106///
107/// @tparam L0 An instantiation of a type-list that holds the types to count
108/// @pre @c L0 is a type-list
109/// @return True if the type list is empty.
110template <class L0>
111constexpr auto empty() -> bool {
112 return arene::base::type_lists::size_v<L0> == 0UL;
113}
114
115/// @brief Get whether the type list is empty.
116///
117/// @tparam L0 An instantiation of a type-list that holds the types to count
118/// @pre @c L0 is a type-list
119/// @return True if the type list is empty.
120template <class L0>
121ARENE_MAYBE_UNUSED extern constexpr bool empty_v{empty<L0>()};
122
123/// @brief Get whether the type list is empty.
124///
125/// @tparam L0 An instantiation of a type-list that holds the types to count
126/// @pre @c L0 is a type-list
127/// @return True if the type list is empty.
128template <class L0>
129struct empty_t {
130 // parasoft-begin-suppress CERT_CPP-DCL56-a-3 "False positive: variable is initialized"
131 // parasoft-begin-suppress AUTOSAR-A3_3_2-a-2 "False positive: initializer is constant"
132 /// @brief The Result of the check
133 static constexpr bool value{empty_v<L0>};
134 // parasoft-end-suppress AUTOSAR-A3_3_2-a-2
135 // parasoft-end-suppress CERT_CPP-DCL56-a-3
136};
137
138} // namespace type_lists
139} // namespace base
140} // namespace arene
141
142#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TYPE_LIST_SIZE_HPP_
Definition apply_all.hpp:14
constexpr auto size() -> std::size_t
Get the size of the type list.
Definition size.hpp:80
ARENE_MAYBE_UNUSED constexpr bool empty_v
Get whether the type list is empty.
constexpr auto empty() -> bool
Get whether the type list is empty.
Definition size.hpp:111
Definition array_exceptions_disabled.cpp:11
Copyright 2026, Toyota Motor Corporation.
Definition array_exceptions_disabled.cpp:10
Get whether the type list is empty.
Definition size.hpp:129
static constexpr bool value
The Result of the check.
Definition size.hpp:133