debug.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/debug.h
  3. // Purpose: interface of global functions
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /** @addtogroup group_funcmacro_debug */
  8. //@{
  9. /**
  10. Exits the program immediately.
  11. This is a simple wrapper for the standard abort() function which is not
  12. available under all platforms (currently only Windows CE doesn't provide
  13. it).
  14. @since 2.9.4
  15. */
  16. void wxAbort();
  17. /**
  18. @def wxDEBUG_LEVEL
  19. Preprocessor symbol defining the level of debug support available.
  20. This symbol is defined to 1 by default meaning that asserts are compiled in
  21. (although they may be disabled by a call to wxDisableAsserts()). You may
  22. predefine it as 0 prior to including any wxWidgets headers to omit the
  23. calls to wxASSERT() and related macros entirely in your own code and you
  24. may also predefine it as 0 when building wxWidgets to also avoid including
  25. any asserts in wxWidgets itself.
  26. Alternatively, you may predefine it as 2 to include wxASSERT_LEVEL_2() and
  27. similar macros which are used for asserts which have non-trivial run-time
  28. costs and so are disabled by default.
  29. @since 2.9.1
  30. @header{wx/debug.h}
  31. */
  32. #define wxDEBUG_LEVEL
  33. /**
  34. @def __WXDEBUG__
  35. Compatibility macro indicating presence of debug support.
  36. This symbol is defined if wxDEBUG_LEVEL is greater than 0 and undefined
  37. otherwise.
  38. @header{wx/debug.h}
  39. */
  40. #define __WXDEBUG__
  41. /**
  42. Type for the function called in case of assert failure.
  43. @see wxSetAssertHandler()
  44. */
  45. typedef void (*wxAssertHandler_t)(const wxString& file,
  46. int line,
  47. const wxString& func,
  48. const wxString& cond,
  49. const wxString& msg);
  50. /**
  51. Assert macro. An error message will be generated if the condition is @false in
  52. debug mode, but nothing will be done in the release build.
  53. Please note that the condition in wxASSERT() should have no side effects
  54. because it will not be executed in release mode at all.
  55. This macro should be used to catch (in debug builds) logical errors done
  56. by the programmer.
  57. @see wxASSERT_MSG(), wxCOMPILE_TIME_ASSERT()
  58. @header{wx/debug.h}
  59. */
  60. #define wxASSERT( condition )
  61. /**
  62. Assert macro for expensive run-time checks.
  63. This macro does nothing unless wxDEBUG_LEVEL is 2 or more and is meant to
  64. be used for the assertions with noticeable performance impact and which,
  65. hence, should be disabled during run-time.
  66. If wxDEBUG_LEVEL is 2 or more, it becomes the same as wxASSERT().
  67. @header{wx/debug.h}
  68. */
  69. #define wxASSERT_LEVEL_2( condition )
  70. /**
  71. Assert macro with a custom message for expensive run-time checks.
  72. If wxDEBUG_LEVEL is 2 or more, this is the same as wxASSERT_MSG(),
  73. otherwise it doesn't do anything at all.
  74. @see wxASSERT_LEVEL_2()
  75. @header{wx/debug.h}
  76. */
  77. #define wxASSERT_LEVEL_2_MSG( condition, msg)
  78. /**
  79. This macro results in a @ref wxCOMPILE_TIME_ASSERT "compile time assertion failure"
  80. if the size of the given @c type is less than @c size bits.
  81. This macro should be used to catch (in debug builds) logical errors done
  82. by the programmer.
  83. You may use it like this, for example:
  84. @code
  85. // we rely on the int being able to hold values up to 2^32
  86. wxASSERT_MIN_BITSIZE(int, 32);
  87. // can't work with the platforms using UTF-8 for wchar_t
  88. wxASSERT_MIN_BITSIZE(wchar_t, 16);
  89. @endcode
  90. @header{wx/debug.h}
  91. */
  92. #define wxASSERT_MIN_BITSIZE( type, size )
  93. /**
  94. Assert macro with message.
  95. An error message will be generated if the condition is @false.
  96. This macro should be used to catch (in debug builds) logical errors done
  97. by the programmer.
  98. @see wxASSERT(), wxCOMPILE_TIME_ASSERT()
  99. @header{wx/debug.h}
  100. */
  101. #define wxASSERT_MSG( condition, message )
  102. /**
  103. Checks that the condition is @true, returns with the given return value if
  104. not (stops execution in debug mode). This check is done even in release mode.
  105. This macro should be used to catch (both in debug and release builds) logical
  106. errors done by the programmer.
  107. @header{wx/debug.h}
  108. */
  109. #define wxCHECK( condition, retValue )
  110. /**
  111. Checks that the condition is @true, returns with the given return value if
  112. not (stops execution in debug mode). This check is done even in release mode.
  113. This macro may be only used in non-void functions, see also wxCHECK_RET().
  114. This macro should be used to catch (both in debug and release builds) logical
  115. errors done by the programmer.
  116. @header{wx/debug.h}
  117. */
  118. #define wxCHECK_MSG( condition, retValue, message )
  119. /**
  120. Checks that the condition is @true, and returns if not (stops execution
  121. with the given error message in debug mode). This check is done even in
  122. release mode.
  123. This macro should be used in void functions instead of wxCHECK_MSG().
  124. This macro should be used to catch (both in debug and release builds) logical
  125. errors done by the programmer.
  126. @header{wx/debug.h}
  127. */
  128. #define wxCHECK_RET( condition, message )
  129. /**
  130. Checks that the condition is @true, and if not, it will wxFAIL() and
  131. execute the given @c operation if it is not. This is a generalisation of
  132. wxCHECK() and may be used when something else than just returning from the
  133. function must be done when the @c condition is @false. This check is done
  134. even in release mode.
  135. This macro should be used to catch (both in debug and release builds) logical
  136. errors done by the programmer.
  137. @header{wx/debug.h}
  138. */
  139. #define wxCHECK2(condition, operation)
  140. /**
  141. This is the same as wxCHECK2(), but wxFAIL_MSG() with the specified
  142. @c message is called instead of wxFAIL() if the @c condition is @false.
  143. This macro should be used to catch (both in debug and release builds) logical
  144. errors done by the programmer.
  145. @header{wx/debug.h}
  146. */
  147. #define wxCHECK2_MSG( condition, operation, message )
  148. /**
  149. Using wxCOMPILE_TIME_ASSERT() results in a compilation error if the
  150. specified @c condition is @false. The compiler error message should include
  151. the @c message identifier - please note that it must be a valid C++
  152. identifier and not a string unlike in the other cases.
  153. This macro is mostly useful for testing the expressions involving the
  154. @c sizeof operator as they can't be tested by the preprocessor but it is
  155. sometimes desirable to test them at the compile time.
  156. Note that this macro internally declares a struct whose name it tries to
  157. make unique by using the @c __LINE__ in it but it may still not work if you
  158. use it on the same line in two different source files. In this case you may
  159. either change the line in which either of them appears on or use the
  160. wxCOMPILE_TIME_ASSERT2() macro.
  161. Also note that Microsoft Visual C++ has a bug which results in compiler
  162. errors if you use this macro with 'Program Database For Edit And Continue'
  163. (@c /ZI) option, so you shouldn't use it ('Program Database' (@c /Zi) is ok
  164. though) for the code making use of this macro.
  165. This macro should be used to catch misconfigurations at compile-time.
  166. @see wxASSERT_MSG(), wxASSERT_MIN_BITSIZE()
  167. @header{wx/debug.h}
  168. */
  169. #define wxCOMPILE_TIME_ASSERT( condition, message )
  170. /**
  171. This macro is identical to wxCOMPILE_TIME_ASSERT() except that it allows
  172. you to specify a unique @c name for the struct internally defined by this
  173. macro to avoid getting the compilation errors described for
  174. wxCOMPILE_TIME_ASSERT().
  175. This macro should be used to catch misconfigurations at compile-time.
  176. @header{wx/debug.h}
  177. */
  178. #define wxCOMPILE_TIME_ASSERT2(condition, message, name)
  179. /**
  180. Disable the condition checks in the assertions.
  181. This is the same as calling wxSetAssertHandler() with @NULL handler.
  182. @since 2.9.0
  183. @header{wx/debug.h}
  184. */
  185. void wxDisableAsserts();
  186. /**
  187. @def wxDISABLE_ASSERTS_IN_RELEASE_BUILD
  188. Use this macro to disable asserts in release build when not using
  189. wxIMPLEMENT_APP().
  190. By default, assert message boxes are suppressed in release build by
  191. wxIMPLEMENT_APP() which uses this macro. If you don't use wxIMPLEMENT_APP()
  192. because your application initializes wxWidgets directly (e.g. calls
  193. wxEntry() or wxEntryStart() itself) but still want to suppress assert
  194. notifications in release build you need to use this macro directly.
  195. @see wxDISABLE_DEBUG_SUPPORT()
  196. @since 2.9.1
  197. @header{wx/debug.h}
  198. */
  199. #define wxDISABLE_ASSERTS_IN_RELEASE_BUILD() wxDisableAsserts()
  200. /**
  201. Will always generate an assert error if this code is reached (in debug mode).
  202. Note that you don't have to (and cannot) use brackets when invoking this
  203. macro:
  204. @code
  205. if (...some condition...) {
  206. wxFAIL;
  207. }
  208. @endcode
  209. This macro should be used to catch (in debug builds) logical errors done
  210. by the programmer.
  211. @see wxFAIL_MSG()
  212. @header{wx/debug.h}
  213. */
  214. #define wxFAIL
  215. /**
  216. Will always generate an assert error with specified message if this code is
  217. reached (in debug mode).
  218. This macro is useful for marking "unreachable" code areas, for example it
  219. may be used in the "default:" branch of a switch statement if all possible
  220. cases are processed above.
  221. This macro should be used to catch (in debug builds) logical errors done
  222. by the programmer.
  223. @see wxFAIL()
  224. @header{wx/debug.h}
  225. */
  226. #define wxFAIL_MSG( message )
  227. /**
  228. Returns @true if the program is running under debugger, @false otherwise.
  229. Please note that this function is currently only implemented for Win32 and
  230. always returns @false elsewhere.
  231. @header{wx/debug.h}
  232. */
  233. bool wxIsDebuggerRunning();
  234. /**
  235. Sets the function to be called in case of assertion failure.
  236. The default assert handler forwards to wxApp::OnAssertFailure() whose
  237. default behaviour is, in turn, to show the standard assertion failure
  238. dialog if a wxApp object exists or shows the same dialog itself directly
  239. otherwise.
  240. While usually it is enough -- and more convenient -- to just override
  241. OnAssertFailure(), to handle all assertion failures, including those
  242. occurring even before wxApp object creation of after its destruction you
  243. need to provide your assertion handler function.
  244. This function also provides a simple way to disable all asserts: simply
  245. pass @NULL pointer to it. Doing this will result in not even evaluating
  246. assert conditions at all, avoiding almost all run-time cost of asserts.
  247. Notice that this function is not MT-safe, so you should call it before
  248. starting any other threads.
  249. The return value of this function is the previous assertion handler. It can
  250. be called after any pre-processing by your handler and can also be restored
  251. later if you uninstall your handler.
  252. @param handler
  253. The function to call in case of assertion failure or @NULL.
  254. @return
  255. The previous assert handler which is not @NULL by default but could be
  256. @NULL if it had been previously set to this value using this function.
  257. @since 2.9.0
  258. @header{wx/debug.h}
  259. */
  260. wxAssertHandler_t wxSetAssertHandler(wxAssertHandler_t handler);
  261. /**
  262. Reset the assert handler to default function which shows a message box when
  263. an assert happens.
  264. This can be useful for the applications compiled in release build (with @c
  265. NDEBUG defined) for which the asserts are by default disabled: if you wish
  266. to enable them even in this case you need to call this function.
  267. @since 2.9.1
  268. @header{wx/debug.h}
  269. */
  270. void wxSetDefaultAssertHandler();
  271. /**
  272. Generate a debugger exception meaning that the control is passed to the
  273. debugger if one is attached to the process.
  274. Otherwise the program just terminates abnormally.
  275. If @c wxDEBUG_LEVEL is 0 (which is not the default) this function does
  276. nothing.
  277. @header{wx/debug.h}
  278. */
  279. void wxTrap();
  280. //@}