This module contains a variant of the assert
-macro
from the standard library.
The idea is that there are conditions
which the programmer believes to be true,
but which can still be checked at runtime.
Examples are:
Furthermore, by defining a conditional compilation flag (CHECK_OFF), the macros for checking the conditions are defined as null statements. So as soon as the program is fully debugged, there is no runtime overhead, and the assertions are only useful comments. (Of course, the question remains whether any program is ever fully debugged, and at least for safety critical applications, one should probably retain the assertions.)
I have defined my own version of the assert
-macro
for the following reasons:
assert.h
was not always available.
The were also bad implementations
(using if
without an else
)
which can create very strange and hard-to-find errors.
assert(false)
because this generates a condition which can be evaluated
at compile-time.
My version contains a special variant CHECK_IMPOSSIBLE
for this.
Note that this module is one of the very few modules of SLP
which define no class.
All names of functions or macros
begin with check
or CHECK
,
so there should be no name clashes.
void check_init(void)
;
void CHECK(bool_t EX, str_t MSG)
;EX
evauates to false,
an error message is printed an the program is aborted.
The error message contains MSG
and the location of the call to CHECK
in the source file.
CHECK_VALID(EX)
;EX
if debugging is turned off.
Otherwise it calls the method valid
,
which many classes define,
before EX
is evaluated at usual.
So the macro is simply defined as
(valid(), (EX))
.
CHECK_PAR(PAR, MSG)
;(PAR)->valid()
,
i.e. the integrity check function of the given parameter.
If the integrity check returns BOOL_FALSE
,
an error message is printed an the program is aborted.
It is no problem if PAR
is null,
this is tested before the integrity check is called
(and the condition counts as satisfied).
If debugging is turned off,
no checking is done and the call is replaced by ((void)0)
.
void check_impossible(str_t MSG)
;CHECK
,
the bug was detected by the usual program logic
(such as an impossible "default"-case in a switch).
An error message is printed
(containing MSG
and the source file position)
and the program is aborted.
CHECK_CODE(CODE)
;CHECK_OFF
is defined,
this macro is expanded as the empty string,
otherwise as CODE
.