Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
scaled_unit.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_UNITS_SCALED_UNIT_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_UNITS_SCALED_UNIT_HPP_
7
8// parasoft-begin-suppress AUTOSAR-A7_1_5-a-2 "Trailing return syntax permitted by A7-1-5 Permit #1 v1.0.0"
9
12#include "arene/base/stdlib_choice/cstdint.hpp"
13#include "arene/base/stdlib_choice/enable_if.hpp"
14#include "arene/base/stdlib_choice/initializer_list.hpp"
15#include "arene/base/stdlib_choice/is_floating_point.hpp"
16#include "arene/base/stdlib_choice/ratio.hpp"
24
25namespace arene {
26namespace base {
27
28namespace scaled_unit_detail {
29
30/// @brief Helper trait to check if the specified type is an instance of @c std::ratio
31/// @tparam Type the type to check
32template <typename Type>
33extern constexpr bool is_ratio_v = false;
34
35/// @brief Helper trait to check if the specified type is an instance of @c std::ratio
36/// @tparam Numerator the numerator of the ratio
37/// @tparam Denominator the denominator of the ratio
38template <std::intmax_t Numerator, std::intmax_t Denominator>
39extern constexpr bool is_ratio_v<std::ratio<Numerator, Denominator>> = true;
40
41/// @brief Helper trait to check if the specified type is an instance of @c std::ratio equivalent to 1
42/// @tparam Type the type to check
43template <typename Type, typename = constraints<>>
44extern constexpr bool is_identity_ratio_v = false;
45
46/// @brief Helper trait to check if the specified type is an instance of @c std::ratio equivalent to 1
47/// @tparam Type the type to check
48template <typename Type>
49extern constexpr bool is_identity_ratio_v<Type, constraints<std::enable_if_t<is_ratio_v<Type>>>> =
50 (Type::num == Type::den);
51
52/// @brief Helper trait to check for a valid floating point wrapper scale: the @c Type must be a class with a public
53/// floating point @c constexpr @c static data member named @c value that is not infinity, NaN, zero or subnormal
54/// @tparam Type the type to check
55template <typename Type, typename = constraints<>>
56extern constexpr bool is_floating_point_wrapper_scale_v = false;
57
58/// @brief Helper trait to check for a valid floating point wrapper scale: the @c Type must be a class with a public
59/// floating point @c constexpr @c static data member named @c value that is not infinity, NaN, zero or subnormal
60/// @tparam Type the type to check
61template <typename Type>
62extern constexpr bool is_floating_point_wrapper_scale_v<
63 Type,
65 std::enable_if_t<std::is_floating_point<decltype(Type::value)>::value>,
66 std::enable_if_t<isnormal(Type::value)>>> = true;
67
68/// @brief Helper trait to check for a valid scale: either a ratio or a valid floating point wrapper
69/// @tparam Type the type to check
70template <typename Type>
71extern constexpr bool is_valid_scale_v = is_floating_point_wrapper_scale_v<Type> || is_ratio_v<Type>;
72
73/// @brief Implementation class that represents the inverse of a floating point wrapper scale
74/// @tparam Type the type to invert
75template <typename Type>
76class inverse_unit_scale {
77 public:
78 // parasoft-begin-suppress AUTOSAR-M11_0_1-a "This member is the public API"
79 /// @brief The resulting scale factor
80 static constexpr double value{1. / Type::value};
81 // parasoft-end-suppress AUTOSAR-M11_0_1-a
82};
83
84/// @brief Helper class to select the implementation of an inverse of a scale
85/// @tparam Type the type to invert
86template <typename Type, typename = constraints<>>
87class inverse_unit_scale_helper {
88 static_assert(is_floating_point_wrapper_scale_v<Type>, "The type being inverted must be a floating point wrapper");
89
90 public:
91 /// @brief A type representing the inverse scale
92 using type = inverse_unit_scale<Type>;
93};
94
95/// @brief Implementation class for a quantity unit that is another unit scaled by some scale factor
96/// @tparam BaseUnit the underlying unit
97/// @tparam Scale the scale factor
98template <typename BaseUnit, typename Scale>
99class scaled_unit_impl : public quantity_unit<scaled_unit_impl<BaseUnit, Scale>, typename BaseUnit::unit_kind_type> {
100 public:
101 /// @brief The scale factor
102 using scale_factor = Scale;
103 /// @brief Denotes that quantity kind is implicit and does not mark a
104 /// boundary when aggregating and simplifying quantity kind types.
105 using is_implicit = void;
106};
107
108/// @brief Implementation class determining the representation type for a quantity unit that is another unit scaled by
109/// some scale factor
110/// @tparam BaseUnit the underlying unit
111/// @tparam Scale the scale factor
112template <typename BaseUnit, typename Scale, typename = constraints<>>
113class scaled_unit_helper {
114 static_assert(is_unit_v<BaseUnit>, "The supplied BaseUnit must be a valid quantity unit");
115 static_assert(is_valid_scale_v<Scale>, "The scale factor must be a ratio or a class wrapping a floating point scale");
116
117 public:
118 /// @brief The type to use to represent the scaled unit
119 using type = scaled_unit_impl<BaseUnit, Scale>;
120};
121
122} // namespace scaled_unit_detail
123
124/// @brief Type alias for a quantity unit that is another unit scaled by some scale factor
125/// @tparam BaseUnit the underlying unit
126/// @tparam Scale the scale factor
127template <typename BaseUnit, typename Scale>
128using scaled_unit = typename scaled_unit_detail::scaled_unit_helper<BaseUnit, Scale>::type;
129
130/// @brief A type that represents the inverse of a scale factor
131/// @tparam Type the type to invert
132template <typename Type>
133using inverse_unit_scale_t = typename scaled_unit_detail::inverse_unit_scale_helper<Type>::type;
134
135namespace scaled_unit_detail {
136
137// parasoft-begin-suppress AUTOSAR-M3_3_2-a "False positive: inline function used in multiple translation units"
138/// @brief Helper function to get the scale value from a class wrapping a floating point scale
139/// @tparam Type the class wrapping the scale
140/// @return The scale factor
141template <typename Type, constraints<std::enable_if_t<std::is_floating_point<decltype(Type::value)>::value>> = nullptr>
142constexpr auto scale_value() noexcept -> double {
143 return Type::value;
144}
145// parasoft-end-suppress AUTOSAR-M3_3_2-a
146
147/// @brief Helper function to get the scale value from a ratio
148/// @tparam Type the ratio
149/// @return The scale factor
150template <typename Type, constraints<std::enable_if_t<is_ratio_v<Type>>> = nullptr>
151constexpr auto scale_value() noexcept -> double {
152 return static_cast<double>(Type::num) / static_cast<double>(Type::den);
153}
154
155/// @brief Helper function to get the resulting scale factor from a combination of scale factors
156/// @param values the scale factors being combined
157/// @return The combined scale factor
158inline constexpr auto combined_scale_value(std::initializer_list<double> values) noexcept -> double {
159 double scale{1.};
160 for (auto const elem : values) {
161 // Multiply all the scale factors together
162 scale *= elem;
163 }
164 return scale;
165}
166
167/// @brief A template representing a scale factor combined from multiple individual scale factors
168/// @tparam Scales the scale factors that are combined
169template <typename... Scales>
170class combined_scales {
171 public:
172 // parasoft-begin-suppress AUTOSAR-M11_0_1-a "This member is the public API"
173 /// @brief The numeric value of the combined scale factor
174 static constexpr double value{combined_scale_value({scale_value<Scales>()...})};
175 // parasoft-end-suppress AUTOSAR-M11_0_1-a
176};
177
178/// @brief Helper template for canonicalizing the result of combining scale factors, so empty lists are represented as
179/// an identity ratio, single element lists are represented as the list element, and multi-element lists have a
180/// consistent order
181/// @tparam Scales the scale factors being combined
182template <typename Scales>
183class canonicalize_scale {
184 public:
185 /// @brief The resulting scale factor type
186 using type = Scales;
187};
188
189/// @brief Helper template for canonicalizing the result of combining scale factors, so empty lists are represented as
190/// an identity ratio, single element lists are represented as the list element, and multi-element lists have a
191/// consistent order
192template <>
193class canonicalize_scale<combined_scales<>> {
194 public:
195 /// @brief The resulting scale factor type
196 using type = std::ratio<1, 1>;
197};
198
199/// @brief Helper template for canonicalizing the result of combining scale factors, so empty lists are represented as
200/// an identity ratio, single element lists are represented as the list element, and multi-element lists have a
201/// consistent order
202/// @tparam Scale the solitary scale factor in the list
203template <typename Scale>
204class canonicalize_scale<combined_scales<Scale>> {
205 public:
206 /// @brief The resulting scale factor type
207 using type = Scale;
208};
209
210/// @brief Helper template that compares two scale factors to determine which should be ordered before the other when
211/// combining.
212///
213/// There should be at most one ratio in a list being sorted with this predicate. The resulting sorted order places the
214/// ratio (if there is one) first, followed by the other entries in order of their type name.
215/// @tparam LhsScale The left-hand scale being compared
216/// @tparam RhsScale The right-hand scale being compared
217template <typename LhsScale, typename RhsScale>
218class scale_sorts_before {
219 public:
220 // parasoft-begin-suppress AUTOSAR-M11_0_1-a "This member is the public API"
221 // parasoft-begin-suppress CERT_CPP-DCL56-a "False positive: type_name_v is initialized"
222 /// @brief The result of the comparison
223 static constexpr bool value{
224 !is_ratio_v<RhsScale> && (is_ratio_v<LhsScale> || (type_name_v<LhsScale> < type_name_v<RhsScale>))
225 };
226 // parasoft-end-suppress CERT_CPP-DCL56-a
227 // parasoft-end-suppress AUTOSAR-M11_0_1-a
228};
229
230/// @brief Helper template for canonicalizing the result of combining scale factors, so empty lists are represented as
231/// an identity ratio, single element lists are represented as the list element, and multi-element lists have a
232/// consistent order
233///
234/// The ratio goes first if there is one, then the rest, ordered by type name
235/// @tparam Scales the combined scale factors
236template <typename... Scales>
237class canonicalize_scale<combined_scales<Scales...>> {
238 public:
239 /// @brief The resulting scale factor type
240 using type = type_lists::sort_t<combined_scales<Scales...>, scale_sorts_before>;
241};
242
243/// @brief Helper class for combining two scales
244/// @tparam Scale1 the first scale to combine
245/// @tparam Scale2 the second scale to combine
246template <typename Scale1, typename Scale2, typename = constraints<>>
247class combine_scales_helper {
248 public:
249 /// @brief The resulting scale factor type
250 using type = combined_scales<Scale1, Scale2>;
251};
252
253/// @brief Type alias for combining two scales. The result is a type that represents the product of the two scales
254/// @tparam Scale1 the first scale to combine
255/// @tparam Scale2 the second scale to combine
256template <typename Scale1, typename Scale2>
257using combine_scales_t = typename canonicalize_scale<typename combine_scales_helper<Scale1, Scale2>::type>::type;
258
259/// @brief Helper class for combining two scales. The result is a type that represents the product of the two scales
260/// @tparam Scale1 the first scale to combine
261/// @tparam Scale2 the second scale to combine
262///
263/// @details This is where they are both ratios; the result is equivalent to <c>std::ratio_multiply<Scale1,Scale2></c>
264template <typename Scale1, typename Scale2>
265class combine_scales_helper<
266 Scale1,
267 Scale2,
268 constraints<std::enable_if_t<is_ratio_v<Scale1>>, std::enable_if_t<is_ratio_v<Scale2>>>> {
269 public:
270 /// @brief The resulting scale factor type
272};
273
274/// @brief Helper class for combining two scales. The result is a type that represents the product of the two scales
275/// @tparam Scale1 the first scale to combine
276/// @tparam Scale2 the second scale to combine
277///
278/// @details This is where the second is a ratio, but not the first: other code assumes ratios are first, so switch the
279/// order.
280template <typename Scale1, typename Scale2>
281class combine_scales_helper<
282 Scale1,
283 Scale2,
284 constraints<std::enable_if_t<!is_ratio_v<Scale1>>, std::enable_if_t<is_ratio_v<Scale2>>>> {
285 public:
286 /// @brief The resulting scale factor type
287 using type = combine_scales_t<Scale2, Scale1>;
288};
289
290/// @brief Helper class for combining two scales. The result is a type that represents the product of the two scales
291/// @tparam Scales1 the individual elements of the first scale to combine
292/// @tparam Scale2 the second scale to combine
293///
294/// @details This is where the first is already a combined scale
295template <typename... Scales1, typename Scale2>
296class combine_scales_helper<
297 combined_scales<Scales1...>,
298 Scale2,
300 std::enable_if_t<!is_ratio_v<Scale2>>,
301 std::enable_if_t<!is_one_of_v<inverse_unit_scale_t<Scale2>, Scales1...>>>> {
302 public:
303 /// @brief The resulting scale factor type
304 using type = combined_scales<Scales1..., Scale2>;
305};
306
307/// @brief Helper class for combining two scales. The result is a type that represents the product of the two scales
308/// @tparam Scales1 the individual elements of the first scale to combine
309/// @tparam Scale2 the second scale to combine
310///
311/// @details This is where the first is already a combined scale and the second is an inverse of one of the elements
312template <typename... Scales1, typename Scale2>
313class combine_scales_helper<
314 combined_scales<Scales1...>,
315 Scale2,
317 std::enable_if_t<!is_ratio_v<Scale2>>,
318 std::enable_if_t<is_one_of_v<inverse_unit_scale_t<Scale2>, Scales1...>>>> {
319 public:
320 /// @brief The resulting scale factor type
321 using type = type_lists::remove_nth_t<
322 combined_scales<Scales1...>,
323 type_lists::index_of_v<combined_scales<Scales1...>, inverse_unit_scale_t<Scale2>>>;
324};
325
326/// @brief Helper class for combining two scales. The result is a type that represents the product of the two scales
327/// @tparam Scale1 the first scale to combine
328/// @tparam FirstScale2 the first element of the second scale to combine
329/// @tparam OtherScales2 the remaining elements of the second scale to combine
330///
331/// @details This is where the first is a ratio, and the second does not have a ratio element
332template <typename Scale1, typename FirstScale2, typename... OtherScales2>
333class combine_scales_helper<
334 Scale1,
335 combined_scales<FirstScale2, OtherScales2...>,
337 std::enable_if_t<is_ratio_v<Scale1>>,
338 std::enable_if_t<!is_identity_ratio_v<Scale1>>,
339 std::enable_if_t<!is_ratio_v<FirstScale2>>>> {
340 public:
341 /// @brief The resulting scale factor type
342 using type = combined_scales<Scale1, FirstScale2, OtherScales2...>;
343};
344
345/// @brief Helper class for combining two scales. The result is a type that represents the product of the two scales
346/// @tparam Scale1 the first scale to combine
347/// @tparam FirstScale2 the first element of the second scale to combine
348/// @tparam OtherScales2 the remaining elements of the second scale to combine
349///
350/// @details This is where the first is a ratio, and the second has a ratio element
351template <typename Scale1, typename FirstScale2, typename... OtherScales2>
352class combine_scales_helper<
353 Scale1,
354 combined_scales<FirstScale2, OtherScales2...>,
355 constraints<std::enable_if_t<is_ratio_v<Scale1>>, std::enable_if_t<is_ratio_v<FirstScale2>>>> {
356 public:
357 /// @brief The resulting scale factor type
358 using type = combine_scales_t<std::ratio_multiply<Scale1, FirstScale2>, combined_scales<OtherScales2...>>;
359};
360
361/// @brief Helper class for combining two scales. The result is a type that represents the product of the two scales
362/// @tparam Scale1 the first scale to combine
363/// @tparam Scales2 the elements of the second scale to combine
364///
365/// @details This is where the first is an identity ratio, and the second is a combined scale
366template <typename Scale1, typename... Scales2>
367class combine_scales_helper<
368 Scale1,
369 combined_scales<Scales2...>,
370 constraints<std::enable_if_t<is_identity_ratio_v<Scale1>>>> {
371 public:
372 /// @brief The resulting scale factor type
373 using type = combined_scales<Scales2...>;
374};
375
376/// @brief Implementation class for a quantity unit that is another unit scaled by some scale factor
377/// @tparam BaseUnit the underlying unit
378/// @tparam BaseScale the existing scale factor
379/// @tparam Scale the additional scale factor
380template <typename BaseUnit, typename BaseScale, typename Scale>
381class scaled_unit_helper<
382 scaled_unit_impl<BaseUnit, BaseScale>,
383 Scale,
384 constraints<std::enable_if_t<!is_identity_ratio_v<Scale>>>> {
385 static_assert(is_valid_scale_v<Scale>, "The scale factor must be a ratio or a class wrapping a floating point scale");
386
387 public:
388 /// @brief The type to use to represent the scaled unit
390};
391
392/// @brief Implementation class for a quantity unit that is another unit scaled by some scale factor
393/// @tparam BaseUnit the underlying unit
394/// @tparam Scale the scale factor
395///
396/// This is for the case that the @c Scale is a ratio of 1
397template <typename BaseUnit, typename Scale>
398class scaled_unit_helper<BaseUnit, Scale, constraints<std::enable_if_t<is_identity_ratio_v<Scale>>>> {
399 static_assert(is_unit_v<BaseUnit>, "The supplied BaseUnit must be a valid quantity unit");
400 static_assert(is_valid_scale_v<Scale>, "The scale factor must be a ratio or a class wrapping a floating point scale");
401
402 public:
403 /// @brief The type to use to represent the scaled unit
404 using type = BaseUnit;
405};
406
407/// @brief Helper class to select the implementation of an inverse of a scale
408/// @tparam Type the type to invert
409template <typename Type>
410class inverse_unit_scale_helper<Type, constraints<std::enable_if_t<is_ratio_v<Type>>>> {
411 public:
412 /// @brief A type representing the inverse scale
414};
415
416/// @brief Helper class to select the implementation of an inverse of a scale
417/// @tparam Type the type to invert
418template <typename Type>
419class inverse_unit_scale_helper<inverse_unit_scale<Type>> {
420 public:
421 /// @brief A type representing the inverse scale
422 using type = Type;
423};
424
425} // namespace scaled_unit_detail
426
427} // namespace base
428} // namespace arene
429
430#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_UNITS_SCALED_UNIT_HPP_
Implementation of std::initializer_list for brace initialization.
Definition initializer_list.hpp:21
A compile-time ratio of two integers, automatically reduced to lowest terms.
Definition ratio.hpp:27
typename sort< TypeList, ComparisonPredicate >::type sort_t
A type alias for a type list holding the elements of TypeList sorted by ComparisonPredicate ....
Definition sort.hpp:256
typename remove_nth< List, Index >::type remove_nth_t
Filter a type-list to remove the element at the specified index.
Definition remove_nth.hpp:89
ARENE_MAYBE_UNUSED constexpr std::size_t index_of_v
Get the index of T in the type-list, L0.
Definition accumulate.hpp:37
typename scaled_unit_detail::scaled_unit_helper< BaseUnit, Scale >::type scaled_unit
Type alias for a quantity unit that is another unit scaled by some scale factor.
Definition scaled_unit.hpp:128
constexpr auto type_name_v
The name of a type as an inline_string.
constexpr bool is_unit_v
Type trait for detecting if a type is a unit. Evaluates to true if the type is a unit,...
typename scaled_unit_detail::inverse_unit_scale_helper< Type >::type inverse_unit_scale_t
A type that represents the inverse of a scale factor.
Definition scaled_unit.hpp:133
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
Definition accumulate.hpp:36
STL namespace.
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
typename ratio_detail::ratio_multiply_impl< R1, R2 >::type ratio_multiply
Defines the ratio yielded by multiplying the two given ratios.
Definition ratio.hpp:213