Improved C ++ builder assert ()

zhaozj2021-02-16  44

Http://bdn.borland.com/Article/0,1410,28432,00.html

Summary: Assert () call Allows you to test the problem in your code, but it is a bit limit. This article details an improved ASSERT call.

Improved C builder assert ()

As part of a good programming, assertion in your code is a simple way to monitor what happens. This article discusses to increase flexibility to Assert () make it more useful.

Some backgrounds of assertions

For many years, I have always used Assert () management code.

Assert (int Test)

This function gets TEST. As a result, the results are true - if true, nothing, otherwise generate a warning, allowing your program to easily detect errors. Assert () The advantage is that it is also a macro, if you #define ndebug, these macros disappear from your code, nothing left.

At the same time as checking the code, there are some problems with assert () itself:

Failure asserts The default is a jump out of the program, and a warning test will end. At each arrival, assert () is checked, which makes it difficult to use in the loop. It is difficult to check the code because Assert () usually calls Abort (), ignoring the assertion is impossible.

AskSERTING ()

Of course, these problems are being solved herein. By modifying assert (), we get a more useful way to verify the code. The result is a macro I call asserting (), that is, the code is detached, and it is divided into assert (). It contains the assert2.cpp / .dfm / .h file of the example code:

Asserting (int test, char * errorMessage)

Like normal assert (), ndebug determines whether this code is actually created. It also adds an explanation information to help understand errors.

The macro of the header file indicates how it should be processed. If NDEBUG is not defined, the following code will be expanded when each asserting () call.

#define asserting (TEST, MSG)

{

IF (! (test))

{

Static int Callit = 1;

IF (Callit)

{

IF (Handleasserting (# test, # msg, __ file __, __ line __, & callit))

{_ASM {INT 3}}

}

}

}

This macro uses a static variable to determine if the call continues. Our subunies HandleAsSrting () can close this static variable, allow us to disable the test after this location (for example, after the first failure in a loop). Subfremount Returns True Performs Compilation Call 'INT 3', interrupt to the debugger after asserting.

Three options

Due to these features, HandleAsSerting () calls have three options:

Set the static logo as 0, block the subsequent test, "dismiss" assertions. Return true, interrupt to the debugger. Return False and continue.

Below is a simple Non-VCL implementation, with the end of the assert2.cpp file (commented):

Int Handleasserting (Char * Teststr, Char * Msgstr, Char * FileStr, Int line, int * callflag)

{

// assert message & and return flag regarding aborting:

// Callflag Set if Repeating Forbidden

Static char s_text [199] = ""; // Don't Assign Dynamically In // Case of 'Out of Memory' Error

WSPRINTF (S_Text, "Failed:% SRN"

"Error: (% s) RN"

"File '% S', LINE% DRN"

"Abort execution, allow assert retry, or ignore in future?",

MSGSTR, Teststr, FileStr, Line

Switch (:: messagebox (null, s_text, "assertion error", MB_ABORTRETRYIGNORE))

{

Case idignore: // prevent calling again - Turn Off Flag

* Callflag = 0; // Never Call Again

Break;

Case Idabort: // Return Flag and Break

Return 1; // Abort / Break

}

Return 0;

}

This function calls MessageBox () to display assertion failure, use the Abort / Retry / Ignore button to get three possible conditions.

In view of this more useful, we let C Builder use our calls, of course, can customize VCL FORM according to our needs. The source code provided herein contains a TRICHEDIT control, and the format of the assertion error message is very clear. The demo allows you to dismiss assembes and experimentally different options. A warning about the VCL version is - avoiding it during other forms.

End

As a result, it is possible to ensure that the code is doing as you expect, does not increase the redundant code. This article provides some additional features that you will find more useful and extend the opportunity to use assertions.

(Translation 01Soft)

转载请注明原文地址:https://www.9cbs.com/read-23649.html

New Post(0)