dKingyo Utility Toolkitでのassertの書き方

http://www.radiumsoftware.com/0510.html#051007
にてassertについて述べられている。記事を読んでいる時間は無い*1のでdKingyo Utility Toolkitにて採用されているのassert()の書き方のルールに関する着目点を書く。


  • デバッグ用のチェックにassertを使用する事。

    デバッグ時にDebugBreak();出来て便利だからである。
  • Releaseビルド時にはassertしない事。

    return false;とかthrow 使おうよ。そしてその時の処理はプログラマが書こうよ。エラーハンドリングは大切だよ〜、使いやすいアプリケーションを作るには!って事。
  • それでもassertする場合。

    もう、これが間違っていたらプログラムは破綻してしまうという時にだけ使う。しかもその条件がきたら即終了処理exit(1);をするようにする。
以上のようなポリシーから以下のプログラムをgoogle:dkutil_cのdkcOSIndependent.hに定義している。
なおdkutil_c 0.199から以下のソースを抜粋した。

#if defined(DEBUG) || defined(DKUTIL_DEBUG)
# define dkcmNOT_ASSERT(ex) ( (ex) && dkcErrorMessage(#ex,__FILE__,__LINE__,NULL) )
# define dkcmASSERT(ex) ( (ex) || dkcErrorMessage(#ex,__FILE__,__LINE__,NULL) )
# define dkcmNOT_ASSERT_MESSAGE(ex,mes) ( (ex) && dkcErrorMessage(#ex,__FILE__,__LINE__,mes) )
# define dkcmASSERT_MESSAGE(ex,mes) ( (ex) || dkcErrorMessage(#ex,__FILE__,__LINE__,mes) )
#else
# define dkcmNOT_ASSERT_MESSAGE(ex,mes) ((void)0)
# define dkcmASSERT_MESSAGE(ex,mes) ((void)0)
# define dkcmNOT_ASSERT(ex) ((void)0)
# define dkcmASSERT(ex) ((void)0)
#endif

# define dkcmFORCE_NOT_ASSERT(ex) ( (ex) && dkcErrorMessage(#ex,__FILE__,__LINE__,NULL) )
# define dkcmFORCE_ASSERT(ex) ( (ex) || dkcErrorMessage(#ex,__FILE__,__LINE__,NULL) )
# define dkcmFORCE_NOT_ASSERT_MESSAGE(ex,mes) ( (ex) && dkcErrorMessage(#ex,__FILE__,__LINE__,mes) )
# define dkcmFORCE_ASSERT_MESSAGE(ex,mes) ( (ex) || dkcErrorMessage(#ex,__FILE__,__LINE__,mes) )

*1:英語は読めない