P99

◆ P99_PROTECTED_BLOCK

#define P99_PROTECTED_BLOCK (   BEFORE,
  AFTER 
)

A meta-macro to protect a dependent block or statement by the statements BEFORE that is executed before the block and AFTER that is executed afterward.

Early exit from the block is possible with break or continue.

Warning
return, exit() or other functions that don't return to the caller inside the dependent block will result in not executing AFTER, so be careful.

An example of a potential use is

#define P99_INVARIANT(EXPR) P99_PROTECTED_BLOCK(assert((EXPR) && "failed on entry"), assert((EXPR) && "failed on leave"))

Such a construct may then be used like this

P99_INVARIANT(x > 5) {
// do something with x that changes it but ensures that
// it will be strictly less than 5 at the end
}

and this would trigger an assertion whenever the condition is not fulfilled when entering or leaving the block.

Definition at line 209 of file p99_block.h.