2010年1月19日 星期二

Assert - assertion

// An assertion statement specifies a condition that you expect to hold true at some particular point in your program.

Assert(m_hThread != NULL, _T("CuWinThread::ResumeThread m_hThread != NULL");

// uAssert.h


#if defined(_DEBUG)


    #define WIDEN2(x) L ## x
    #define WIDEN(x) WIDEN2(x)
    #define __WFILE__ WIDEN(__FILE__)

    extern bool CustomAssertFunction( bool, TCHAR*, int, TCHAR*, bool*);

    #if defined(_WINDOWS)
    #define ASSERTPRESS { _asm { int 3 } }
    #else
    #define ASSERTPRESS {}
    #endif // defined(_WINDOWS)


    #define Assert( exp, description) \
    { static bool ignoreAlways = false; \
        if( !ignoreAlways) { \
            if( CustomAssertFunction( (int)(exp), description, __LINE__, __FILE__, &ignoreAlways) ) \
                ASSERTPRESS \
        } \
    }

    #define Verify(exp, description)          Assert(exp, description)


#else

    #define Assert(exp, description)    ((void)0)
    #define Verify(exp, description)    ((void)(exp))

#endif  // defined(_DEBUG)




// uAssert.cpp



// Return:
// true - Stop On Assert Line
// false - continue
bool CustomAssertFunction( bool bExp, TCHAR* strDesc, int iLine, TCHAR* strFilename,  bool *ignoreAlways)
{

    if( !bExp )
    {
        TCHAR strTitle[255] = TEXT("");
        TCHAR strText[255] = TEXT("");
        wsprintf(strTitle, TEXT("%s - %d"), strFilename, iLine);
        wsprintf(strText, TEXT("%s \n Yes to Continue \n No to Break\n Cancle to Ignore Always"), strDesc);
        int nRelease = MessageBox(NULL, strText, strTitle, MB_YESNOCANCEL);
        switch( nRelease )
        {
        case 6:
            return false;
            break;
        case 7:
            return true;
            break;
        case 2:
            *ignoreAlways = true;
            break;
        }
    }
        
    return false;

}
這個有擴充過, 使用 _asm { int 3 } 能停在Assert 那一行.

1 則留言:

  1. 從某本遊戲設計書上弄出來, 本來是打算打上堆疊的功能, 讓assert能顯示出執行流程! 但是,還沒遇到就沒寫了~

    回覆刪除