|
CX Framework
Cross-platform C utility framework
|
Macros | |
| #define | closureCreate(func, ...) _closureCreate(func, count_macro_args(__VA_ARGS__), (stvar[]) { __VA_ARGS__ }) |
| #define | closureCall(cls, ...) _closureCall(cls, count_macro_args(__VA_ARGS__), (stvar[]) { __VA_ARGS__ }) |
Typedefs | |
| typedef struct closure_ref * | closure |
| Opaque handle to a closure. | |
| typedef bool(* | closureFunc) (stvlist *cvars, stvlist *args) |
Functions | |
| closure | closureClone (closure cls) |
| void | closureDestroy (closure *cls) |
Function closures that capture environment variables for deferred execution.
A closure packages a function pointer with a list of captured variables (cvars) that act as the function's environment. When the closure is called, these captured variables are passed to the function along with any call-time arguments.
This is useful for:
Basic usage:
| #define closureCall | ( | cls, | |
| ... | |||
| ) | _closureCall(cls, count_macro_args(__VA_ARGS__), (stvar[]) { __VA_ARGS__ }) |
bool closureCall(closure cls, ...)
Call a closure with the given arguments.
Invokes the closure's function, passing both the captured variables (cvars) and the provided call-time arguments.
| cls | Closure to call |
| ... | Zero or more stvar arguments to pass to the closure function |
| #define closureCreate | ( | func, | |
| ... | |||
| ) | _closureCreate(func, count_macro_args(__VA_ARGS__), (stvar[]) { __VA_ARGS__ }) |
closure closureCreate(closureFunc func, ...)
Create a new closure with captured variables.
Captures the provided variables (cvars) which will be available to the function when the closure is called. The variables are copied, so the originals can be safely destroyed.
| func | Closure function to call |
| ... | Zero or more stvar arguments to capture as the closure's environment |
Closure function signature
Closure functions receive two argument lists:
Both are accessed using stvlist iteration functions (stvlNext*, stvlNextPtr, etc.)
| cvars | List of captured variables from closureCreate() |
| args | List of arguments from closureCall() |
| closure closureClone | ( | closure | cls | ) |
Create a copy of a closure
Creates a new closure with the same function and captured variables. The captured variables are deep-copied, so modifications to the original won't affect the clone.
| cls | Closure to clone |
| void closureDestroy | ( | closure * | cls | ) |
Destroy a closure and release its resources
Frees the closure and all captured variables. Sets the closure pointer to NULL.
| cls | Pointer to closure to destroy |