CX Framework
Cross-platform C utility framework
Loading...
Searching...
No Matches
Metaprogramming

Modules

 Block Wrapping
 
 Protected Blocks
 
 Exception Handling
 

Detailed Description

Advanced C metaprogramming macros for complex control flow patterns.

The meta module provides compile-time and runtime control flow facilities built on creative uses of for loops and other C constructs. The primary tool is blkWrap(), which enables automatic cleanup and resource management patterns similar to RAII in C++.

Primary Use Case - blkWrap():

The blkWrap() macro executes code before and after a block, guaranteeing cleanup even if the block exits early with break or continue. This is lightweight and suitable for most situations:

blkWrap(mutex_lock(&mtx), mutex_unlock(&mtx)) {
// critical section - mutex is always unlocked on exit
if (condition) break; // early exit still runs cleanup
}
#define blkWrap(before,...)
Definition block.h:225

Advanced Features:

For more complex scenarios involving nested unwinding or exception-like behavior, the module also provides protected blocks (pblock) and try/catch constructs (ptTry/ptCatch/ptFinally). However, these are heavyweight operations with significant performance implications:

Use these advanced features sparingly, only when simpler alternatives are insufficient.

Compile-Time Feature Inhibition:

The module includes a facility for compile-time checks that prevent use of specific language features (like return) within certain blocks, helping catch errors early.

See Block Wrapping for block wrapping and basic control flow, Protected Blocks for protected blocks, and Exception Handling for exception handling.