Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
make_unsigned.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_UNSIGNED_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_MAKE_UNSIGNED_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_unsigned
35/// @tparam Type The type to convert to an unsigned 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_unsigned {};
39// parasoft-end-suppress AUTOSAR-A11_0_2
40
41/// @brief Internal class providing @c std::make_unsigned for integral types that are already unsigned
42/// @tparam Type The type to convert to an unsigned 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_unsigned for integral types or enumeration types that are @c const
57/// qualified
58/// @tparam Type The type to convert to an unsigned equivalent
59template <typename Type>
61 Type,
66 /// @brief The resulting type
68};
69
70/// @brief Internal class providing @c std::make_unsigned for integral types or enumeration types that are @c volatile
71/// qualified
72/// @tparam Type The type to convert to an unsigned equivalent
73template <typename Type>
75 Type,
80 /// @brief The resulting type
82};
83
84/// @brief Internal class providing @c std::make_unsigned for integral types or enumeration types that are @c const
85/// and @c volatile qualified
86/// @tparam Type The type to convert to an unsigned 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_unsigned for @c signed char
100template <>
101struct make_unsigned<signed char> {
102 /// @brief The resulting type
103 using type = unsigned char;
104};
105
106/// @brief Internal class providing @c std::make_unsigned for @c short
107template <>
108// NOLINTNEXTLINE(google-runtime-int)
109struct make_unsigned<short> {
110 /// @brief The resulting type
111 // NOLINTNEXTLINE(google-runtime-int)
112 using type = unsigned short;
113};
114
115/// @brief Internal class providing @c std::make_unsigned for @c int
116template <>
117struct make_unsigned<int> {
118 /// @brief The resulting type
119 using type = unsigned;
120};
121
122/// @brief Internal class providing @c std::make_unsigned for @c long
123template <>
124// NOLINTNEXTLINE(google-runtime-int)
125struct make_unsigned<long> {
126 /// @brief The resulting type
127 // NOLINTNEXTLINE(google-runtime-int)
128 using type = unsigned long;
129};
130
131/// @brief Internal class providing @c std::make_unsigned for @c long @c long
132template <>
133// NOLINTNEXTLINE(google-runtime-int)
134struct make_unsigned<long long> {
135 /// @brief The resulting type
136 // NOLINTNEXTLINE(google-runtime-int)
137 using type = unsigned long long;
138};
139
140/// @brief Internal class providing @c std::make_unsigned for @c char
141template <>
142struct make_unsigned<char> {
143 /// @brief The resulting type
144 // NOLINTNEXTLINE(google-runtime-int)
145 using type = conditional_t<is_signed_v<char>, unsigned char, char>;
146};
147
148/// @brief Internal class providing an unsigned type with a specified size
149/// @tparam Size the size to check
150template <size_t Size, typename = arene::base::constraints<>>
152
153/// @brief Internal class providing an unsigned type with the size of @c char
154/// @tparam Size the size to check
155template <size_t Size>
157 /// @brief The resulting type
158 using type = unsigned char;
159};
160
161/// @brief Internal class providing an unsigned 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_unsigned<Size, arene::base::constraints<enable_if_t<Size == sizeof(short)>>> {
166 /// @brief The resulting type
167 // NOLINTNEXTLINE(google-runtime-int)
168 using type = unsigned short;
169};
170
171/// @brief Internal class providing an unsigned 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 = unsigned;
180};
181
182/// @brief Internal class providing an unsigned 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 = unsigned long;
192};
193
194/// @brief Internal class providing an unsigned type with the size of @c long 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 = unsigned 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 an unsigned type for @c wchar_t
209template <>
210struct make_unsigned<wchar_t> {
211 /// @brief The resulting type
212 // NOLINTNEXTLINE(google-runtime-int)
213 using type = typename sized_unsigned<sizeof(wchar_t)>::type;
214};
215// parasoft-end-suppress AUTOSAR-A2_13_3-a-2
216
217/// @brief Internal class providing an unsigned type for @c char16_t
218template <>
219struct make_unsigned<char16_t> {
220 /// @brief The resulting type
221 using type = typename sized_unsigned<sizeof(char16_t)>::type;
222};
223
224/// @brief Internal class providing an unsigned type for @c char32_t
225template <>
226struct make_unsigned<char32_t> {
227 /// @brief The resulting type
228 using type = typename sized_unsigned<sizeof(char32_t)>::type;
229};
230
231/// @brief Internal class providing @c std::make_unsigned for integral types that are already unsigned
232/// @tparam Type The type to convert to an unsigned equivalent
233template <typename Type>
235 Type,
236 arene::base::
238 /// @brief The resulting type
239 using type = typename sized_unsigned<sizeof(Type)>::type;
240};
241
242} // namespace make_unsigned_detail
243
244/// @brief A type trait to provide the equivalent unsigned 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 unsigned 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_UNSIGNED_HPP_
Definition make_unsigned.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_unsigned.
Definition make_unsigned.hpp:38
Internal class providing an unsigned type with a specified size.
Definition make_unsigned.hpp:151