• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef API_BASE_CONTAINERS_TYPE_TRAITS_H
17 #define API_BASE_CONTAINERS_TYPE_TRAITS_H
18 
19 #include <cstddef>
20 
21 #include <base/namespace.h>
22 
23 BASE_BEGIN_NAMESPACE()
24 template<class T, T v>
25 struct integral_constant {
26     static constexpr T value = v;
27     using value_type = T;
28     using type = integral_constant;
value_typeintegral_constant29     constexpr operator value_type() const noexcept
30     {
31         return (value);
32     }
operatorintegral_constant33     constexpr value_type operator()() const noexcept
34     {
35         return (value);
36     }
37 };
38 
39 using false_type = integral_constant<bool, false>;
40 using true_type = integral_constant<bool, true>;
41 
42 template<bool B, class T = void>
43 struct enable_if {};
44 template<class T>
45 struct enable_if<true, T> {
46     using type = T;
47 };
48 template<bool B, class T = void>
49 using enable_if_t = typename enable_if<B, T>::type;
50 
51 template<class T, class U>
52 struct is_same : false_type {};
53 template<class T>
54 struct is_same<T, T> : true_type {};
55 
56 template<class T, class U>
57 constexpr auto is_same_v = is_same<T, U>::value;
58 
59 template<class T>
60 struct remove_extent {
61     using type = T;
62 };
63 template<class T>
64 struct remove_extent<T[]> {
65     using type = T;
66 };
67 template<class T, size_t N>
68 struct remove_extent<T[N]> {
69     using type = T;
70 };
71 template<class T>
72 using remove_extent_t = typename remove_extent<T>::type;
73 
74 template<class T, unsigned N = 0>
75 struct extent : integral_constant<size_t, 0> {};
76 
77 template<class T>
78 struct extent<T[], 0> : integral_constant<size_t, 0> {};
79 
80 template<class T, unsigned N>
81 struct extent<T[], N> : extent<T, N - 1> {};
82 
83 template<class T, size_t I>
84 struct extent<T[I], 0> : integral_constant<size_t, I> {};
85 
86 template<class T, size_t I, unsigned N>
87 struct extent<T[I], N> : extent<T, N - 1> {};
88 
89 template<class T, unsigned N = 0>
90 inline constexpr size_t extent_v = extent<T, N>::value;
91 
92 template<class T>
93 struct remove_const {
94     using type = T;
95 };
96 template<class T>
97 struct remove_const<const T> {
98     using type = T;
99 };
100 template<class T>
101 using remove_const_t = typename remove_const<T>::type;
102 
103 template<class T>
104 struct is_const : false_type {};
105 template<class T>
106 struct is_const<const T> : true_type {};
107 template<class T>
108 constexpr auto is_const_v = is_const<T>::value;
109 
110 template<class T>
111 struct is_array : false_type {};
112 template<class T>
113 struct is_array<T[]> : true_type {};
114 template<class T, size_t N>
115 struct is_array<T[N]> : true_type {};
116 template<class T>
117 constexpr bool is_array_v = is_array<T>::value;
118 template<class T>
119 using is_array_t = typename is_array<T>::type;
120 
121 template<class T>
122 struct is_pointer : false_type {};
123 template<class T>
124 struct is_pointer<T*> : true_type {};
125 template<class T>
126 constexpr auto is_pointer_v = is_pointer<T>::value;
127 
128 template<class T>
129 struct is_reference : false_type {};
130 template<class T>
131 struct is_reference<T&> : true_type {};
132 template<class T>
133 struct is_reference<T&&> : true_type {};
134 template<class T>
135 constexpr bool is_reference_v = is_reference<T>::value;
136 
137 template<class T>
138 struct is_lvalue_reference : false_type {};
139 template<class T>
140 struct is_lvalue_reference<T&> : true_type {};
141 template<class T>
142 constexpr bool is_lvalue_reference_v = is_lvalue_reference<T>::value;
143 
144 template<class T>
145 struct is_rvalue_reference : false_type {};
146 template<class T>
147 struct is_rvalue_reference<T&&> : true_type {};
148 template<class T>
149 constexpr bool is_rvalue_reference_v = is_rvalue_reference<T>::value;
150 
151 template<class... _Types>
152 using void_t = void;
153 template<class T, class = void>
154 struct add_lvalue_reference {
155     using type = T;
156 };
157 template<class T, class = void>
158 struct add_rvalue_reference {
159     using type = T;
160 };
161 template<class T>
162 struct add_lvalue_reference<T, void_t<T&>> {
163     using type = T&;
164 };
165 template<class T>
166 struct add_rvalue_reference<T, void_t<T&>> {
167     using type = T&&;
168 };
169 template<class T>
170 using add_lvalue_reference_t = typename add_lvalue_reference<T>::type;
171 template<class T>
172 using add_rvalue_reference_t = typename add_rvalue_reference<T>::type;
173 
174 template<class T>
175 add_rvalue_reference_t<T> declval() noexcept;
176 
177 template<class T>
178 struct remove_reference {
179     using type = T;
180 };
181 template<class T>
182 struct remove_reference<T&> {
183     using type = T;
184 };
185 template<class T>
186 struct remove_reference<T&&> {
187     using type = T;
188 };
189 template<class T>
190 using remove_reference_t = typename remove_reference<T>::type;
191 template<class T>
192 constexpr remove_reference_t<T>&& move(T&& _Arg) noexcept
193 {
194     static_assert(!is_const<typename remove_reference<T>::type>::value, "move of const object is invalid.");
195     return (static_cast<remove_reference_t<T>&&>(_Arg));
196 }
197 template<class T>
198 constexpr T&& forward(remove_reference_t<T>& _Arg) noexcept
199 {
200     return (static_cast<T&&>(_Arg));
201 }
202 template<class T>
203 constexpr T&& forward(remove_reference_t<T>&& _Arg) noexcept
204 {
205     static_assert(!is_lvalue_reference_v<T>, "bad forward call");
206     return (static_cast<T&&>(_Arg));
207 }
208 
209 using nullptr_t = decltype(nullptr);
210 template<class T, class U = T>
211 constexpr T exchange(T& obj, U&& new_value)
212 {
213     T old_value = move(obj);
214     obj = forward<U>(new_value);
215     return old_value;
216 }
217 
218 template<typename T>
219 struct type_identity {
220     using type = T;
221 };
222 
223 template<class T>
224 using type_identity_t = typename type_identity<T>::type;
225 
226 template<bool Condition, class TrueType, class FalseType>
227 struct conditional {
228     using type = TrueType;
229 };
230 template<class TrueType, class FalseType>
231 struct conditional<false, TrueType, FalseType> {
232     using type = FalseType;
233 };
234 template<bool Condition, class TrueType, class FalseType>
235 using conditional_t = typename conditional<Condition, TrueType, FalseType>::type;
236 
237 template<class T>
238 struct is_void : is_same<void, remove_const_t<T>> {};
239 template<typename T>
240 inline constexpr bool is_void_v = is_void<T>::value;
241 
242 template<typename T>
243 struct is_floating_point
244     : integral_constant<bool, is_same_v<float, remove_const_t<T>> || is_same_v<double, remove_const_t<T>> ||
245                                   is_same_v<long double, remove_const_t<T>>> {};
246 template<typename T>
247 inline constexpr bool is_floating_point_v = is_floating_point<T>::value;
248 
249 namespace detail {
250 template<typename T>
251 struct is_integral : false_type {};
252 
253 template<>
254 struct is_integral<bool> : true_type {};
255 
256 template<>
257 struct is_integral<signed char> : true_type {};
258 
259 template<>
260 struct is_integral<short> : true_type {};
261 
262 template<>
263 struct is_integral<int> : true_type {};
264 
265 template<>
266 struct is_integral<long> : true_type {};
267 
268 template<>
269 struct is_integral<long long> : true_type {};
270 
271 template<>
272 struct is_integral<unsigned char> : true_type {};
273 
274 template<>
275 struct is_integral<unsigned short> : true_type {};
276 
277 template<>
278 struct is_integral<unsigned int> : true_type {};
279 
280 template<>
281 struct is_integral<unsigned long> : true_type {};
282 
283 template<>
284 struct is_integral<unsigned long long> : true_type {};
285 } // namespace detail
286 
287 template<typename T>
288 struct is_integral : detail::is_integral<remove_const_t<T>>::type {};
289 
290 template<class T>
291 inline constexpr bool is_integral_v = is_integral<T>::value;
292 
293 template<class T>
294 struct is_arithmetic : integral_constant<bool, is_integral_v<T> || is_floating_point_v<T>> {};
295 
296 template<class T>
297 inline constexpr bool is_arithmetic_v = is_arithmetic<T>::value;
298 
299 template<typename T, bool = is_arithmetic_v<T>>
300 struct is_unsigned : integral_constant<bool, T(0) < T(-1)> {};
301 
302 template<typename T>
303 struct is_unsigned<T, false> : false_type {};
304 
305 template<typename T>
306 inline constexpr bool is_unsigned_v = is_unsigned<T>::value;
307 
308 template<typename T, bool = is_arithmetic_v<T>>
309 struct is_signed : integral_constant<bool, T(-1) < T(0)> {};
310 
311 template<typename T>
312 struct is_signed<T, false> : false_type {};
313 
314 template<typename T>
315 inline constexpr bool is_signed_v = is_signed<T>::value;
316 
317 namespace detail {
318 template<typename T>
319 auto is_returnable(int) -> decltype(void(static_cast<T (*)()>(nullptr)), true_type {});
320 
321 template<typename>
322 auto is_returnable(...) -> false_type;
323 
324 template<typename T>
325 inline constexpr bool is_returnable_v = decltype(is_returnable<T>(0))::value;
326 
327 template<typename From, typename To>
328 auto is_implicitly_convertible(int) -> decltype(void(declval<void (&)(To)>()(declval<From>())), true_type {});
329 
330 template<typename, typename>
331 auto is_implicitly_convertible(...) -> false_type;
332 
333 template<typename From, typename To>
334 inline constexpr bool is_implicitly_convertible_v = decltype(is_implicitly_convertible<From, To>(0))::value;
335 } // namespace detail
336 
337 template<typename From, typename To>
338 struct is_convertible
339     : integral_constant<bool, (is_void_v<From> && is_void_v<To>) ||
340                                   (detail::is_returnable_v<To> && detail::is_implicitly_convertible_v<From, To>)> {};
341 template<typename From, typename To>
342 constexpr bool is_convertible_v = is_convertible<From, To>::value;
343 
344 // return underlying type, works atleast for enums.
345 template<typename _Ty>
346 struct underlying_type {
347     using type = __underlying_type(_Ty);
348 };
349 template<typename _Ty>
350 using underlying_type_t = typename underlying_type<_Ty>::type;
351 BASE_END_NAMESPACE()
352 
353 #endif // API_BASE_TYPE_TRAITS_H
354