Debian Codegraph @

core/typedefs.h

raw symbols SCIP
1 /**************************************************************************/
2 /* typedefs.h */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #pragma once
32
33 /**
34 * Basic definitions and simple functions to be used everywhere.
35 */
36
37 // IWYU pragma: always_keep
38
39 // Ensure that C++ standard is at least C++17.
40 // If on MSVC, also ensures that the `Zc:__cplusplus` flag is present.
41 static_assert(__cplusplus >= 201703L, "Minimum of C++17 required.");
42
43 // IWYU pragma: begin_exports
44
45 // Include first in case the platform needs to pre-define/include some things.
46 #include "platform_config.h"
47
48 // Should be available everywhere.
49 #include "core/error/error_list.h"
50
51 #include <cstddef>
52 #include <cstdint>
53 #include <cstring>
54 #include <type_traits>
55 #include <utility>
56
57 // IWYU pragma: end_exports
58
59 // Turn argument to string constant:
60 // https://gcc.gnu.org/onlinedocs/cpp/Stringizing.html#Stringizing
61 #ifndef _STR
62 #define _STR(m_x) #m_x
63 #define _MKSTR(m_x) _STR(m_x)
64 #endif
65
66 // Should always inline no matter what.
67 #ifndef _ALWAYS_INLINE_
68 #if defined(__GNUC__)
69 #define _ALWAYS_INLINE_ __attribute__((always_inline)) inline
70 #elif defined(_MSC_VER)
71 #define _ALWAYS_INLINE_ __forceinline
72 #else
73 #define _ALWAYS_INLINE_ inline
74 #endif
75 #endif
76
77 // Should always inline, except in dev builds because it makes debugging harder,
78 // or `size_enabled` builds where inlining is actively avoided.
79 #ifndef _FORCE_INLINE_
80 #if defined(DEV_ENABLED) || defined(SIZE_EXTRA)
81 #define _FORCE_INLINE_ inline
82 #else
83 #define _FORCE_INLINE_ _ALWAYS_INLINE_
84 #endif
85 #endif
86
87 // Should never inline.
88 #ifndef _NO_INLINE_
89 #if defined(__GNUC__)
90 #define _NO_INLINE_ __attribute__((noinline))
91 #elif defined(_MSC_VER)
92 #define _NO_INLINE_ __declspec(noinline)
93 #else
94 #define _NO_INLINE_
95 #endif
96 #endif
97
98 // In some cases [[nodiscard]] will get false positives,
99 // we can prevent the warning in specific cases by preceding the call with a cast.
100 #ifndef _ALLOW_DISCARD_
101 #define _ALLOW_DISCARD_ (void)
102 #endif
103
104 // Windows badly defines a lot of stuff we'll never use. Undefine it.
105 #ifdef _WIN32
106 #undef min // override standard definition
107 #undef max // override standard definition
108 #undef ERROR // override (really stupid) wingdi.h standard definition
109 #undef DELETE // override (another really stupid) winnt.h standard definition
110 #undef MessageBox // override winuser.h standard definition
111 #undef Error
112 #undef OK
113 #undef CONNECT_DEFERRED // override from Windows SDK, clashes with Object enum
114 #undef MONO_FONT
115 #endif
116
117 // Make room for our constexpr's below by overriding potential system-specific macros.
118 #undef SIGN
119 #undef MIN
120 #undef MAX
121 #undef CLAMP
122
123 template <typename T>
124 constexpr const T SIGN(const T m_v) {
125 return m_v > 0 ? +1.0f : (m_v < 0 ? -1.0f : 0.0f);
126 }
127
128 template <typename T, typename T2>
129 constexpr auto MIN(const T m_a, const T2 m_b) {
130 return m_a < m_b ? m_a : m_b;
131 }
132
133 template <typename T, typename T2>
134 constexpr auto MAX(const T m_a, const T2 m_b) {
135 return m_a > m_b ? m_a : m_b;
136 }
137
138 template <typename T, typename T2, typename T3>
139 constexpr auto CLAMP(const T m_a, const T2 m_min, const T3 m_max) {
140 return m_a < m_min ? m_min : (m_a > m_max ? m_max : m_a);
141 }
142
143 // Generic swap template.
144 #ifndef SWAP
145 #define SWAP(m_x, m_y) std::swap((m_x), (m_y))
146 #endif // SWAP
147
148 // Like std::size, but without requiring any additional includes.
149 template <typename T, size_t SIZE>
150 constexpr size_t std_size(const T (&)[SIZE]) {
151 return SIZE;
152 }
153
154 /* Functions to handle powers of 2 and shifting. */
155
156 // Returns `true` if a positive integer is a power of 2, `false` otherwise.
157 template <typename T>
158 inline bool is_power_of_2(const T x) {
159 return x && ((x & (x - 1)) == 0);
160 }
161
162 // Function to find the next power of 2 to an integer.
163 constexpr uint64_t next_power_of_2(uint64_t p_number) {
164 if (p_number == 0) {
165 return 0;
166 }
167
168 --p_number;
169 p_number |= p_number >> 1;
170 p_number |= p_number >> 2;
171 p_number |= p_number >> 4;
172 p_number |= p_number >> 8;
173 p_number |= p_number >> 16;
174 p_number |= p_number >> 32;
175
176 return ++p_number;
177 }
178
179 constexpr uint32_t next_power_of_2(uint32_t p_number) {
180 if (p_number == 0) {
181 return 0;
182 }
183
184 --p_number;
185 p_number |= p_number >> 1;
186 p_number |= p_number >> 2;
187 p_number |= p_number >> 4;
188 p_number |= p_number >> 8;
189 p_number |= p_number >> 16;
190
191 return ++p_number;
192 }
193
194 // Function to find the previous power of 2 to an integer.
195 constexpr uint64_t previous_power_of_2(uint64_t p_number) {
196 p_number |= p_number >> 1;
197 p_number |= p_number >> 2;
198 p_number |= p_number >> 4;
199 p_number |= p_number >> 8;
200 p_number |= p_number >> 16;
201 p_number |= p_number >> 32;
202 return p_number - (p_number >> 1);
203 }
204
205 constexpr uint32_t previous_power_of_2(uint32_t p_number) {
206 p_number |= p_number >> 1;
207 p_number |= p_number >> 2;
208 p_number |= p_number >> 4;
209 p_number |= p_number >> 8;
210 p_number |= p_number >> 16;
211 return p_number - (p_number >> 1);
212 }
213
214 // Function to find the closest power of 2 to an integer.
215 constexpr uint64_t closest_power_of_2(uint64_t p_number) {
216 uint64_t nx = next_power_of_2(p_number);
217 uint64_t px = previous_power_of_2(p_number);
218 return (nx - p_number) > (p_number - px) ? px : nx;
219 }
220
221 constexpr uint32_t closest_power_of_2(uint32_t p_number) {
222 uint32_t nx = next_power_of_2(p_number);
223 uint32_t px = previous_power_of_2(p_number);
224 return (nx - p_number) > (p_number - px) ? px : nx;
225 }
226
227 // Get a shift value from a power of 2.
228 constexpr int32_t get_shift_from_power_of_2(uint64_t p_bits) {
229 for (uint64_t i = 0; i < (uint64_t)64; i++) {
230 if (p_bits == (uint64_t)((uint64_t)1 << i)) {
231 return i;
232 }
233 }
234
235 return -1;
236 }
237
238 constexpr int32_t get_shift_from_power_of_2(uint32_t p_bits) {
239 for (uint32_t i = 0; i < (uint32_t)32; i++) {
240 if (p_bits == (uint32_t)((uint32_t)1 << i)) {
241 return i;
242 }
243 }
244
245 return -1;
246 }
247
248 template <typename T>
249 static _FORCE_INLINE_ T nearest_power_of_2_templated(T p_number) {
250 --p_number;
251
252 // The number of operations on x is the base two logarithm
253 // of the number of bits in the type. Add three to account
254 // for sizeof(T) being in bytes.
255 constexpr size_t shift_steps = get_shift_from_power_of_2((uint64_t)sizeof(T)) + 3;
256
257 // If the compiler is smart, it unrolls this loop.
258 // If it's dumb, this is a bit slow.
259 for (size_t i = 0; i < shift_steps; i++) {
260 p_number |= p_number >> (1 << i);
261 }
262
263 return ++p_number;
264 }
265
266 // Function to find the nearest (bigger) power of 2 to an integer.
267 constexpr uint64_t nearest_shift(uint64_t p_number) {
268 uint64_t i = 63;
269 do {
270 i--;
271 if (p_number & ((uint64_t)1 << i)) {
272 return i + (uint64_t)1;
273 }
274 } while (i != 0);
275
276 return 0;
277 }
278
279 constexpr uint32_t nearest_shift(uint32_t p_number) {
280 uint32_t i = 31;
281 do {
282 i--;
283 if (p_number & ((uint32_t)1 << i)) {
284 return i + (uint32_t)1;
285 }
286 } while (i != 0);
287
288 return 0;
289 }
290
291 // constexpr function to find the floored log2 of a number
292 template <typename T>
293 constexpr T floor_log2(T x) {
294 return x < 2 ? x : 1 + floor_log2(x >> 1);
295 }
296
297 // Get the number of bits needed to represent the number.
298 // IE, if you pass in 8, you will get 4.
299 // If you want to know how many bits are needed to store 8 values however, pass in (8 - 1).
300 template <typename T>
301 constexpr T get_num_bits(T x) {
302 return floor_log2(x);
303 }
304
305 // Swap 16, 32 and 64 bits value for endianness.
306 #if defined(__GNUC__)
307 #define BSWAP16(x) __builtin_bswap16(x)
308 #define BSWAP32(x) __builtin_bswap32(x)
309 #define BSWAP64(x) __builtin_bswap64(x)
310 #elif defined(_MSC_VER)
311 #define BSWAP16(x) _byteswap_ushort(x)
312 #define BSWAP32(x) _byteswap_ulong(x)
313 #define BSWAP64(x) _byteswap_uint64(x)
314 #else
315 static inline uint16_t BSWAP16(uint16_t x) {
316 return (x >> 8) | (x << 8);
317 }
318
319 static inline uint32_t BSWAP32(uint32_t x) {
320 return ((x << 24) | ((x << 8) & 0x00FF0000) | ((x >> 8) & 0x0000FF00) | (x >> 24));
321 }
322
323 static inline uint64_t BSWAP64(uint64_t x) {
324 x = (x & 0x00000000FFFFFFFF) << 32 | (x & 0xFFFFFFFF00000000) >> 32;
325 x = (x & 0x0000FFFF0000FFFF) << 16 | (x & 0xFFFF0000FFFF0000) >> 16;
326 x = (x & 0x00FF00FF00FF00FF) << 8 | (x & 0xFF00FF00FF00FF00) >> 8;
327 return x;
328 }
329 #endif
330
331 // Generic comparator used in Map, List, etc.
332 template <typename T>
333 struct Comparator {
334 _ALWAYS_INLINE_ bool operator()(const T &p_a, const T &p_b) const { return (p_a < p_b); }
335 };
336
337 // Global lock macro, relies on the static Mutex::_global_mutex.
338 void _global_lock();
339 void _global_unlock();
340
341 struct _GlobalLock {
342 _GlobalLock() { _global_lock(); }
343 ~_GlobalLock() { _global_unlock(); }
344 };
345
346 #define GLOBAL_LOCK_FUNCTION _GlobalLock _global_lock_;
347
348 #if defined(__GNUC__)
349 #define likely(x) __builtin_expect(!!(x), 1)
350 #define unlikely(x) __builtin_expect(!!(x), 0)
351 #else
352 #define likely(x) x
353 #define unlikely(x) x
354 #endif
355
356 #if defined(__GNUC__)
357 #define _PRINTF_FORMAT_ATTRIBUTE_2_0 __attribute__((format(printf, 2, 0)))
358 #define _PRINTF_FORMAT_ATTRIBUTE_2_3 __attribute__((format(printf, 2, 3)))
359 #else
360 #define _PRINTF_FORMAT_ATTRIBUTE_2_0
361 #define _PRINTF_FORMAT_ATTRIBUTE_2_3
362 #endif
363
364 // This is needed due to a strange OpenGL API that expects a pointer
365 // type for an argument that is actually an offset.
366 #define CAST_INT_TO_UCHAR_PTR(ptr) ((uint8_t *)(uintptr_t)(ptr))
367
368 // Home-made index sequence trick, so it can be used everywhere without the costly include of std::tuple.
369 // https://stackoverflow.com/questions/15014096/c-index-of-type-during-variadic-template-expansion
370 template <size_t... Is>
371 struct IndexSequence {};
372
373 template <size_t N, size_t... Is>
374 struct BuildIndexSequence : BuildIndexSequence<N - 1, N - 1, Is...> {};
375
376 template <size_t... Is>
377 struct BuildIndexSequence<0, Is...> : IndexSequence<Is...> {};
378
379 // Limit the depth of recursive algorithms when dealing with Array/Dictionary
380 #define MAX_RECURSION 100
381
382 // Macro GD_IS_DEFINED() allows to check if a macro is defined. It needs to be defined to anything (say 1) to work.
383 #define __GDARG_PLACEHOLDER_1 false,
384 #define __gd_take_second_arg(__ignored, val, ...) val
385 #define ____gd_is_defined(arg1_or_junk) __gd_take_second_arg(arg1_or_junk true, false)
386 #define ___gd_is_defined(val) ____gd_is_defined(__GDARG_PLACEHOLDER_##val)
387 #define GD_IS_DEFINED(x) ___gd_is_defined(x)
388
389 // Whether the default value of a type is just all-0 bytes.
390 // This can most commonly be exploited by using memset for these types instead of loop-construct.
391 // Trivially constructible types are also zero-constructible.
392 template <typename T>
393 struct is_zero_constructible : std::is_trivially_constructible<T> {};
394
395 template <typename T>
396 struct is_zero_constructible<const T> : is_zero_constructible<T> {};
397
398 template <typename T>
399 struct is_zero_constructible<volatile T> : is_zero_constructible<T> {};
400
401 template <typename T>
402 struct is_zero_constructible<const volatile T> : is_zero_constructible<T> {};
403
404 template <typename T>
405 inline constexpr bool is_zero_constructible_v = is_zero_constructible<T>::value;
406
407 // Warning suppression helper macros.
408 #if defined(__clang__)
409 #define GODOT_CLANG_PRAGMA(m_content) _Pragma(#m_content)
410 #define GODOT_CLANG_WARNING_PUSH GODOT_CLANG_PRAGMA(clang diagnostic push)
411 #define GODOT_CLANG_WARNING_IGNORE(m_warning) GODOT_CLANG_PRAGMA(clang diagnostic ignored m_warning)
412 #define GODOT_CLANG_WARNING_POP GODOT_CLANG_PRAGMA(clang diagnostic pop)
413 #define GODOT_CLANG_WARNING_PUSH_AND_IGNORE(m_warning) GODOT_CLANG_WARNING_PUSH GODOT_CLANG_WARNING_IGNORE(m_warning)
414 #else
415 #define GODOT_CLANG_PRAGMA(m_content)
416 #define GODOT_CLANG_WARNING_PUSH
417 #define GODOT_CLANG_WARNING_IGNORE(m_warning)
418 #define GODOT_CLANG_WARNING_POP
419 #define GODOT_CLANG_WARNING_PUSH_AND_IGNORE(m_warning)
420 #endif
421
422 #if defined(__GNUC__) && !defined(__clang__)
423 #define GODOT_GCC_PRAGMA(m_content) _Pragma(#m_content)
424 #define GODOT_GCC_WARNING_PUSH GODOT_GCC_PRAGMA(GCC diagnostic push)
425 #define GODOT_GCC_WARNING_IGNORE(m_warning) GODOT_GCC_PRAGMA(GCC diagnostic ignored m_warning)
426 #define GODOT_GCC_WARNING_POP GODOT_GCC_PRAGMA(GCC diagnostic pop)
427 #define GODOT_GCC_WARNING_PUSH_AND_IGNORE(m_warning) GODOT_GCC_WARNING_PUSH GODOT_GCC_WARNING_IGNORE(m_warning)
428 #else
429 #define GODOT_GCC_PRAGMA(m_content)
430 #define GODOT_GCC_WARNING_PUSH
431 #define GODOT_GCC_WARNING_IGNORE(m_warning)
432 #define GODOT_GCC_WARNING_POP
433 #define GODOT_GCC_WARNING_PUSH_AND_IGNORE(m_warning)
434 #endif
435
436 #if defined(_MSC_VER) && !defined(__clang__)
437 #define GODOT_MSVC_PRAGMA(m_command) __pragma(m_command)
438 #define GODOT_MSVC_WARNING_PUSH GODOT_MSVC_PRAGMA(warning(push))
439 #define GODOT_MSVC_WARNING_IGNORE(m_warning) GODOT_MSVC_PRAGMA(warning(disable : m_warning))
440 #define GODOT_MSVC_WARNING_POP GODOT_MSVC_PRAGMA(warning(pop))
441 #define GODOT_MSVC_WARNING_PUSH_AND_IGNORE(m_warning) GODOT_MSVC_WARNING_PUSH GODOT_MSVC_WARNING_IGNORE(m_warning)
442 #else
443 #define GODOT_MSVC_PRAGMA(m_command)
444 #define GODOT_MSVC_WARNING_PUSH
445 #define GODOT_MSVC_WARNING_IGNORE(m_warning)
446 #define GODOT_MSVC_WARNING_POP
447 #define GODOT_MSVC_WARNING_PUSH_AND_IGNORE(m_warning)
448 #endif
449
450 template <typename T, typename = void>
451 struct is_fully_defined : std::false_type {};
452
453 template <typename T>
454 struct is_fully_defined<T, std::void_t<decltype(sizeof(T))>> : std::true_type {};
455
456 template <typename T>
457 constexpr bool is_fully_defined_v = is_fully_defined<T>::value;
458
459 #ifndef SCU_BUILD_ENABLED
460 /// Enforces the requirement that a class is not fully defined.
461 /// This can be used to reduce include coupling and keep compile times low.
462 /// The check must be made at the top of the corresponding .cpp file of a header.
463 #define STATIC_ASSERT_INCOMPLETE_TYPE(m_keyword, m_type) \
464 m_keyword m_type; \
465 static_assert(!is_fully_defined_v<m_type>, #m_type " was unexpectedly fully defined. Please check the include hierarchy of '" __FILE__ "' and remove includes that resolve the " #m_keyword ".");
466 #else
467 #define STATIC_ASSERT_INCOMPLETE_TYPE(m_keyword, m_type)
468 #endif
469
470 #define _GD_VARNAME_CONCAT_B_(m_ignore, m_name) m_name
471 #define _GD_VARNAME_CONCAT_A_(m_a, m_b, m_c) _GD_VARNAME_CONCAT_B_(hello there, m_a##m_b##m_c)
472 #define _GD_VARNAME_CONCAT_(m_a, m_b, m_c) _GD_VARNAME_CONCAT_A_(m_a, m_b, m_c)
473 #define GD_UNIQUE_NAME(m_name) _GD_VARNAME_CONCAT_(m_name, _, __COUNTER__)