Defs.hpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*************************************************************************/
  2. /* Defs.hpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 DEFS_H
  31. #define DEFS_H
  32. namespace godot {
  33. enum class Error {
  34. OK,
  35. FAILED, ///< Generic fail error
  36. ERR_UNAVAILABLE, ///< What is requested is unsupported/unavailable
  37. ERR_UNCONFIGURED, ///< The object being used hasnt been properly set up yet
  38. ERR_UNAUTHORIZED, ///< Missing credentials for requested resource
  39. ERR_PARAMETER_RANGE_ERROR, ///< Parameter given out of range (5)
  40. ERR_OUT_OF_MEMORY, ///< Out of memory
  41. ERR_FILE_NOT_FOUND,
  42. ERR_FILE_BAD_DRIVE,
  43. ERR_FILE_BAD_PATH,
  44. ERR_FILE_NO_PERMISSION, // (10)
  45. ERR_FILE_ALREADY_IN_USE,
  46. ERR_FILE_CANT_OPEN,
  47. ERR_FILE_CANT_WRITE,
  48. ERR_FILE_CANT_READ,
  49. ERR_FILE_UNRECOGNIZED, // (15)
  50. ERR_FILE_CORRUPT,
  51. ERR_FILE_MISSING_DEPENDENCIES,
  52. ERR_FILE_EOF,
  53. ERR_CANT_OPEN, ///< Can't open a resource/socket/file
  54. ERR_CANT_CREATE, // (20)
  55. ERR_QUERY_FAILED,
  56. ERR_ALREADY_IN_USE,
  57. ERR_LOCKED, ///< resource is locked
  58. ERR_TIMEOUT,
  59. ERR_CANT_CONNECT, // (25)
  60. ERR_CANT_RESOLVE,
  61. ERR_CONNECTION_ERROR,
  62. ERR_CANT_AQUIRE_RESOURCE,
  63. ERR_CANT_FORK,
  64. ERR_INVALID_DATA, ///< Data passed is invalid (30)
  65. ERR_INVALID_PARAMETER, ///< Parameter passed is invalid
  66. ERR_ALREADY_EXISTS, ///< When adding, item already exists
  67. ERR_DOES_NOT_EXIST, ///< When retrieving/erasing, it item does not exist
  68. ERR_DATABASE_CANT_READ, ///< database is full
  69. ERR_DATABASE_CANT_WRITE, ///< database is full (35)
  70. ERR_COMPILATION_FAILED,
  71. ERR_METHOD_NOT_FOUND,
  72. ERR_LINK_FAILED,
  73. ERR_SCRIPT_FAILED,
  74. ERR_CYCLIC_LINK, // (40)
  75. ERR_INVALID_DECLARATION,
  76. ERR_DUPLICATE_SYMBOL,
  77. ERR_PARSE_ERROR,
  78. ERR_BUSY,
  79. ERR_SKIP, // (45)
  80. ERR_HELP, ///< user requested help!!
  81. ERR_BUG, ///< a bug in the software certainly happened, due to a double check failing or unexpected behavior.
  82. ERR_PRINTER_ON_FIRE, /// the parallel port printer is engulfed in flames
  83. };
  84. } // namespace godot
  85. #include <GodotGlobal.hpp>
  86. // alloca() is non-standard. When using MSVC, it's in malloc.h.
  87. #if defined(__linux__) || defined(__APPLE__)
  88. #include <alloca.h>
  89. #else
  90. #include <malloc.h>
  91. #endif
  92. typedef float real_t;
  93. // This epsilon should match the one used by Godot for consistency.
  94. // Using `f` when `real_t` is float.
  95. #define CMP_EPSILON 0.00001f
  96. #define CMP_EPSILON2 (CMP_EPSILON * CMP_EPSILON)
  97. #define Math_PI 3.1415926535897932384626433833
  98. #define Math_TAU 6.2831853071795864769252867666
  99. #define _PLANE_EQ_DOT_EPSILON 0.999
  100. #define _PLANE_EQ_D_EPSILON 0.0001
  101. #ifdef __GNUC__
  102. #define likely(x) __builtin_expect(!!(x), 1)
  103. #define unlikely(x) __builtin_expect(!!(x), 0)
  104. #else
  105. #define likely(x) x
  106. #define unlikely(x) x
  107. #endif
  108. // Don't use this directly; instead, use any of the CRASH_* macros
  109. #ifdef _MSC_VER
  110. #define GENERATE_TRAP \
  111. __debugbreak(); \
  112. /* Avoid warning about control paths */ \
  113. for (;;) { \
  114. }
  115. #else
  116. #define GENERATE_TRAP __builtin_trap();
  117. #endif
  118. // ERR/WARN macros
  119. #ifndef WARN_PRINT
  120. #define WARN_PRINT(msg) godot::Godot::print_warning(msg, __func__, __FILE__, __LINE__)
  121. #endif
  122. #ifndef WARN_PRINTS
  123. #define WARN_PRINTS(msg) WARN_PRINT((msg).utf8().get_data())
  124. #endif
  125. #ifndef ERR_PRINT
  126. #define ERR_PRINT(msg) godot::Godot::print_error(msg, __func__, __FILE__, __LINE__)
  127. #endif
  128. #ifndef ERR_PRINTS
  129. #define ERR_PRINTS(msg) ERR_PRINT((msg).utf8().get_data())
  130. #endif
  131. #ifndef FATAL_PRINT
  132. #define FATAL_PRINT(msg) ERR_PRINT(godot::String("FATAL: ") + (msg))
  133. #endif
  134. #ifndef ERR_MSG_INDEX
  135. #define ERR_MSG_INDEX(index, size) (godot::String("Index ") + #index + "=" + godot::String::num_int64(index) + " out of size (" + #size + "=" + godot::String::num_int64(size) + ")")
  136. #endif
  137. #ifndef ERR_MSG_NULL
  138. #define ERR_MSG_NULL(param) (godot::String("Parameter '") + #param + "' is null.")
  139. #endif
  140. #ifndef ERR_MSG_COND
  141. #define ERR_MSG_COND(cond) (godot::String("Condition '") + #cond + "' is true.")
  142. #endif
  143. #ifndef ERR_FAIL_INDEX
  144. #define ERR_FAIL_INDEX(index, size) \
  145. do { \
  146. if (unlikely((index) < 0 || (index) >= (size))) { \
  147. ERR_PRINT(ERR_MSG_INDEX(index, size)); \
  148. return; \
  149. } \
  150. } while (0)
  151. #endif
  152. #ifndef ERR_FAIL_INDEX_V
  153. #define ERR_FAIL_INDEX_V(index, size, ret) \
  154. do { \
  155. if (unlikely((index) < 0 || (index) >= (size))) { \
  156. ERR_PRINT(ERR_MSG_INDEX(index, size)); \
  157. return ret; \
  158. } \
  159. } while (0)
  160. #endif
  161. #ifndef ERR_FAIL_UNSIGNED_INDEX_V
  162. #define ERR_FAIL_UNSIGNED_INDEX_V(index, size, ret) \
  163. do { \
  164. if (unlikely((index) >= (size))) { \
  165. ERR_PRINT(ERR_MSG_INDEX(index, size)); \
  166. return ret; \
  167. } \
  168. } while (0)
  169. #endif
  170. #ifndef CRASH_BAD_INDEX
  171. #define CRASH_BAD_INDEX(index, size) \
  172. do { \
  173. if (unlikely((index) < 0 || (index) >= (size))) { \
  174. FATAL_PRINT(ERR_MSG_INDEX(index, size)); \
  175. GENERATE_TRAP; \
  176. } \
  177. } while (0)
  178. #endif
  179. #ifndef ERR_FAIL_NULL
  180. #define ERR_FAIL_NULL(param) \
  181. do { \
  182. if (unlikely(!param)) { \
  183. ERR_PRINT(ERR_MSG_NULL(param)); \
  184. return; \
  185. } \
  186. } while (0)
  187. #endif
  188. #ifndef ERR_FAIL_NULL_V
  189. #define ERR_FAIL_NULL_V(param, ret) \
  190. do { \
  191. if (unlikely(!param)) { \
  192. ERR_PRINT(ERR_MSG_NULL(param)); \
  193. return ret; \
  194. } \
  195. } while (0)
  196. #endif
  197. #ifndef ERR_FAIL_COND
  198. #define ERR_FAIL_COND(cond) \
  199. do { \
  200. if (unlikely(cond)) { \
  201. ERR_PRINT(ERR_MSG_COND(cond)); \
  202. return; \
  203. } \
  204. } while (0)
  205. #endif
  206. #ifndef CRASH_COND
  207. #define CRASH_COND(cond) \
  208. do { \
  209. if (unlikely(cond)) { \
  210. FATAL_PRINT(ERR_MSG_COND(cond)); \
  211. GENERATE_TRAP; \
  212. } \
  213. } while (0)
  214. #endif
  215. #ifndef ERR_FAIL_COND_V
  216. #define ERR_FAIL_COND_V(cond, ret) \
  217. do { \
  218. if (unlikely(cond)) { \
  219. ERR_PRINT(ERR_MSG_COND(cond)); \
  220. return ret; \
  221. } \
  222. } while (0)
  223. #endif
  224. #ifndef ERR_CONTINUE
  225. #define ERR_CONTINUE(cond) \
  226. { \
  227. if (unlikely(cond)) { \
  228. ERR_PRINT(ERR_MSG_COND(cond)); \
  229. continue; \
  230. } \
  231. }
  232. #endif
  233. #ifndef ERR_BREAK
  234. #define ERR_BREAK(cond) \
  235. { \
  236. if (unlikely(cond)) { \
  237. ERR_PRINT(ERR_MSG_COND(cond)); \
  238. break; \
  239. } \
  240. }
  241. #endif
  242. #ifndef ERR_FAIL
  243. #define ERR_FAIL() \
  244. do { \
  245. ERR_PRINT("Method/Function Failed."); \
  246. return; \
  247. } while (0)
  248. #endif
  249. #ifndef ERR_FAIL_V
  250. #define ERR_FAIL_V(ret) \
  251. do { \
  252. ERR_PRINT("Method/Function Failed."); \
  253. return ret; \
  254. } while (0)
  255. #endif
  256. #ifndef CRASH_NOW
  257. #define CRASH_NOW() \
  258. do { \
  259. FATAL_PRINT("Method/Function Failed."); \
  260. GENERATE_TRAP; \
  261. } while (0)
  262. #endif
  263. #endif // DEFS_H