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

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)
 

Detailed Description

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:

bool myCallback(stvlist *cvars, stvlist *args) {
int captured = stvlNextInt(cvars);
string arg = stvlNextPtr(string, args);
// use captured value and argument...
return true;
}
closure cls = closureCreate(myCallback, stvar(int32, 42));
closureCall(cls, stvar(string, myString));
void closureDestroy(closure *cls)
#define closureCall(cls,...)
Definition closure.h:86
#define closureCreate(func,...)
Definition closure.h:72
#define stvar(typen, val)
Definition stvar.h:153
#define stvlNextPtr(list)
Definition stvar.h:537

Macro Definition Documentation

◆ closureCall

#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.

Parameters
clsClosure to call
...Zero or more stvar arguments to pass to the closure function
Returns
Return value from the closure function

Definition at line 86 of file closure.h.

◆ closureCreate

#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.

Parameters
funcClosure function to call
...Zero or more stvar arguments to capture as the closure's environment
Returns
New closure object (must be freed with closureDestroy())

Definition at line 72 of file closure.h.

Typedef Documentation

◆ closureFunc

typedef bool(* closureFunc) (stvlist *cvars, stvlist *args)

Closure function signature

Closure functions receive two argument lists:

  • cvars: Captured variables provided when the closure was created
  • args: Arguments provided when the closure is called

Both are accessed using stvlist iteration functions (stvlNext*, stvlNextPtr, etc.)

Parameters
cvarsList of captured variables from closureCreate()
argsList of arguments from closureCall()
Returns
true on success, false on failure

Definition at line 58 of file closure.h.

Function Documentation

◆ closureClone()

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.

Parameters
clsClosure to clone
Returns
New independent closure (must be freed with closureDestroy())

◆ closureDestroy()

void closureDestroy ( closure *  cls)

Destroy a closure and release its resources

Frees the closure and all captured variables. Sets the closure pointer to NULL.

Parameters
clsPointer to closure to destroy