CX Framework
Cross-platform C utility framework
Loading...
Searching...
No Matches
closure.h
Go to the documentation of this file.
1
96
97#pragma once
98
99#include <cx/cx.h>
100#include <cx/stype/stvar.h>
101#include <cx/utils/macros/args.h>
102
103CX_C_BEGIN
104
105// Opaque closure reference type (internal)
106typedef struct closure_ref {
107 void* _is_closure;
108} closure_ref;
109
111typedef struct closure_ref* closure;
112
122typedef bool (*closureFunc)(stvlist* cvars, stvlist* args);
123
124_Ret_valid_ closure _closureCreate(_In_ closureFunc func, int n, stvar cvars[]);
125
135#define closureCreate(func, ...) \
136 _closureCreate(func, count_macro_args(__VA_ARGS__), (stvar[]) { __VA_ARGS__ })
137
138bool _closureCall(_In_ closure cls, int n, stvar args[]);
139
147#define closureCall(cls, ...) \
148 _closureCall(cls, count_macro_args(__VA_ARGS__), (stvar[]) { __VA_ARGS__ })
149
150_Ret_valid_ closure _closureCreateAs(_In_ void (*func)(void), _In_z_ const char* sig, int n,
151 stvar cvars[]);
152
168#define closureCreateAs(sigtype, func, ...) \
169 _closureCreateAs((void (*)(void))(1 ? (func) : (sigtype)0), \
170 #sigtype, \
171 count_macro_args(__VA_ARGS__), \
172 (stvar[]) { __VA_ARGS__ })
173
174// Internal: back closureCallAs(). They are usable directly by code that needs to call a typed
175// closure without the macro, but closureCallAs() is the supported way.
176void (*_closureFuncAs(_In_ closure cls, _In_z_ const char* sig))(void);
177_Ret_valid_ stvlist* _closureCvars(_In_ closure cls, _Out_ stvlist* storage);
178
195#define closureCallAs(sigtype, cls, ...) \
196 ((sigtype)_closureFuncAs((cls), #sigtype))(_closureCvars((cls), &(stvlist) { 0 }), __VA_ARGS__)
197
201typedef void (*closureDestroyFunc)(stvlist* cvars);
202
210void closureSetDestroy(_Inout_ closure cls, _In_opt_ closureDestroyFunc destroy);
211
220_Ret_maybenull_ closure closureClone(_In_opt_ closure cls);
221
228void closureDestroy(_Inout_opt_ closure* _Nullable cls);
229
231// end of closure_basic group
232
233CX_C_END
Macro argument counting utilities.
void closureSetDestroy(closure cls, closureDestroyFunc destroy)
void(* closureDestroyFunc)(stvlist *cvars)
Definition closure.h:201
void closureDestroy(closure *cls)
struct closure_ref * closure
Opaque handle to a closure.
Definition closure.h:111
bool(* closureFunc)(stvlist *cvars, stvlist *args)
Definition closure.h:122
closure closureClone(closure cls)
#define stvar(typen, val)
Definition stvar.h:162
Variant type containers and type-safe variadic argument support.