defs.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*************************************************************************/
  2. /* defs.hpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. #ifndef GODOT_DEFS_HPP
  31. #define GODOT_DEFS_HPP
  32. #include <cstddef>
  33. #include <cstdint>
  34. #include <cstring>
  35. #if !defined(GDE_EXPORT)
  36. #if defined(_WIN32)
  37. #define GDE_EXPORT __declspec(dllexport)
  38. #elif defined(__GNUC__)
  39. #define GDE_EXPORT __attribute__((visibility("default")))
  40. #else
  41. #define GDE_EXPORT
  42. #endif
  43. #endif
  44. // Turn argument to string constant:
  45. // https://gcc.gnu.org/onlinedocs/cpp/Stringizing.html#Stringizing
  46. #ifndef _STR
  47. #define _STR(m_x) #m_x
  48. #define _MKSTR(m_x) _STR(m_x)
  49. #endif
  50. // Should always inline no matter what.
  51. #ifndef _ALWAYS_INLINE_
  52. #if defined(__GNUC__)
  53. #define _ALWAYS_INLINE_ __attribute__((always_inline)) inline
  54. #elif defined(_MSC_VER)
  55. #define _ALWAYS_INLINE_ __forceinline
  56. #else
  57. #define _ALWAYS_INLINE_ inline
  58. #endif
  59. #endif
  60. // Should always inline, except in debug builds because it makes debugging harder.
  61. #ifndef _FORCE_INLINE_
  62. #ifdef DISABLE_FORCED_INLINE
  63. #define _FORCE_INLINE_ inline
  64. #else
  65. #define _FORCE_INLINE_ _ALWAYS_INLINE_
  66. #endif
  67. #endif
  68. #ifndef _NO_DISCARD_
  69. #define _NO_DISCARD_ [[nodiscard]]
  70. #endif
  71. // Windows badly defines a lot of stuff we'll never use. Undefine it.
  72. #ifdef _WIN32
  73. #undef min // override standard definition
  74. #undef max // override standard definition
  75. #undef ERROR // override (really stupid) wingdi.h standard definition
  76. #undef DELETE // override (another really stupid) winnt.h standard definition
  77. #undef MessageBox // override winuser.h standard definition
  78. #undef MIN // override standard definition
  79. #undef MAX // override standard definition
  80. #undef CLAMP // override standard definition
  81. #undef Error
  82. #undef OK
  83. #undef CONNECT_DEFERRED // override from Windows SDK, clashes with Object enum
  84. #endif
  85. #if defined(__GNUC__)
  86. #define likely(x) __builtin_expect(!!(x), 1)
  87. #define unlikely(x) __builtin_expect(!!(x), 0)
  88. #else
  89. #define likely(x) x
  90. #define unlikely(x) x
  91. #endif
  92. #ifdef REAL_T_IS_DOUBLE
  93. typedef double real_t;
  94. #else
  95. typedef float real_t;
  96. #endif
  97. // Generic swap template.
  98. #ifndef SWAP
  99. #define SWAP(m_x, m_y) __swap_tmpl((m_x), (m_y))
  100. template <class T>
  101. inline void __swap_tmpl(T &x, T &y) {
  102. T aux = x;
  103. x = y;
  104. y = aux;
  105. }
  106. #endif // SWAP
  107. // Home-made index sequence trick, so it can be used everywhere without the costly include of std::tuple.
  108. // https://stackoverflow.com/questions/15014096/c-index-of-type-during-variadic-template-expansion
  109. template <size_t... Is>
  110. struct IndexSequence {};
  111. template <size_t N, size_t... Is>
  112. struct BuildIndexSequence : BuildIndexSequence<N - 1, N - 1, Is...> {};
  113. template <size_t... Is>
  114. struct BuildIndexSequence<0, Is...> : IndexSequence<Is...> {};
  115. #endif // GODOT_DEFS_HPP