Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
make_signed.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_STDLIB_INCLUDE_STDLIB_DETAIL_MAKE_SIGNED_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_MAKE_SIGNED_HPP_
7
8// parasoft-begin-suppress CERT_CPP-DCL58-a-2 "Part of a standard library implementation"
9// parasoft-begin-suppress AUTOSAR-A17_6_1-a-2 "Part of a standard library implementation"
10
11// IWYU pragma: private, include <type_traits>
12// IWYU pragma: friend "stdlib_detail/.*"
13
14#include "arene/base/constraints.hpp"
15#include "stdlib/include/stdlib_detail/add_const.hpp"
16#include "stdlib/include/stdlib_detail/add_volatile.hpp"
17#include "stdlib/include/stdlib_detail/conditional.hpp"
18#include "stdlib/include/stdlib_detail/cstdint.hpp"
19#include "stdlib/include/stdlib_detail/enable_if.hpp"
20#include "stdlib/include/stdlib_detail/is_const.hpp"
21#include "stdlib/include/stdlib_detail/is_enum.hpp"
22#include "stdlib/include/stdlib_detail/is_integral.hpp"
23#include "stdlib/include/stdlib_detail/is_same.hpp"
24#include "stdlib/include/stdlib_detail/is_signed.hpp"
25#include "stdlib/include/stdlib_detail/is_volatile.hpp"
26#include "stdlib/include/stdlib_detail/remove_cv.hpp"
27
28namespace std {
29
30// parasoft-begin-suppress AUTOSAR-A2_7_3-a "False positive: all declarations
31// and typedefs *are* preceded by a comment with @brief"
32
34/// @brief Internal class providing @c std::make_signed
35/// @tparam Type The type to convert to a signed equivalent
36// parasoft-begin-suppress AUTOSAR-A11_0_2 "False positive: Not used as a base class here"
37template <typename Type, typename = arene::base::constraints<>>
38struct make_signed {};
39// parasoft-end-suppress AUTOSAR-A11_0_2
40
41/// @brief Internal class providing @c std::make_signed for integral types that are already signed
42/// @tparam Type The type to convert to a signed equivalent
43template <typename Type>
45 Type,
49 enable_if_t<!is_same_v<Type, bool>>,
52 /// @brief The resulting type
53 using type = Type;
54};
55
56/// @brief Internal class providing @c std::make_signed for integral types or enumeration types that are @c const
57/// qualified
58/// @tparam Type The type to convert to a signed equivalent
59template <typename Type>
61 Type,
66 /// @brief The resulting type
68};
69
70/// @brief Internal class providing @c std::make_signed for integral types or enumeration types that are @c volatile
71/// qualified
72/// @tparam Type The type to convert to a signed equivalent
73template <typename Type>
75 Type,
80 /// @brief The resulting type
82};
83
84/// @brief Internal class providing @c std::make_signed for integral types or enumeration types that are @c const
85/// and @c volatile qualified
86/// @tparam Type The type to convert to a signed equivalent
87template <typename Type>
97
98// parasoft-begin-suppress AUTOSAR-A3_9_1-b-2 "The built-in types must be handled explicitly here for correctness"
99/// @brief Internal class providing @c std::make_signed for @c unsigned @c char
100template <>
101struct make_signed<unsigned char> {
102 /// @brief The resulting type
103 using type = signed char;
104};
105
106/// @brief Internal class providing @c std::make_signed for @c unsigned @c short
107template <>
108// NOLINTNEXTLINE(google-runtime-int)
109struct make_signed<unsigned short> {
110 /// @brief The resulting type
111 // NOLINTNEXTLINE(google-runtime-int)
112 using type = short;
113};
114
115/// @brief Internal class providing @c std::make_signed for @c unsigned @c int
116template <>
117struct make_signed<unsigned int> {
118 /// @brief The resulting type
119 using type = int;
120};
121
122/// @brief Internal class providing @c std::make_signed for @c long
123template <>
124// NOLINTNEXTLINE(google-runtime-int)
125struct make_signed<unsigned long> {
126 /// @brief The resulting type
127 // NOLINTNEXTLINE(google-runtime-int)
128 using type = long;
129};
130
131/// @brief Internal class providing @c std::make_signed for @c unsigned @c long @c long
132template <>
133// NOLINTNEXTLINE(google-runtime-int)
134struct make_signed<unsigned long long> {
135 /// @brief The resulting type
136 // NOLINTNEXTLINE(google-runtime-int)
137 using type = long long;
138};
139
140/// @brief Internal class providing @c std::make_signed for @c char
141template <>
142struct make_signed<char> {
143 /// @brief The resulting type
144 // NOLINTNEXTLINE(google-runtime-int)
145 using type = signed char;
146};
147
148/// @brief Internal class providing a signed type with a specified size
149/// @tparam Size the size to check
150template <size_t Size, typename = arene::base::constraints<>>
151struct sized_signed {};
152
153/// @brief Internal class providing a signed type with the size of @c char
154/// @tparam Size the size to check
155template <size_t Size>
156struct sized_signed<Size, arene::base::constraints<enable_if_t<Size == sizeof(char)>>> {
157 /// @brief The resulting type
158 using type = signed char;
159};
160
161/// @brief Internal class providing a signed type with the size of @c short
162/// @tparam Size the size to check
163template <size_t Size>
164// NOLINTNEXTLINE(google-runtime-int)
165struct sized_signed<Size, arene::base::constraints<enable_if_t<Size == sizeof(short)>>> {
166 /// @brief The resulting type
167 // NOLINTNEXTLINE(google-runtime-int)
168 using type = short;
169};
170
171/// @brief Internal class providing a signed type with the size of @c int
172/// @tparam Size the size to check
173template <size_t Size>
175 Size,
176 // NOLINTNEXTLINE(google-runtime-int)
177 arene::base::constraints<enable_if_t<(Size > sizeof(short))>, enable_if_t<Size == sizeof(int)>>> {
178 /// @brief The resulting type
179 using type = int;
180};
181
182/// @brief Internal class providing a signed type with the size of @c long
183/// @tparam Size the size to check
184template <size_t Size>
186 Size,
187 // NOLINTNEXTLINE(google-runtime-int)
188 arene::base::constraints<enable_if_t<(Size > sizeof(int))>, enable_if_t<Size == sizeof(long)>>> {
189 /// @brief The resulting type
190 // NOLINTNEXTLINE(google-runtime-int)
191 using type = long;
192};
193
194/// @brief Internal class providing a signed type with the size of @c long @c long
195/// @tparam Size the size to check
196template <size_t Size>
198 Size,
199 // NOLINTNEXTLINE(google-runtime-int)
200 arene::base::constraints<enable_if_t<(Size > sizeof(long))>, enable_if_t<Size == sizeof(long long)>>> {
201 /// @brief The resulting type
202 // NOLINTNEXTLINE(google-runtime-int)
203 using type = long long;
204};
205// parasoft-end-suppress AUTOSAR-A3_9_1-b-2
206
207// parasoft-begin-suppress AUTOSAR-A2_13_3-a-2 "wchar_t must be used here to ensure correctness"
208/// @brief Internal class providing a signed type for @c wchar_t
209template <>
210struct make_signed<wchar_t> {
211 /// @brief The resulting type
212 // NOLINTNEXTLINE(google-runtime-int)
213 using type = typename sized_signed<sizeof(wchar_t)>::type;
214};
215// parasoft-end-suppress AUTOSAR-A2_13_3-a-2
216
217/// @brief Internal class providing a signed type for @c char16_t
218template <>
219struct make_signed<char16_t> {
220 /// @brief The resulting type
221 using type = typename sized_signed<sizeof(char16_t)>::type;
222};
223
224/// @brief Internal class providing a signed type for @c char32_t
225template <>
226struct make_signed<char32_t> {
227 /// @brief The resulting type
228 using type = typename sized_signed<sizeof(char32_t)>::type;
229};
230
231/// @brief Internal class providing @c std::make_signed for integral types that are already signed
232/// @tparam Type The type to convert to a signed equivalent
233template <typename Type>
235 Type,
236 arene::base::
238 /// @brief The resulting type
239 using type = typename sized_signed<sizeof(Type)>::type;
240};
241
242} // namespace make_signed_detail
243
244/// @brief A type trait to provide the equivalent signed type for integral and enumeration types other than @c bool
245/// @tparam Type The type to get the equivalent of
246template <typename Type>
248
249/// @brief A type alias for the equivalent signed type for integral and enumeration types other than @c bool
250/// @tparam Type The type to get the equivalent of
251template <typename Type>
253
254// parasoft-end-suppress AUTOSAR-A2_7_3-a
255
256} // namespace std
257
258#endif // INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_MAKE_SIGNED_HPP_
Definition make_signed.hpp:33
constexpr auto operator()(::arene::base::result< void, E > const &value) const noexcept(noexcept(hash< E >{}(std::declval< E const & >()))) -> std::size_t
Calculate the hash of a result.
Definition result.hpp:1827
Internal class providing std::make_signed.
Definition make_signed.hpp:38
Internal class providing a signed type with a specified size.
Definition make_signed.hpp:151