Improved C ++ Builder assert ()

Source: Internet
Author: User

Http://bdn.borland.com/article/0,1410,28432,00.html
Abstract: The assert () call allows you to test problems in the code, but it has some limitations. This article describes an improved assert call.
Improved C ++ Builder assert ()
As part of good programming, assertions in your code are a simple way to monitor what is happening. This article discusses how to increase the flexibility of assert () to make it more useful.
Assertion background
For many years, I have been using assert () to manage code.
Assert (int test)
This function obtains test and asserted that the test result is true-if it is true, nothing is done. Otherwise, a warning is generated, allowing your program to easily detect errors. The advantage of assert () is that it is also a macro. If you # define NDEBUG, these macros disappear from your code and leave nothing left.
While checking the code is useful, assert () itself has some problems:
By default, assertions that fail are thrown out of the program. If a warning occurs, the test ends.
Assert () checks every time it arrives, making it difficult to use in a loop.
It is difficult to check the code, because assert () usually calls abort (), it is impossible to ignore assertions and continue debugging.
ASSERTING ()
Of course, these problems must be solved in this article. By modifying assert (), we get a more robust and useful way to verify the code. The result is a macro package called ASSERTING (), which is separated from the code and different from assert (). It is included in the assert2.cpp/. dfm/. h file of the sample code:
ASSERTING (int test, char * errorMessage)
  
Like normal assert (), NDEBUG determines whether the code is actually created. It also adds an explanation to help you understand the error.
The header file macro shows how it should be processed. If NDEBUG is not defined, the following code is expanded during each ASSERTING () call.
# Define ASSERTING (test, msg)
{
If (! (Test ))
{
Static int calender = 1;
If (calender)
{
If (HandleAsserting (# test, # msg ,__ FILE __,__ LINE __, & calving ))
{_ Asm {int 3 }}
}
}
}
The macro uses a static variable to determine whether to continue the call. Our sub-function HandleAsserting () can disable this static variable and allow us to disable tests after this position (for example, after the first failure in a loop ). The sub-function returns True to execute the Assembly call 'int 3', And the debugger is interrupted after the assertion.
Three options
  
With these features, HandleAsserting () has three options:
  
Set the static flag to 0 to block future tests.
Returns true to interrupt the debugger.
Returns false and continues execution.
The following is a simple non-VCL implementation that ends with the assert2.cpp file (commented out ):
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 obtain three possible cases.
In view of the usefulness, We have C ++ Builder use our call. Of course, we can customize VCL Form based on our needs. The source code provided in this article contains a TRichEdit control, and the format of the asserted error message is very clear. The demo program allows you to dismiss assertions and experiment with different options. A warning about the VCL version is to avoid using it when constructing other forms.
End
Assertion is an easy-to-use way to ensure that the Code is as expected without adding redundant code. This article provides some additional features that you will find useful and extend the chance to use assertions.
(Translation 01 soft)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.