Contents Up Previous Next

Debugging macros and functions

Useful macros and functions for error checking and defensive programming. wxWidgets defines three families of the assert-like macros: the wxASSERT and wxFAIL macros only do anything if __WXDEBUG__ is defined (in other words, in the debug build) but disappear completely in the release build. On the other hand, the wxCHECK macros stay event in release builds but a check failure doesn't generate any user-visible effects then. Finally, the compile time assertions don't happen during the run-time but result in the compilation error messages if the condition they check fail.

Include files

<wx/debug.h>

::wxOnAssert
wxASSERT
wxASSERT_MIN_BITSIZE
wxASSERT_MSG
wxCOMPILE_TIME_ASSERT
wxCOMPILE_TIME_ASSERT2
wxFAIL
wxFAIL_MSG
wxCHECK
wxCHECK_MSG
wxCHECK_RET
wxCHECK2
wxCHECK2_MSG
::wxTrap
::wxIsDebuggerRunning


::wxOnAssert

void wxOnAssert(const char *fileName, int lineNumber, const char *cond, const char *msg = NULL)

This function is called whenever one of debugging macros fails (i.e. condition is false in an assertion). It is only defined in the debug mode, in release builds the wxCHECK failures don't result in anything.

To override the default behaviour in the debug builds which is to show the user a dialog asking whether he wants to abort the program, continue or continue ignoring any subsequent assert failures, you may override wxApp::OnAssert which is called by this function if the global application object exists.


wxASSERT

wxASSERT(condition)

Assert macro. An error message will be generated if the condition is false in debug mode, but nothing will be done in the release build.

Please note that the condition in wxASSERT() should have no side effects because it will not be executed in release mode at all.

See also

wxASSERT_MSG,
wxCOMPILE_TIME_ASSERT


wxASSERT_MIN_BITSIZE

wxASSERT_MIN_BITSIZE(type, size)

This macro results in a compile time assertion failure if the size of the given type type is less than size bits.

You may use it like this, for example:

    // we rely on the int being able to hold values up to 2^32
    wxASSERT_MIN_BITSIZE(int, 32);

    // can't work with the platforms using UTF-8 for wchar_t
    wxASSERT_MIN_BITSIZE(wchar_t, 16);

wxASSERT_MSG

wxASSERT_MSG(condition, msg)

Assert macro with message. An error message will be generated if the condition is false.

See also

wxASSERT,
wxCOMPILE_TIME_ASSERT


wxCOMPILE_TIME_ASSERT

wxCOMPILE_TIME_ASSERT(condition, msg)

Using wxCOMPILE_TIME_ASSERT results in a compilation error if the specified condition is false. The compiler error message should include the msg identifier - please note that it must be a valid C++ identifier and not a string unlike in the other cases.

This macro is mostly useful for testing the expressions involving the sizeof operator as they can't be tested by the preprocessor but it is sometimes desirable to test them at the compile time.

Note that this macro internally declares a struct whose name it tries to make unique by using the __LINE__ in it but it may still not work if you use it on the same line in two different source files. In this case you may either change the line in which either of them appears on or use the wxCOMPILE_TIME_ASSERT2 macro.

Also note that Microsoft Visual C++ has a bug which results in compiler errors if you use this macro with 'Program Database For Edit And Continue' (/ZI) option, so you shouldn't use it ('Program Database' (/Zi) is ok though) for the code making use of this macro.

See also

wxASSERT_MSG,
wxASSERT_MIN_BITSIZE


wxCOMPILE_TIME_ASSERT2

wxCOMPILE_TIME_ASSERT(condition, msg, name)

This macro is identical to wxCOMPILE_TIME_ASSERT2 except that it allows you to specify a unique name for the struct internally defined by this macro to avoid getting the compilation errors described above.


wxFAIL

wxFAIL()

Will always generate an assert error if this code is reached (in debug mode).

See also: wxFAIL_MSG


wxFAIL_MSG

wxFAIL_MSG(msg)

Will always generate an assert error with specified message if this code is reached (in debug mode).

This macro is useful for marking unreachable" code areas, for example it may be used in the "default:" branch of a switch statement if all possible cases are processed above.

See also

wxFAIL


wxCHECK

wxCHECK(condition, retValue)

Checks that the condition is true, returns with the given return value if not (FAILs in debug mode). This check is done even in release mode.


wxCHECK_MSG

wxCHECK_MSG(condition, retValue, msg)

Checks that the condition is true, returns with the given return value if not (FAILs in debug mode). This check is done even in release mode.

This macro may be only used in non void functionsse wxDataObjectComposite This is a simple but powerful solution which allows you to support any number of formats (either standard or custom if you combine it with the previous solution). 4. Use wxDataObject directly This is the solution for maximal flexibility and efficiency, but it is also the most difficult to implement.

Please note that the easiest way to use drag and drop and the clipboard with multiple formats is by using wxDataObjectComposite, but it is not the most efficient one as each wxDataObjectSimple would contain the whole data in its respective formats. Now imagine that you want to paste 200 pages of text in your proprietary format, as well as Word, RTF, HTML, Unicode and plain text to the clipboard and even today's computers are in trouble. For this case, you will have to derive from wxDataObject directly and make it enumerate its formats and provide the data in the requested format on demand.

Note that neither the GTK+ data transfer mechanisms for clipboard and drag and drop, nor OLE data transfer, copy any data until another application actually requests the data. This is in contrast to the 'feel' offered to the user of a program who would normally think that the data resides in the clipboard after having pressed 'Copy' - in reality it is only declared to be available.

There are several predefined data object classes derived from wxDataObjectSimple: wxFileDataObject, wxTextDataObject and wxBitmapDataObject which can be used without change.

You may also derive your own data object classes from wxCustomDataObject for user-defined types. The format of user-defined data is given as a mime-type string literal, such as "application/word" or "image/png". These strings are