| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
static_assert(__cplusplus >= 201703L, "Minimum of C++17 required."); |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
#include "platform_config.h" |
| 47 |
|
| 48 |
|
| 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 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
#ifndef _STR |
| 62 |
#define _STR(m_x) #m_x |
| 63 |
#define _MKSTR(m_x) _STR(m_x) |
| 64 |
#endif |
| 65 |
|
| 66 |
|
| 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 |
|
| 78 |
|
| 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 |
|
| 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 |
|
| 99 |
|
| 100 |
#ifndef _ALLOW_DISCARD_ |
| 101 |
#define _ALLOW_DISCARD_ (void) |
| 102 |
#endif |
| 103 |
|
| 104 |
|
| 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 |
|
| 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 |
|
| 144 |
#ifndef SWAP |
| 145 |
#define SWAP(m_x, m_y) std::swap((m_x), (m_y)) |
| 146 |
#endif |
| 147 |
|
| 148 |
|
| 149 |
template <typename T, size_t SIZE> |
| 150 |
constexpr size_t std_size(const T (&)[SIZE]) { |
| 151 |
return SIZE; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
template <typename T> |
| 158 |
inline bool is_power_of_2(const T x) { |
| 159 |
return x && ((x & (x - 1)) == 0); |
| 160 |
} |
| 161 |
|
| 162 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 253 |
|
| 254 |
|
| 255 |
constexpr size_t shift_steps = get_shift_from_power_of_2((uint64_t)sizeof(T)) + 3; |
| 256 |
|
| 257 |
|
| 258 |
|
| 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 |
|
| 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 |
|
| 292 |
template <typename T> |
| 293 |
constexpr T floor_log2(T x) { |
| 294 |
return x < 2 ? x : 1 + floor_log2(x >> 1); |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
template <typename T> |
| 301 |
constexpr T get_num_bits(T x) { |
| 302 |
return floor_log2(x); |
| 303 |
} |
| 304 |
|
| 305 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 365 |
|
| 366 |
#define CAST_INT_TO_UCHAR_PTR(ptr) ((uint8_t *)(uintptr_t)(ptr)) |
| 367 |
|
| 368 |
|
| 369 |
|
| 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 |
|
| 380 |
#define MAX_RECURSION 100 |
| 381 |
|
| 382 |
|
| 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 |
|
| 390 |
|
| 391 |
|
| 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 |
|
| 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 |
|
| 461 |
|
| 462 |
|
| 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__) |