Contents Up Previous Next 原文へのリンク

デバッグ用マクロと関数 Debugging macros and functions

エラーチェックや防衛的プログラミングに役立つマクロや関数. wxWidgetsは,アサートの様なマクロを三種類に定義している: wxASSERTとwxFAILマクロは,__WXDEBUG__が定義されていた場合(つまり,デバッ グビルドの場合)のみ機能し,リリースビルドにおいては完全に消えてしまう. 一方,wxCHECKマクロは,リリースビルドにおいても機能するが,チェッ クが失敗してもユーザに見えるような結果を生成することはない. 最後に,コンパイル時アサートは,チェック条件が偽だった場合に,実行時で はなくコンパイルエラーメッセージの結果において発生するものである.
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


::wxOnAssert

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

デバッグ用マクロが失敗すると(つまり,アサート条件が偽だった場合),この 関数が呼ばれる.デバッグモードにおいてのみ定義され,リリースビルドに おけるwxCHECKの失敗は,なにも起こさな い.

デバッグビルドにおけるデフォルトの動作は,プログラムを終了するか続ける か,あるいは全エラーを無視するかを尋ねるダイアログをユーザ表示する事で あるが,これをオーバーライドするには,グローバルアプリケーションオブジェ クトが存在する場合にこの関数から呼ばれるwxApp::OnAssertをオーバーライドすれ ばよい.

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)

アサートマクロ. デバッグモードでは,条件が偽だった場合にエラーメッセージが生成されるが, リリースビルドでは何も起こらない.

リリースモードでは全く動作しないので,wxASSERT()の条件は副次的な作用を 持っていてはならない事に注意する.

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)

与えられた型typeのサイズがsizeビットよりも小さい場合, このマクロはcompile time assertion failureとなる.
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:

    // intは2^32までの値を保持することができるはず
    // we rely on the int being able to hold values up to 2^32
    wxASSERT_MIN_BITSIZE(int, 32);

    // wchar_tにUTF-8を用いているプラットフォームでは動作しない
    // 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)

wxCOMPILE_TIME_ASSERTを用いると,指定のconditionが偽の 場合にコンパイルエラーが起こる.コンパイルエラーメッセージは, msg識別子 - その他の場合とは異なり,文字列ではなくC++の有効な識 別子でなければならない事に注意 - を含む.

このマクロは主に,プリプロセッサではテストできないsizeofオペ レータにを用いた表現のテストに役立つが,コンパイル時にテストすることが できない場合もある.

このマクロは,内部で__LINE__を用いた固有の名前を持つ構造体を 宣言するが,これが二つの異なるソースファイルの同じ行で用いられると動作 しない.この場合,どちらかの行を変更するか,あるいはwxCOMPILE_TIME_ASSERT2マクロ を用いればよい.

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.

See also

wxASSERT_MSG,
wxASSERT_MIN_BITSIZE


wxCOMPILE_TIME_ASSERT2

wxCOMPILE_TIME_ASSERT(condition, msg, name)

このマクロは,上述のコンパイル エラーを回避するために,このマクロ内部で宣言される構造体の一意な名前を プログラマが決定できる,という点を除いてwxCOMPILE_TIME_ASSERTと等価 である.
原文のwxCOMPILE_TIME_ASSERT2は, wxCOMPILE_TIME_ASSERTの誤植だと思われる
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)

(デバッグモードにおいて)このコードの場所では,常に指定されたメッセージ を含むアサートエラーが生成される.

このマクロは,コード上の"通過できない"場所をマーキングするのに役 立つ.例えば,あらゆる場合がその上部に書かれたコードで処理されるswitch 文の,最後の"default:"部分などで用いられるだろう.

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)

条件が真であるかどうかをチェックし,そうでない場合(デバッグモードにお けるFAIL),与えられた戻り値を返す.このチェックは,リリースモード においても行われる.
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)

条件が真であるかどうかをチェックし,そうでない場合(デバッグモードにお けるFAIL),与えられた戻り値を返す.このチェックは,リリースモード においても行われる.

このマクロは,非void関数でのみ用いられるだろう.wxCHECK_RETを参照.

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 functions, see also wxCHECK_RET.


wxCHECK_RET

wxCHECK_RET(condition, msg)

条件が真であるかどうかをチェックし,そうでない場合(デバッグモードにお ける,与えられたエラーメッセージを伴うFAIL)返す. このマクロは,wxCHECK_MSGの代わりに void関数で用いられる.
Checks that the condition is true, and returns if not (FAILs with given error message in debug mode). This check is done even in release mode.

This macro should be used in void functions instead of wxCHECK_MSG.


wxCHECK2

wxCHECK2(condition, operation)

条件が真であるかどうかをチェックし,そうでない場合, wxFAILおよびoperationの実行が行 われる.これは,wxCHECKの一般形であり, conditionが偽の場合に,単に関数から返るだけ以上の事を行わなけれ ばならない場合に用いられるだろう.

このチェックは,リリースモードにおいても行われる.

Checks that the condition is true and wxFAIL and execute operation if it is not. This is a generalisation of wxCHECK and may be used when something else than just returning from the function must be done when the condition is false.

This check is done even in release mode.


wxCHECK2_MSG

wxCHECK2(condition, operation, msg)

これはwxCHECK2と等価であるが, conditionが偽の場合は,wxFAIL()の代わりに指定されたmsgを 伴ったwxFAIL_MSGが呼び出される.
This is the same as wxCHECK2, but wxFAIL_MSG with the specified msg is called instead of wxFAIL() if the condition is false.


::wxTrap

void wxTrap()

デバッグモードにおいて(__WXDEBUG__が定義されている場合), この関数は,それがプロセスにアタッチされた場合にデバッガに制御が渡され るデバッガ例外を生成する.そうでない場合は,プログラムは単に異常終了す る.

リリースモードでは,この関数はなにも行わない.

In debug mode (when __WXDEBUG__ is defined) this function generates a debugger exception meaning that the control is passed to the debugger if one is attached to the process. Otherwise the program just terminates abnormally.

In release mode this function does nothing.

Include files

<wx/debug.h>