GCC Code Coverage Report


Directory: include/kumi/
File: include/kumi/utils/ct_helpers.hpp
Date: 2026-10-14 16:33:56
Exec Total Coverage
Lines: 11 11 100.0%
Functions: 79 80 98.8%
Branches: 0 0 -%

Line Branch Exec Source
1 //======================================================================================================================
2 /*
3 KUMI - Compact Tuple Tools
4 Copyright : KUMI Project Contributors
5 SPDX-License-Identifier: BSL-1.0
6 */
7 //======================================================================================================================
8 #pragma once
9
10 namespace kumi
11 {
12 namespace _
13 {
14 template<char... c> constexpr auto b10()
15 {
16 auto value = 0ULL;
17 ((value = value * 10 + (c - '0')), ...);
18 return value;
19 }
20 }
21
22 //====================================================================================================================
23 /**
24 @ingroup kumi_types
25 @class index_t
26 @brief Integral constant type
27
28 Defines a integral constant wrapper used to carry compile-time constant through API
29 **/
30 //====================================================================================================================
31 template<std::size_t N> struct index_t
32 {
33 /// Value stored by the constant
34 static constexpr auto value = N;
35
36 /// Conversion operator to integer
37 274 constexpr inline operator std::size_t() const noexcept { return N; }
38
39 /// Conversion operator to integral_constant
40 constexpr inline operator std::integral_constant<std::size_t, N>() const noexcept { return {}; }
41 };
42
43 //====================================================================================================================
44 /**
45 @ingroup kumi_utility
46 @brief Inline integral constant value for kumi::index_t
47 **/
48 //====================================================================================================================
49 template<std::size_t N> inline constexpr kumi::index_t<N> index = {};
50
51 //====================================================================================================================
52 /**
53 @ingroup kumi_types
54 @class label_t
55 @brief Literal constant type
56
57 Defines a Literal constant wrapper used to carry compile-time strings through API
58 **/
59 //====================================================================================================================
60 template<kumi::str Label> struct label_t
61 {
62 using type = decltype(Label);
63
64 /// Value stored by the constant
65 static constexpr kumi::str value = Label;
66
67 /// Conversion operator to kumi::str
68 constexpr inline operator type() const noexcept { return Label; }
69
70 template<typename CharT, typename Traits>
71 4 friend std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, label_t const&) noexcept
72 {
73 4 return os << value;
74 }
75 };
76
77 //====================================================================================================================
78 /**
79 @ingroup kumi_utility
80 @brief Inline literal constant value for kumi::label_t
81 **/
82 //====================================================================================================================
83 template<str Label> inline constexpr kumi::label_t<Label> label = {};
84
85 //====================================================================================================================
86 /**
87 @namespace literals
88 @brief KUMI literals namespace
89 **/
90 //====================================================================================================================
91 inline namespace literals
92 {
93 //==================================================================================================================
94 /**
95 @ingroup kumi_utility
96 @brief Forms a integral constant literal of the desired value.
97 @return An instance of kumi::index_t for the specified integral value
98 ## Example:
99 @include doc/infra/index.cpp
100 **/
101 //==================================================================================================================
102 1128 template<char... c> constexpr auto operator""_c() noexcept
103 {
104 1128 return kumi::index<kumi::_::b10<c...>()>;
105 }
106
107 //==================================================================================================================
108 /**
109 @ingroup kumi_utility
110 @brief Forms a constant string literal of the desired value.
111 @return An instance of kumi::name for the specified string
112 ##Example:
113 @include doc/infra/name.cpp
114 **/
115 //==================================================================================================================
116 635 template<kumi::str ID> constexpr auto operator""_id() noexcept
117 {
118 635 return kumi::name<ID>{};
119 }
120
121 //==================================================================================================================
122 /**
123 @ingroup kumi_utility
124 @brief Forms a constant string literal of the desired value.
125 @return An instance of kumi::label for the specified string
126
127 @note It differs from the ""_id operator on the type and the overload that it will pick up. An identifier
128 and a label are two different parts of a field. Label represents the value that is displayed.
129
130 ##Example:
131 @include doc/infra/label.cpp
132 **/
133 //==================================================================================================================
134 18 template<kumi::str ID> constexpr auto operator""_l() noexcept
135 {
136 18 return kumi::label<ID>;
137 }
138 }
139
140 //====================================================================================================================
141 /**
142 @ingroup kumi_utility
143 @brief Convert a unary template meta-program in a running predicate
144 @tparam Pred Unary template meta-program to convert.
145 @return A Callable Object applying Pred to the type of its arguments
146 **/
147 //====================================================================================================================
148 56 template<template<class> class Pred> [[nodiscard]] constexpr auto predicate() noexcept
149 {
150 168 return []<typename T>(T const&) constexpr { return Pred<T>::value; };
151 }
152
153 //====================================================================================================================
154 /**
155 @ingroup kumi_utility
156 @brief Helper to retrieve the index of a type in a product type by its type
157
158 @note This function does not participate in overload resolution if the product type has several instances of the
159 same type or if it is empty.
160
161 @return the index of the element of type U in the product type if it exists
162 **/
163 //====================================================================================================================
164 template<typename U, kumi::concepts::product_type T>
165 KUMI_ABI consteval auto get_index_of_type()
166 requires(kumi::concepts::queryable_by_type<U, T>)
167 {
168 return [&]<std::size_t... I>(std::index_sequence<I...>) {
169 return kumi::_::index_of_type<U, kumi::stored_element_t<I, T>...>::value;
170 }(std::make_index_sequence<kumi::size_v<T>>{});
171 }
172
173 //====================================================================================================================
174 /**
175 @ingroup kumi_utility
176 @brief Helper to retrieve the index of a type in a product type by its identifier
177
178 @note This function does not participate in overload resolution if the product type has several instances of the
179 same identifier or has no identifier at all.
180
181 @return the index of the element whose identifier matches Id in the product type if it exists
182 **/
183 //====================================================================================================================
184 template<kumi::concepts::identifier Id, kumi::concepts::product_type T>
185 KUMI_ABI consteval auto get_index_of_field()
186 requires(kumi::concepts::queryable_by_identifier<Id, T>)
187 {
188 return [&]<std::size_t... I>(std::index_sequence<I...>) {
189 return kumi::_::index_of_tag<Id, kumi::element_t<I, T>...>::value;
190 }(std::make_index_sequence<kumi::size_v<T>>{});
191 }
192
193 //====================================================================================================================
194 /**
195 @ingroup kumi_utility
196 @brief Helper to retrieve the index of a type in a product type by its identifier
197
198 @note This function does not participate in overload resolution if the product type has several instances of the
199 same identifier or has no identifier at all.
200
201 @return the index of the element whose identifier matches Id in the product type if it exists
202 **/
203 //====================================================================================================================
204 template<kumi::str L, kumi::concepts::product_type T>
205 KUMI_ABI consteval auto get_index_of_label()
206 requires(kumi::concepts::queryable_by_label<kumi::label_t<L>, T>)
207 {
208 return [&]<std::size_t... I>(std::index_sequence<I...>) {
209 return kumi::_::index_of_label<kumi::label_t<L>, kumi::element_t<I, T>...>::value;
210 }(std::make_index_sequence<kumi::size_v<T>>{});
211 }
212 }
213