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
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,
46 arene::base::constraints<
47 enable_if_t<is_integral_v<Type>>,
48 enable_if_t<!is_signed_v<Type>>,
49 enable_if_t<!is_same_v<Type, bool>>,
50 enable_if_t<!is_const_v<Type>>,
51 enable_if_t<!is_volatile_v<Type>>>> {
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,
62 arene::base::constraints<
63 enable_if_t<is_integral_v<Type> || is_enum_v<Type>>,
64 enable_if_t<is_const_v<Type>>,
65 enable_if_t<!is_volatile_v<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,
76 arene::base::constraints<
77 enable_if_t<is_integral_v<Type> || is_enum_v<Type>>,
78 enable_if_t<is_volatile_v<Type>>,
79 enable_if_t<!is_const_v<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>
89 Type,
90 arene::base::constraints<
91 enable_if_t<is_integral_v<Type> || is_enum_v<Type>>,
92 enable_if_t<is_const_v<Type>>,
93 enable_if_t<is_volatile_v<Type>>>> {
94 /// @brief The resulting type
96};
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>
156struct sized_unsigned<Size, arene::base::constraints<enable_if_t<Size == sizeof(char)>>> {
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::
237 constraints<enable_if_t<is_enum_v<Type>>, enable_if_t<!is_const_v<Type>>, enable_if_t<!is_volatile_v<Type>>>> {
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_
Public export header for the constraints subpackage.
Definition accumulate.hpp:36
Definition make_unsigned.hpp:33
STL namespace.
typename add_volatile< Type >::type add_volatile_t
Add volatile to the provided type. An alias for the modified type.
Definition add_volatile.hpp:29
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 add_const< Type >::type add_const_t
Add const to the provided type. An alias for the modified type.
Definition add_const.hpp:27
make_unsigned_detail::make_unsigned< Type > make_unsigned
A type trait to provide the equivalent unsigned type for integral and enumeration types other than bo...
Definition make_unsigned.hpp:247
typename conditional< Condition, TrueType, FalseType >::type conditional_t
Helper template alias for selecting types based on a condition. If the supplied Condition is true the...
Definition conditional.hpp:57
typename make_unsigned< Type >::type make_unsigned_t
A type alias for the equivalent unsigned type for integral and enumeration types other than bool.
Definition make_unsigned.hpp:252
typename sized_unsigned< sizeof(char16_t)>::type type
The resulting type.
Definition make_unsigned.hpp:221
typename sized_unsigned< sizeof(char32_t)>::type type
The resulting type.
Definition make_unsigned.hpp:228
conditional_t< is_signed_v< char >, unsigned char, char > type
The resulting type.
Definition make_unsigned.hpp:145
unsigned type
The resulting type.
Definition make_unsigned.hpp:119
unsigned long type
The resulting type.
Definition make_unsigned.hpp:128
unsigned long long type
The resulting type.
Definition make_unsigned.hpp:137
unsigned short type
The resulting type.
Definition make_unsigned.hpp:112
unsigned char type
The resulting type.
Definition make_unsigned.hpp:103
typename sized_unsigned< sizeof(wchar_t)>::type type
The resulting type.
Definition make_unsigned.hpp:213
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