error_macros.hpp 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. /**************************************************************************/
  2. /* error_macros.hpp */
  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. #pragma once
  31. #include <godot_cpp/core/defs.hpp>
  32. #include <atomic>
  33. namespace godot {
  34. class String;
  35. void _err_print_error(const char *p_function, const char *p_file, int p_line, const char *p_error, bool p_editor_notify = false, bool p_is_warning = false);
  36. void _err_print_error(const char *p_function, const char *p_file, int p_line, const String &p_error, bool p_editor_notify = false, bool p_is_warning = false);
  37. void _err_print_error(const char *p_function, const char *p_file, int p_line, const char *p_error, const char *p_message, bool p_editor_notify = false, bool p_is_warning = false);
  38. void _err_print_error(const char *p_function, const char *p_file, int p_line, const String &p_error, const char *p_message, bool p_editor_notify = false, bool p_is_warning = false);
  39. void _err_print_error(const char *p_function, const char *p_file, int p_line, const char *p_error, const String &p_message, bool p_editor_notify = false, bool p_is_warning = false);
  40. void _err_print_error(const char *p_function, const char *p_file, int p_line, const String &p_error, const String &p_message, bool p_editor_notify = false, bool p_is_warning = false);
  41. void _err_print_index_error(const char *p_function, const char *p_file, int p_line, int64_t p_index, int64_t p_size, const char *p_index_str, const char *p_size_str, const char *p_message = "", bool p_editor_notify = false, bool p_fatal = false);
  42. void _err_print_index_error(const char *p_function, const char *p_file, int p_line, int64_t p_index, int64_t p_size, const char *p_index_str, const char *p_size_str, const String &p_message, bool p_editor_notify = false, bool p_fatal = false);
  43. void _err_flush_stdout();
  44. } // namespace godot
  45. #ifdef __GNUC__
  46. #define FUNCTION_STR __FUNCTION__
  47. #else
  48. #define FUNCTION_STR __FUNCTION__
  49. #endif
  50. #ifdef _MSC_VER
  51. /**
  52. * Don't use GENERATE_TRAP() directly, should only be used be the macros below.
  53. */
  54. #define GENERATE_TRAP() __debugbreak()
  55. #else
  56. /**
  57. * Don't use GENERATE_TRAP() directly, should only be used be the macros below.
  58. */
  59. #define GENERATE_TRAP() __builtin_trap()
  60. #endif
  61. /**
  62. * Error macros.
  63. * WARNING: These macros work in the opposite way to assert().
  64. *
  65. * Unlike exceptions and asserts, these macros try to maintain consistency and stability.
  66. * In most cases, bugs and/or invalid data are not fatal. They should never allow a perfectly
  67. * running application to fail or crash.
  68. * Always try to return processable data, so the engine can keep running well.
  69. * Use the _MSG versions to print a meaningful message to help with debugging.
  70. *
  71. * The `((void)0)` no-op statement is used as a trick to force us to put a semicolon after
  72. * those macros, making them look like proper statements.
  73. * The if wrappers are used to ensure that the macro replacement does not trigger unexpected
  74. * issues when expanded e.g. after an `if (cond) ERR_FAIL();` without braces.
  75. */
  76. // Index out of bounds error macros.
  77. // These macros should be used instead of `ERR_FAIL_COND` for bounds checking.
  78. // Integer index out of bounds error macros.
  79. /**
  80. * Try using `ERR_FAIL_INDEX_MSG`.
  81. * Only use this macro if there is no sensible error message.
  82. *
  83. * Ensures an integer index `m_index` is less than `m_size` and greater than or equal to 0.
  84. * If not, the current function returns.
  85. */
  86. #define ERR_FAIL_INDEX(m_index, m_size) \
  87. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  88. ::godot::_err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size)); \
  89. return; \
  90. } else \
  91. ((void)0)
  92. /**
  93. * Ensures an integer index `m_index` is less than `m_size` and greater than or equal to 0.
  94. * If not, prints `m_msg` and the current function returns.
  95. */
  96. #define ERR_FAIL_INDEX_MSG(m_index, m_size, m_msg) \
  97. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  98. ::godot::_err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), m_msg); \
  99. return; \
  100. } else \
  101. ((void)0)
  102. /**
  103. * Same as `ERR_FAIL_INDEX_MSG` but also notifies the editor.
  104. */
  105. #define ERR_FAIL_INDEX_EDMSG(m_index, m_size, m_msg) \
  106. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  107. ::godot::_err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), m_msg, true); \
  108. return; \
  109. } else \
  110. ((void)0)
  111. /**
  112. * Try using `ERR_FAIL_INDEX_V_MSG`.
  113. * Only use this macro if there is no sensible error message.
  114. *
  115. * Ensures an integer index `m_index` is less than `m_size` and greater than or equal to 0.
  116. * If not, the current function returns `m_retval`.
  117. */
  118. #define ERR_FAIL_INDEX_V(m_index, m_size, m_retval) \
  119. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  120. ::godot::_err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size)); \
  121. return m_retval; \
  122. } else \
  123. ((void)0)
  124. /**
  125. * Ensures an integer index `m_index` is less than `m_size` and greater than or equal to 0.
  126. * If not, prints `m_msg` and the current function returns `m_retval`.
  127. */
  128. #define ERR_FAIL_INDEX_V_MSG(m_index, m_size, m_retval, m_msg) \
  129. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  130. ::godot::_err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), m_msg); \
  131. return m_retval; \
  132. } else \
  133. ((void)0)
  134. /**
  135. * Same as `ERR_FAIL_INDEX_V_MSG` but also notifies the editor.
  136. */
  137. #define ERR_FAIL_INDEX_V_EDMSG(m_index, m_size, m_retval, m_msg) \
  138. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  139. ::godot::_err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), m_msg, true); \
  140. return m_retval; \
  141. } else \
  142. ((void)0)
  143. /**
  144. * Try using `ERR_FAIL_INDEX_MSG` or `ERR_FAIL_INDEX_V_MSG`.
  145. * Only use this macro if there is no sensible fallback i.e. the error is unrecoverable, and
  146. * there is no sensible error message.
  147. *
  148. * Ensures an integer index `m_index` is less than `m_size` and greater than or equal to 0.
  149. * If not, the application crashes.
  150. */
  151. #define CRASH_BAD_INDEX(m_index, m_size) \
  152. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  153. ::godot::_err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), "", false, true); \
  154. ::godot::_err_flush_stdout(); \
  155. GENERATE_TRAP(); \
  156. } else \
  157. ((void)0)
  158. /**
  159. * Try using `ERR_FAIL_INDEX_MSG` or `ERR_FAIL_INDEX_V_MSG`.
  160. * Only use this macro if there is no sensible fallback i.e. the error is unrecoverable.
  161. *
  162. * Ensures an integer index `m_index` is less than `m_size` and greater than or equal to 0.
  163. * If not, prints `m_msg` and the application crashes.
  164. */
  165. #define CRASH_BAD_INDEX_MSG(m_index, m_size, m_msg) \
  166. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  167. ::godot::_err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), m_msg, false, true); \
  168. ::godot::_err_flush_stdout(); \
  169. GENERATE_TRAP(); \
  170. } else \
  171. ((void)0)
  172. // Unsigned integer index out of bounds error macros.
  173. /**
  174. * Try using `ERR_FAIL_UNSIGNED_INDEX_MSG`.
  175. * Only use this macro if there is no sensible error message.
  176. *
  177. * Ensures an unsigned integer index `m_index` is less than `m_size`.
  178. * If not, the current function returns.
  179. */
  180. #define ERR_FAIL_UNSIGNED_INDEX(m_index, m_size) \
  181. if (unlikely((m_index) >= (m_size))) { \
  182. ::godot::_err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size)); \
  183. return; \
  184. } else \
  185. ((void)0)
  186. /**
  187. * Ensures an unsigned integer index `m_index` is less than `m_size`.
  188. * If not, prints `m_msg` and the current function returns.
  189. */
  190. #define ERR_FAIL_UNSIGNED_INDEX_MSG(m_index, m_size, m_msg) \
  191. if (unlikely((m_index) >= (m_size))) { \
  192. ::godot::_err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), m_msg); \
  193. return; \
  194. } else \
  195. ((void)0)
  196. /**
  197. * Same as `ERR_FAIL_UNSIGNED_INDEX_MSG` but also notifies the editor.
  198. */
  199. #define ERR_FAIL_UNSIGNED_INDEX_EDMSG(m_index, m_size, m_msg) \
  200. if (unlikely((m_index) >= (m_size))) { \
  201. ::godot::_err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), m_msg, true); \
  202. return; \
  203. } else \
  204. ((void)0)
  205. /**
  206. * Try using `ERR_FAIL_UNSIGNED_INDEX_V_MSG`.
  207. * Only use this macro if there is no sensible error message.
  208. *
  209. * Ensures an unsigned integer index `m_index` is less than `m_size`.
  210. * If not, the current function returns `m_retval`.
  211. */
  212. #define ERR_FAIL_UNSIGNED_INDEX_V(m_index, m_size, m_retval) \
  213. if (unlikely((m_index) >= (m_size))) { \
  214. ::godot::_err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size)); \
  215. return m_retval; \
  216. } else \
  217. ((void)0)
  218. /**
  219. * Ensures an unsigned integer index `m_index` is less than `m_size`.
  220. * If not, prints `m_msg` and the current function returns `m_retval`.
  221. */
  222. #define ERR_FAIL_UNSIGNED_INDEX_V_MSG(m_index, m_size, m_retval, m_msg) \
  223. if (unlikely((m_index) >= (m_size))) { \
  224. ::godot::_err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), m_msg); \
  225. return m_retval; \
  226. } else \
  227. ((void)0)
  228. /**
  229. * Same as `ERR_FAIL_UNSIGNED_INDEX_V_EDMSG` but also notifies the editor.
  230. */
  231. #define ERR_FAIL_UNSIGNED_INDEX_V_EDMSG(m_index, m_size, m_retval, m_msg) \
  232. if (unlikely((m_index) >= (m_size))) { \
  233. ::godot::_err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), m_msg, true); \
  234. return m_retval; \
  235. } else \
  236. ((void)0)
  237. /**
  238. * Try using `ERR_FAIL_UNSIGNED_INDEX_MSG` or `ERR_FAIL_UNSIGNED_INDEX_V_MSG`.
  239. * Only use this macro if there is no sensible fallback i.e. the error is unrecoverable, and
  240. * there is no sensible error message.
  241. *
  242. * Ensures an unsigned integer index `m_index` is less than `m_size`.
  243. * If not, the application crashes.
  244. */
  245. #define CRASH_BAD_UNSIGNED_INDEX(m_index, m_size) \
  246. if (unlikely((m_index) >= (m_size))) { \
  247. ::godot::_err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), "", false, true); \
  248. ::godot::_err_flush_stdout(); \
  249. GENERATE_TRAP(); \
  250. } else \
  251. ((void)0)
  252. /**
  253. * Try using `ERR_FAIL_UNSIGNED_INDEX_MSG` or `ERR_FAIL_UNSIGNED_INDEX_V_MSG`.
  254. * Only use this macro if there is no sensible fallback i.e. the error is unrecoverable.
  255. *
  256. * Ensures an unsigned integer index `m_index` is less than `m_size`.
  257. * If not, prints `m_msg` and the application crashes.
  258. */
  259. #define CRASH_BAD_UNSIGNED_INDEX_MSG(m_index, m_size, m_msg) \
  260. if (unlikely((m_index) >= (m_size))) { \
  261. ::godot::_err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), m_msg, false, true); \
  262. ::godot::_err_flush_stdout(); \
  263. GENERATE_TRAP(); \
  264. } else \
  265. ((void)0)
  266. // Null reference error macros.
  267. /**
  268. * Try using `ERR_FAIL_NULL_MSG`.
  269. * Only use this macro if there is no sensible error message.
  270. *
  271. * Ensures a pointer `m_param` is not null.
  272. * If it is null, the current function returns.
  273. */
  274. #define ERR_FAIL_NULL(m_param) \
  275. if (unlikely(m_param == nullptr)) { \
  276. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Parameter \"" _STR(m_param) "\" is null."); \
  277. return; \
  278. } else \
  279. ((void)0)
  280. /**
  281. * Ensures a pointer `m_param` is not null.
  282. * If it is null, prints `m_msg` and the current function returns.
  283. */
  284. #define ERR_FAIL_NULL_MSG(m_param, m_msg) \
  285. if (unlikely(m_param == nullptr)) { \
  286. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Parameter \"" _STR(m_param) "\" is null.", m_msg); \
  287. return; \
  288. } else \
  289. ((void)0)
  290. /**
  291. * Same as `ERR_FAIL_NULL_MSG` but also notifies the editor.
  292. */
  293. #define ERR_FAIL_NULL_EDMSG(m_param, m_msg) \
  294. if (unlikely(m_param == nullptr)) { \
  295. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Parameter \"" _STR(m_param) "\" is null.", m_msg, true); \
  296. return; \
  297. } else \
  298. ((void)0)
  299. /**
  300. * Try using `ERR_FAIL_NULL_V_MSG`.
  301. * Only use this macro if there is no sensible error message.
  302. *
  303. * Ensures a pointer `m_param` is not null.
  304. * If it is null, the current function returns `m_retval`.
  305. */
  306. #define ERR_FAIL_NULL_V(m_param, m_retval) \
  307. if (unlikely(m_param == nullptr)) { \
  308. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Parameter \"" _STR(m_param) "\" is null."); \
  309. return m_retval; \
  310. } else \
  311. ((void)0)
  312. /**
  313. * Ensures a pointer `m_param` is not null.
  314. * If it is null, prints `m_msg` and the current function returns `m_retval`.
  315. */
  316. #define ERR_FAIL_NULL_V_MSG(m_param, m_retval, m_msg) \
  317. if (unlikely(m_param == nullptr)) { \
  318. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Parameter \"" _STR(m_param) "\" is null.", m_msg); \
  319. return m_retval; \
  320. } else \
  321. ((void)0)
  322. /**
  323. * Same as `ERR_FAIL_NULL_V_MSG` but also notifies the editor.
  324. */
  325. #define ERR_FAIL_NULL_V_EDMSG(m_param, m_retval, m_msg) \
  326. if (unlikely(m_param == nullptr)) { \
  327. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Parameter \"" _STR(m_param) "\" is null.", m_msg, true); \
  328. return m_retval; \
  329. } else \
  330. ((void)0)
  331. /**
  332. * Try using `ERR_FAIL_COND_MSG`.
  333. * Only use this macro if there is no sensible error message.
  334. * If checking for null use ERR_FAIL_NULL_MSG instead.
  335. * If checking index bounds use ERR_FAIL_INDEX_MSG instead.
  336. *
  337. * Ensures `m_cond` is false.
  338. * If `m_cond` is true, the current function returns.
  339. */
  340. #define ERR_FAIL_COND(m_cond) \
  341. if (unlikely(m_cond)) { \
  342. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true."); \
  343. return; \
  344. } else \
  345. ((void)0)
  346. /**
  347. * Ensures `m_cond` is false.
  348. * If `m_cond` is true, prints `m_msg` and the current function returns.
  349. *
  350. * If checking for null use ERR_FAIL_NULL_MSG instead.
  351. * If checking index bounds use ERR_FAIL_INDEX_MSG instead.
  352. */
  353. #define ERR_FAIL_COND_MSG(m_cond, m_msg) \
  354. if (unlikely(m_cond)) { \
  355. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true.", m_msg); \
  356. return; \
  357. } else \
  358. ((void)0)
  359. /**
  360. * Same as `ERR_FAIL_COND_MSG` but also notifies the editor.
  361. */
  362. #define ERR_FAIL_COND_EDMSG(m_cond, m_msg) \
  363. if (unlikely(m_cond)) { \
  364. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true.", m_msg, true); \
  365. return; \
  366. } else \
  367. ((void)0)
  368. /**
  369. * Try using `ERR_FAIL_COND_V_MSG`.
  370. * Only use this macro if there is no sensible error message.
  371. * If checking for null use ERR_FAIL_NULL_V_MSG instead.
  372. * If checking index bounds use ERR_FAIL_INDEX_V_MSG instead.
  373. *
  374. * Ensures `m_cond` is false.
  375. * If `m_cond` is true, the current function returns `m_retval`.
  376. */
  377. #define ERR_FAIL_COND_V(m_cond, m_retval) \
  378. if (unlikely(m_cond)) { \
  379. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Returning: " _STR(m_retval)); \
  380. return m_retval; \
  381. } else \
  382. ((void)0)
  383. /**
  384. * Ensures `m_cond` is false.
  385. * If `m_cond` is true, prints `m_msg` and the current function returns `m_retval`.
  386. *
  387. * If checking for null use ERR_FAIL_NULL_V_MSG instead.
  388. * If checking index bounds use ERR_FAIL_INDEX_V_MSG instead.
  389. */
  390. #define ERR_FAIL_COND_V_MSG(m_cond, m_retval, m_msg) \
  391. if (unlikely(m_cond)) { \
  392. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Returning: " _STR(m_retval), m_msg); \
  393. return m_retval; \
  394. } else \
  395. ((void)0)
  396. /**
  397. * Same as `ERR_FAIL_COND_V_MSG` but also notifies the editor.
  398. */
  399. #define ERR_FAIL_COND_V_EDMSG(m_cond, m_retval, m_msg) \
  400. if (unlikely(m_cond)) { \
  401. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Returning: " _STR(m_retval), m_msg, true); \
  402. return m_retval; \
  403. } else \
  404. ((void)0)
  405. /**
  406. * Try using `ERR_CONTINUE_MSG`.
  407. * Only use this macro if there is no sensible error message.
  408. *
  409. * Ensures `m_cond` is false.
  410. * If `m_cond` is true, the current loop continues.
  411. */
  412. #define ERR_CONTINUE(m_cond) \
  413. if (unlikely(m_cond)) { \
  414. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Continuing."); \
  415. continue; \
  416. } else \
  417. ((void)0)
  418. /**
  419. * Ensures `m_cond` is false.
  420. * If `m_cond` is true, prints `m_msg` and the current loop continues.
  421. */
  422. #define ERR_CONTINUE_MSG(m_cond, m_msg) \
  423. if (unlikely(m_cond)) { \
  424. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Continuing.", m_msg); \
  425. continue; \
  426. } else \
  427. ((void)0)
  428. /**
  429. * Same as `ERR_CONTINUE_MSG` but also notifies the editor.
  430. */
  431. #define ERR_CONTINUE_EDMSG(m_cond, m_msg) \
  432. if (unlikely(m_cond)) { \
  433. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Continuing.", m_msg, true); \
  434. continue; \
  435. } else \
  436. ((void)0)
  437. /**
  438. * Try using `ERR_BREAK_MSG`.
  439. * Only use this macro if there is no sensible error message.
  440. *
  441. * Ensures `m_cond` is false.
  442. * If `m_cond` is true, the current loop breaks.
  443. */
  444. #define ERR_BREAK(m_cond) \
  445. if (unlikely(m_cond)) { \
  446. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Breaking."); \
  447. break; \
  448. } else \
  449. ((void)0)
  450. /**
  451. * Ensures `m_cond` is false.
  452. * If `m_cond` is true, prints `m_msg` and the current loop breaks.
  453. */
  454. #define ERR_BREAK_MSG(m_cond, m_msg) \
  455. if (unlikely(m_cond)) { \
  456. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Breaking.", m_msg); \
  457. break; \
  458. } else \
  459. ((void)0)
  460. /**
  461. * Same as `ERR_BREAK_MSG` but also notifies the editor.
  462. */
  463. #define ERR_BREAK_EDMSG(m_cond, m_msg) \
  464. if (unlikely(m_cond)) { \
  465. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Breaking.", m_msg, true); \
  466. break; \
  467. } else \
  468. ((void)0)
  469. /**
  470. * Try using `ERR_FAIL_COND_MSG` or `ERR_FAIL_COND_V_MSG`.
  471. * Only use this macro if there is no sensible fallback i.e. the error is unrecoverable, and
  472. * there is no sensible error message.
  473. *
  474. * Ensures `m_cond` is false.
  475. * If `m_cond` is true, the application crashes.
  476. */
  477. #define CRASH_COND(m_cond) \
  478. if (unlikely(m_cond)) { \
  479. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "FATAL: Condition \"" _STR(m_cond) "\" is true."); \
  480. ::godot::_err_flush_stdout(); \
  481. GENERATE_TRAP(); \
  482. } else \
  483. ((void)0)
  484. /**
  485. * Try using `ERR_FAIL_COND_MSG` or `ERR_FAIL_COND_V_MSG`.
  486. * Only use this macro if there is no sensible fallback i.e. the error is unrecoverable.
  487. *
  488. * Ensures `m_cond` is false.
  489. * If `m_cond` is true, prints `m_msg` and the application crashes.
  490. */
  491. #define CRASH_COND_MSG(m_cond, m_msg) \
  492. if (unlikely(m_cond)) { \
  493. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "FATAL: Condition \"" _STR(m_cond) "\" is true.", m_msg); \
  494. ::godot::_err_flush_stdout(); \
  495. GENERATE_TRAP(); \
  496. } else \
  497. ((void)0)
  498. // Generic error macros.
  499. /**
  500. * Try using `ERR_FAIL_COND_MSG` or `ERR_FAIL_MSG`.
  501. * Only use this macro if more complex error detection or recovery is required, and
  502. * there is no sensible error message.
  503. *
  504. * The current function returns.
  505. */
  506. #define ERR_FAIL() \
  507. if (true) { \
  508. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Method/function failed."); \
  509. return; \
  510. } else \
  511. ((void)0)
  512. /**
  513. * Try using `ERR_FAIL_COND_MSG`.
  514. * Only use this macro if more complex error detection or recovery is required.
  515. *
  516. * Prints `m_msg`, and the current function returns.
  517. */
  518. #define ERR_FAIL_MSG(m_msg) \
  519. if (true) { \
  520. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Method/function failed.", m_msg); \
  521. return; \
  522. } else \
  523. ((void)0)
  524. /**
  525. * Same as `ERR_FAIL_MSG` but also notifies the editor.
  526. */
  527. #define ERR_FAIL_EDMSG(m_msg) \
  528. if (true) { \
  529. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Method/function failed.", m_msg, true); \
  530. return; \
  531. } else \
  532. ((void)0)
  533. /**
  534. * Try using `ERR_FAIL_COND_V_MSG` or `ERR_FAIL_V_MSG`.
  535. * Only use this macro if more complex error detection or recovery is required, and
  536. * there is no sensible error message.
  537. *
  538. * The current function returns `m_retval`.
  539. */
  540. #define ERR_FAIL_V(m_retval) \
  541. if (true) { \
  542. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Method/function failed. Returning: " _STR(m_retval)); \
  543. return m_retval; \
  544. } else \
  545. ((void)0)
  546. /**
  547. * Try using `ERR_FAIL_COND_V_MSG`.
  548. * Only use this macro if more complex error detection or recovery is required.
  549. *
  550. * Prints `m_msg`, and the current function returns `m_retval`.
  551. */
  552. #define ERR_FAIL_V_MSG(m_retval, m_msg) \
  553. if (true) { \
  554. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Method/function failed. Returning: " _STR(m_retval), m_msg); \
  555. return m_retval; \
  556. } else \
  557. ((void)0)
  558. /**
  559. * Same as `ERR_FAIL_V_MSG` but also notifies the editor.
  560. */
  561. #define ERR_FAIL_V_EDMSG(m_retval, m_msg) \
  562. if (true) { \
  563. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Method/function failed. Returning: " _STR(m_retval), m_msg, true); \
  564. return m_retval; \
  565. } else \
  566. ((void)0)
  567. /**
  568. * Try using `ERR_FAIL_COND_MSG`, `ERR_FAIL_COND_V_MSG`, `ERR_CONTINUE_MSG` or `ERR_BREAK_MSG`.
  569. * Only use this macro at the start of a function that has not been implemented yet, or
  570. * if more complex error detection or recovery is required.
  571. *
  572. * Prints `m_msg`.
  573. */
  574. #define ERR_PRINT(m_msg) \
  575. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_msg)
  576. /**
  577. * Same as `ERR_PRINT` but also notifies the editor.
  578. */
  579. #define ERR_PRINT_ED(m_msg) \
  580. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_msg, true)
  581. /**
  582. * Prints `m_msg` once during the application lifetime.
  583. */
  584. #define ERR_PRINT_ONCE(m_msg) \
  585. if (true) { \
  586. static bool first_print = true; \
  587. if (first_print) { \
  588. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_msg); \
  589. first_print = false; \
  590. } \
  591. } else \
  592. ((void)0)
  593. /**
  594. * Same as `ERR_PRINT_ONCE` but also notifies the editor.
  595. */
  596. #define ERR_PRINT_ONCE_ED(m_msg) \
  597. if (true) { \
  598. static bool first_print = true; \
  599. if (first_print) { \
  600. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_msg, true); \
  601. first_print = false; \
  602. } \
  603. } else \
  604. ((void)0)
  605. // Print warning message macros.
  606. /**
  607. * Prints `m_msg`.
  608. *
  609. * If warning about deprecated usage, use `WARN_DEPRECATED` or `WARN_DEPRECATED_MSG` instead.
  610. */
  611. #define WARN_PRINT(m_msg) \
  612. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_msg, false, true)
  613. /**
  614. * Same as `WARN_PRINT` but also notifies the editor.
  615. */
  616. #define WARN_PRINT_ED(m_msg) \
  617. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_msg, true, true)
  618. /**
  619. * Prints `m_msg` once during the application lifetime.
  620. *
  621. * If warning about deprecated usage, use `WARN_DEPRECATED` or `WARN_DEPRECATED_MSG` instead.
  622. */
  623. #define WARN_PRINT_ONCE(m_msg) \
  624. if (true) { \
  625. static bool first_print = true; \
  626. if (first_print) { \
  627. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_msg, false, true); \
  628. first_print = false; \
  629. } \
  630. } else \
  631. ((void)0)
  632. /**
  633. * Same as `WARN_PRINT_ONCE` but also notifies the editor.
  634. */
  635. #define WARN_PRINT_ONCE_ED(m_msg) \
  636. if (true) { \
  637. static bool first_print = true; \
  638. if (first_print) { \
  639. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_msg, true, true); \
  640. first_print = false; \
  641. } \
  642. } else \
  643. ((void)0)
  644. // Print deprecated warning message macros.
  645. /**
  646. * Warns that the current function is deprecated.
  647. */
  648. #define WARN_DEPRECATED \
  649. if (true) { \
  650. static std::atomic<bool> warning_shown; \
  651. if (!warning_shown.load()) { \
  652. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "This method has been deprecated and will be removed in the future.", false, true); \
  653. warning_shown.store(true); \
  654. } \
  655. } else \
  656. ((void)0)
  657. /**
  658. * Warns that the current function is deprecated and prints `m_msg`.
  659. */
  660. #define WARN_DEPRECATED_MSG(m_msg) \
  661. if (true) { \
  662. static std::atomic<bool> warning_shown; \
  663. if (!warning_shown.load()) { \
  664. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "This method has been deprecated and will be removed in the future.", m_msg, false, true); \
  665. warning_shown.store(true); \
  666. } \
  667. } else \
  668. ((void)0)
  669. /**
  670. * Do not use.
  671. * If the application should never reach this point use CRASH_NOW_MSG(m_msg) to explain why.
  672. *
  673. * The application crashes.
  674. */
  675. #define CRASH_NOW() \
  676. if (true) { \
  677. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "FATAL: Method/function failed."); \
  678. ::godot::_err_flush_stdout(); \
  679. GENERATE_TRAP(); \
  680. } else \
  681. ((void)0)
  682. /**
  683. * Only use if the application should never reach this point.
  684. *
  685. * Prints `m_msg`, and then the application crashes.
  686. */
  687. #define CRASH_NOW_MSG(m_msg) \
  688. if (true) { \
  689. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "FATAL: Method/function failed.", m_msg); \
  690. ::godot::_err_flush_stdout(); \
  691. GENERATE_TRAP(); \
  692. } else \
  693. ((void)0)
  694. /**
  695. * This should be a 'free' assert for program flow and should not be needed in any releases,
  696. * only used in dev builds.
  697. */
  698. #ifdef DEBUG_ENABLED
  699. #define DEV_ASSERT(m_cond) \
  700. if (unlikely(!(m_cond))) { \
  701. ::godot::_err_print_error(FUNCTION_STR, __FILE__, __LINE__, "FATAL: DEV_ASSERT failed \"" _STR(m_cond) "\" is false."); \
  702. ::godot::_err_flush_stdout(); \
  703. GENERATE_TRAP(); \
  704. } else \
  705. ((void)0)
  706. #else
  707. #define DEV_ASSERT(m_cond)
  708. #endif
  709. /**
  710. * Gives an error message when a method bind is invalid (likely the hash changed).
  711. * Avoids crashing the application in this case. It's not free, so it's debug only.
  712. */
  713. #ifdef DEBUG_ENABLED
  714. #define CHECK_METHOD_BIND_RET(m_mb, m_ret) \
  715. if (unlikely(!m_mb)) { \
  716. ERR_PRINT_ONCE("Method bind was not found. Likely the engine method changed to an incompatible version."); \
  717. return m_ret; \
  718. } else \
  719. ((void)0)
  720. #define CHECK_METHOD_BIND(m_mb) \
  721. if (unlikely(!m_mb)) { \
  722. ERR_PRINT_ONCE("Method bind was not found. Likely the engine method changed to an incompatible version."); \
  723. return; \
  724. } else \
  725. ((void)0)
  726. #else
  727. #define CHECK_METHOD_BIND_RET(m_mb, m_ret)
  728. #define CHECK_METHOD_BIND(m_mb)
  729. #endif