error_macros.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*************************************************************************/
  2. /* error_macros.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef ERROR_MACROS_H
  30. #define ERROR_MACROS_H
  31. /**
  32. * Error macros. Unlike exceptions and asserts, these macros try to mantain consistency and stability
  33. * inside the code. It is recommended to always return processable data, so in case of an error, the
  34. * engine can stay working well.
  35. * In most cases, bugs and/or invalid data are not fatal and should never allow a perfectly running application
  36. * to fail or crash.
  37. */
  38. /**
  39. * Pointer to the error macro priting function. Reassign to any function to have errors printed
  40. */
  41. /** Function used by the error macros */
  42. // function, file, line, error, explanation
  43. enum ErrorHandlerType {
  44. ERR_HANDLER_ERROR,
  45. ERR_HANDLER_WARNING,
  46. ERR_HANDLER_SCRIPT
  47. };
  48. typedef void (*ErrorHandlerFunc)(void*,const char*,const char*,int p_line,const char *, const char *,ErrorHandlerType p_type);
  49. void _err_set_last_error(const char* p_err);
  50. void _err_clear_last_error();
  51. struct ErrorHandlerList {
  52. ErrorHandlerFunc errfunc;
  53. void *userdata;
  54. ErrorHandlerList*next;
  55. ErrorHandlerList() { errfunc=0; next=0; userdata=0; }
  56. };
  57. void add_error_handler(ErrorHandlerList *p_handler);
  58. void remove_error_handler(ErrorHandlerList *p_handler);
  59. void _err_print_error(const char* p_function,const char* p_file,int p_line,const char *p_error,ErrorHandlerType p_type=ERR_HANDLER_ERROR);
  60. #ifndef _STR
  61. #define _STR(m_x) #m_x
  62. #define _MKSTR(m_x) _STR(m_x)
  63. #endif
  64. #define _FNL __FILE__":"
  65. /** An index has failed if m_index<0 or m_index >=m_size, the function exists */
  66. extern bool _err_error_exists;
  67. #ifdef DEBUG_ENABLED
  68. /** Print a warning string.
  69. */
  70. #define ERR_EXPLAINC(m_reason) {_err_set_last_error(m_reason); _err_error_exists=true;}
  71. #define ERR_EXPLAIN(m_string) {_err_set_last_error(String(m_string).utf8().get_data()); _err_error_exists=true;}
  72. #else
  73. #define ERR_EXPLAIN( m_text )
  74. #define ERR_EXPLAINC( m_text )
  75. #endif
  76. #ifdef __GNUC__
  77. //#define FUNCTION_STR __PRETTY_FUNCTION__ - too annoying
  78. #define FUNCTION_STR __FUNCTION__
  79. #else
  80. #define FUNCTION_STR __FUNCTION__
  81. #endif
  82. #define ERR_FAIL_INDEX(m_index,m_size) \
  83. do {if ((m_index)<0 || (m_index)>=(m_size)) { \
  84. _err_print_error(FUNCTION_STR,__FILE__,__LINE__,"Index "_STR(m_index)" out of size ("_STR(m_size)")."); \
  85. return; \
  86. } else _err_error_exists=false; } while(0); \
  87. /** An index has failed if m_index<0 or m_index >=m_size, the function exists.
  88. * This function returns an error value, if returning Error, please select the most
  89. * appropriate error condition from error_macros.h
  90. */
  91. #define ERR_FAIL_INDEX_V(m_index,m_size,m_retval) \
  92. do {if ((m_index)<0 || (m_index)>=(m_size)) { \
  93. _err_print_error(FUNCTION_STR,__FILE__,__LINE__,"Index "_STR(m_index)" out of size ("_STR(m_size)")."); \
  94. return m_retval; \
  95. } else _err_error_exists=false;} while (0);
  96. /** An error condition happened (m_cond tested true) (WARNING this is the opposite as assert().
  97. * the function will exit.
  98. */
  99. #define ERR_FAIL_NULL(m_param) \
  100. { if ( !m_param ) { \
  101. _err_print_error(FUNCTION_STR,__FILE__,__LINE__,"Parameter ' "_STR(m_param)" ' is null."); \
  102. return; \
  103. }else _err_error_exists=false; } \
  104. #define ERR_FAIL_NULL_V(m_param,m_retval) \
  105. { if ( !m_param ) { \
  106. _err_print_error(FUNCTION_STR,__FILE__,__LINE__,"Parameter ' "_STR(m_param)" ' is null."); \
  107. return m_retval; \
  108. }else _err_error_exists=false; } \
  109. /** An error condition happened (m_cond tested true) (WARNING this is the opposite as assert().
  110. * the function will exit.
  111. */
  112. #define ERR_FAIL_COND(m_cond) \
  113. { if ( m_cond ) { \
  114. _err_print_error(FUNCTION_STR,__FILE__,__LINE__,"Condition ' "_STR(m_cond)" ' is true."); \
  115. return; \
  116. }else _err_error_exists=false; } \
  117. /** An error condition happened (m_cond tested true) (WARNING this is the opposite as assert().
  118. * the function will exit.
  119. * This function returns an error value, if returning Error, please select the most
  120. * appropriate error condition from error_macros.h
  121. */
  122. #define ERR_FAIL_COND_V(m_cond,m_retval) \
  123. { if ( m_cond ) { \
  124. _err_print_error(FUNCTION_STR,__FILE__,__LINE__,"Condition ' "_STR(m_cond)" ' is true. returned: "_STR(m_retval)); \
  125. return m_retval; \
  126. }else _err_error_exists=false; } \
  127. /** An error condition happened (m_cond tested true) (WARNING this is the opposite as assert().
  128. * the loop will skip to the next iteration.
  129. */
  130. #define ERR_CONTINUE(m_cond) \
  131. { if ( m_cond ) { \
  132. _err_print_error(FUNCTION_STR,__FILE__,__LINE__,"Condition ' "_STR(m_cond)" ' is true. Continuing..:"); \
  133. continue;\
  134. } else _err_error_exists=false;} \
  135. /** An error condition happened (m_cond tested true) (WARNING this is the opposite as assert().
  136. * the loop will break
  137. */
  138. #define ERR_BREAK(m_cond) \
  139. { if ( m_cond ) { \
  140. _err_print_error(FUNCTION_STR,__FILE__,__LINE__,"Condition ' "_STR(m_cond)" ' is true. Breaking..:"); \
  141. break;\
  142. } else _err_error_exists=false;} \
  143. /** Print an error string and return
  144. */
  145. #define ERR_FAIL() \
  146. { \
  147. _err_print_error(FUNCTION_STR,__FILE__,__LINE__,"Method/Function Failed."); \
  148. _err_error_exists=false;\
  149. return;\
  150. } \
  151. /** Print an error string and return with value
  152. */
  153. #define ERR_FAIL_V(m_value) \
  154. { \
  155. _err_print_error(FUNCTION_STR,__FILE__,__LINE__,"Method/Function Failed, returning: "__STR(m_value)); \
  156. _err_error_exists=false; \
  157. return m_value;\
  158. } \
  159. /** Print an error string.
  160. */
  161. #define ERR_PRINT(m_string) \
  162. { \
  163. _err_print_error(FUNCTION_STR,__FILE__,__LINE__,m_string); \
  164. _err_error_exists=false;\
  165. } \
  166. /** Print a warning string.
  167. */
  168. #define WARN_PRINT(m_string) \
  169. { \
  170. _err_print_error(FUNCTION_STR,__FILE__,__LINE__,m_string,ERR_HANDLER_WARNING); \
  171. _err_error_exists=false;\
  172. } \
  173. #endif