Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
base_kind_set.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_BASE_KIND_SET_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_UNITS_BASE_KIND_SET_HPP_
7
12#include "arene/base/stdlib_choice/multiplies.hpp"
13#include "arene/base/stdlib_choice/remove_const.hpp"
22
23// parasoft-begin-suppress AUTOSAR-A7_1_5-a-2 "Trailing return syntax permitted by A7-1-5 Permit #1 v1.0.0"
24// parasoft-begin-suppress AUTOSAR-M2_10_1-a "Similar identifiers permitted by M2-10-1 Permit #1 v1.0.0"
25// parasoft-begin-suppress AUTOSAR-A3_1_5-a "False positive: all functions are constexpr so are intended to be inlined"
26// parasoft-begin-suppress CERT_C-EXP37-a "False positive: The rule does not mention naming all parameters"
27
28namespace arene {
29namespace base {
30namespace units_detail {
31
32/// @brief specifies the exponents of a quantity kind in the base kind set
33class base_kind_exponents {
34 /// @brief positive exponent value
35 exponent_t positive_;
36 /// @brief negative exponent value
37 exponent_t negative_;
38
39 public:
40 /// @brief constuct with the provided exponents
41 /// @param pos positive exponent value
42 /// @param neg negative exponent value
43 /// @pre @c pos is non-negative
44 /// @pre @c neg is non-negative
45 // NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
46 constexpr base_kind_exponents(exponent_t pos, exponent_t neg) noexcept
47 : positive_{pos},
48 negative_{neg} {
49 ARENE_PRECONDITION(pos >= 0);
50 ARENE_PRECONDITION(neg >= 0);
51 }
52
53 /// @brief default constuct with zero for both positive and negative exponents
54 constexpr base_kind_exponents() noexcept
55 : base_kind_exponents{0, 0} {}
56
57 /// @brief obtain the positive exponent value
58 /// @return positive exponent value
59 constexpr auto positive() const noexcept -> exponent_t { return positive_; }
60
61 /// @brief obtain the negative exponent value
62 /// @return negative exponent value
63 constexpr auto negative() const noexcept -> exponent_t { return negative_; }
64
65 // parasoft-begin-suppress AUTOSAR-A11_3_1-a "Hidden friends permitted by A11-3-1 Permit #2 v1.0.0"
66 /// @brief scale exponents by a scaling factor
67 /// @param scaling factor to scale exponents by
68 /// @param self positive and negative exponents
69 /// @return exponents after applying @c scaling
70 /// @note This function does not guard against overflow/underflow as it is
71 /// should only be invoked in a constant expression.
72 friend constexpr auto operator*(exponent_t const scaling, base_kind_exponents const self) noexcept
73 -> base_kind_exponents {
74 if (scaling < 0) {
75 return {-scaling * self.negative(), -scaling * self.positive()};
76 }
77
78 return {scaling * self.positive(), scaling * self.negative()};
79 }
80 // parasoft-end-suppress AUTOSAR-A11_3_1-a
81
82 /// @brief combine exponents
83 /// @param other exponents to add to @c *this
84 /// @return @c *this
85 /// @note This function does not guard against overflow/underflow as it is
86 /// should only be invoked in a constant expression.
87 // parasoft-begin-suppress AUTOSAR-A7_1_3-a "False positive: const is placed on the right hand side"
88 constexpr auto operator+=(base_kind_exponents const other) noexcept -> base_kind_exponents& {
89 positive_ += other.positive();
90 negative_ += other.negative();
91 return *this;
92 }
93 // parasoft-end-suppress AUTOSAR-A7_1_3-a
94};
95
96/// @brief specifies a set of base quantity kinds with associated exponents
97/// @tparam Kinds base quantity kind types
98template <typename... Kinds>
99class base_kind_set {
100 public:
101 static_assert(all_of_v<is_quantity_kind_v<Kinds>...>, "every type in 'Kinds' must be a quantity kind");
102
103 /// @brief exponents associated with each quantity kind base
104 ///
105 /// Exponent values are associated the quantity kind with the same index.
106 // parasoft-begin-suppress AUTOSAR-M11_0_1-a "'base_kind_set' is an implementation detail with no class invariants"
107 // parasoft-begin-suppress AUTOSAR-M8_5_2-a "False positive: correct initialization"
108 // parasoft-begin-suppress AUTOSAR-M8_5_2-b "False positive: correct initialization"
109 arene::base::array<base_kind_exponents, sizeof...(Kinds)> exponents{};
110 // parasoft-end-suppress AUTOSAR-M8_5_2-a
111 // parasoft-end-suppress AUTOSAR-M8_5_2-b
112 // parasoft-end-suppress AUTOSAR-M11_0_1-a
113
114 // parasoft-begin-suppress AUTOSAR-A11_3_1-a "Hidden friends permitted by A11-3-1 Permit #2 v1.0.0"
115 // parasoft-begin-suppress AUTOSAR-A0_1_3-a "False positive: This is a
116 // public function, and doesn't need to be used in the translation unit"
117 /// @brief scale exponents by a scaling factor
118 /// @param scaling factor to scale exponents by
119 /// @param self set of base quantity kinds with associated exponents
120 /// @return base quantity kinds after scaling exponents values
121 friend constexpr auto operator*(exponent_t const scaling, base_kind_set const& self) noexcept -> base_kind_set {
122 auto updated = base_kind_set{};
123 arene::base::transform( //
124 self.exponents.begin(),
125 self.exponents.end(),
126 updated.exponents.begin(),
127 bind_front(std::multiplies<>{}, scaling)
128 );
129 return updated;
130 }
131 // parasoft-end-suppress AUTOSAR-A0_1_3-a
132 // parasoft-end-suppress AUTOSAR-A11_3_1-a
133
134 /// @brief obtain the exponent values for the specified quantity kind
135 /// @tparam Kind quantity kind of which to obtain the exponent values
136 /// @return reference to the exponents
137 /// @note error if @c Kind is not in @c Kinds...
138 template <typename Kind>
139 constexpr auto exponent_for() noexcept -> base_kind_exponents& {
141 }
142 /// @brief obtain the exponent values for the specified quantity kind
143 /// @tparam Kind quantity kind of which to obtain the exponent values
144 /// @return reference to the exponents
145 /// @note error if @c Kind is not in @c Kinds...
146 template <typename Kind>
147 constexpr auto exponent_for() const noexcept -> base_kind_exponents const& {
149 }
150
151 // parasoft-begin-suppress AUTOSAR-M3_3_2-a "False positive: inline function used in multiple translation units"
152 /// @brief update exponents values from another base kind set
153 /// @tparam OtherKinds base quantity kind types of the other base kind set
154 /// @param other base kind set to combine with @c *this
155 /// @note error if any type in @c OtherKinds is not in @c Kinds..., i.e. @c
156 /// other must be a subset of @c *this
157 /// @return @c *this
158 ///
159 /// Updates @c *this with the exponent values from @c other.
160 template <typename... OtherKinds>
161 constexpr auto add_exponents_from(base_kind_set<OtherKinds...> const& other) noexcept -> base_kind_set& {
162 // parasoft-begin-suppress AUTOSAR-M5_18_1-a "Comma operator avoids an expensive recursive iteration"
163 consume_values({(exponent_for<OtherKinds>() += other.template exponent_for<OtherKinds>(), true)...});
164 // parasoft-end-suppress AUTOSAR-M5_18_1-a
165 return *this;
166 }
167};
168// parasoft-end-suppress AUTOSAR-M3_3_2-a
169
170// parasoft-begin-suppress AUTOSAR-M3_3_2-a "False positive: inline function used in multiple translation units"
171/// @brief combine base kind sets
172/// @tparam Bases base kind set types
173/// @param bases base kind sets to combine
174/// @return an base kind set which is the combination of all input sets
175template <typename... Bases>
176constexpr auto set_union(Bases const&... bases) noexcept -> type_lists::concat_unique_t<Bases...> {
177 static_assert(
179 "every type in 'Bases' must be a base kind set"
180 );
181
182 auto combined = type_lists::concat_unique_t<Bases...>{};
183
184 // parasoft-begin-suppress AUTOSAR-M5_18_1-a "Comma operator avoids an expensive recursive iteration"
185 consume_values({(combined.add_exponents_from(bases), true)...});
186 // parasoft-end-suppress AUTOSAR-M5_18_1-a
187
188 return combined;
189}
190// parasoft-end-suppress AUTOSAR-M3_3_2-a
191
192/// @brief converts a base kind set constant into a list of quantity kind types with exponents
193/// @tparam BaseKindSetConstant type constant that provides a base kind set as a
194/// @c static @c constexpr member @c value
195template <typename BaseKindSetConstant>
196class as_list_of_kinds_with_exponents {
197 // parasoft-begin-suppress CERT_C-PRE31-c "False positive: static_assert is a compile-time assert and can have no
198 // side-effects"
199 static_assert(
200 is_instantiation_of_v<std::remove_const_t<decltype(BaseKindSetConstant::value)>, base_kind_set>,
201 "'BaseKindSetConstant::value' must be a 'base_kind_set'"
202 );
203 // parasoft-end-suppress CERT_C-PRE31-c
204
205 /// @brief lifts a base kind set into a type list
206 /// @tparam BaseKinds quantity base kind types
207 /// @return base kind set lifted into a type list
208 template <typename... BaseKinds>
209 static auto impl(base_kind_set<BaseKinds...>) -> type_list<quantity_kind_with_exponent<
210 BaseKinds,
211 BaseKindSetConstant::value.template exponent_for<BaseKinds>().positive(),
212 BaseKindSetConstant::value.template exponent_for<BaseKinds>().negative()>...>;
213
214 public:
215 /// @brief base kind set type lifted into a type list
216 using type = decltype(impl(BaseKindSetConstant::value));
217};
218
219/// @brief converts a base kind set constant into a list of quantity kind types with exponents
220/// @tparam BaseKindSetConstant type constant that provides a base kind set as a
221/// @c static @c constexpr member @c value
222///
223/// Lifts the base kind set value specified by @c BaseKindSetConstant::value into a type.
224template <typename BaseKindSetConstant>
225using as_list_of_kinds_with_exponents_t = typename as_list_of_kinds_with_exponents<BaseKindSetConstant>::type;
226
227} // namespace units_detail
228} // namespace base
229} // namespace arene
230
231#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_UNITS_BASE_KIND_SET_HPP_
#define ARENE_PRECONDITION(condition)
Check that a precondition holds, and terminate the application if it does not.
Definition contract.hpp:150
arene::base::type_lists::remove_duplicates_t< arene::base::type_lists::concat_t< Ls... > > concat_unique_t
Concatenate multiple lists together with duplicate types removed.
Definition concat_unique.hpp:21
Definition accumulate.hpp:37
constexpr bool is_instantiation_of_v
Definition is_instantiation_of.hpp:16
constexpr void consume_values(std::initializer_list< consume_values_detail::anything > const &) noexcept
Consume a list of values and discard them. Must be called with an extra pair of braces around the exp...
Definition consume_values.hpp:73
constexpr bool all_of_v
Trait which tests if every value in a parameter pack is true .
constexpr auto get(array< T, N > &arr) noexcept -> T &
Get a reference to the element at the specified index in the array.
Definition array.hpp:885
Definition accumulate.hpp:36
typename remove_const< Type >::type remove_const_t
remove const from the provided type. An alias for the modified type.
Definition remove_const.hpp:36
Provides the all_of_v<bool...> trait.