From 38be3528bb544bd2fa0101b096186014a1161cc5 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Sat, 4 Jun 2016 02:23:02 -0700 Subject: thunk: initial commit Something I slapped together in the interests of mechanizing the creation of thunks in C. In this context a thunk is simply a helper function that serves to normalize the calling convention of functions having potentially varying arity. This code has not been used in anything yet, I just put it together because I anticipate using it soon based on some projects I've been thinking about. example.c contains a trivial usage example. --- example.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 example.c (limited to 'example.c') diff --git a/example.c b/example.c new file mode 100644 index 0000000..5aa5e43 --- /dev/null +++ b/example.c @@ -0,0 +1,39 @@ +/* contrived usage example */ +#include +#include + +#include "thunk.h" + +THUNK_DEFINE(sum, int, a, int, b, int *, res) +{ + *res = a + b; + + return 0; +} + +THUNK_DEFINE(mult, int, a, int, b, int, c, int *, res) +{ + *res = a * b * c; + + return 0; +} + +int normalized_helper(thunk_t *thunk) +{ + return thunk_dispatch(thunk); +} + + +int main(void) +{ + int out; + + normalized_helper(THUNK(sum(1, 2, &out))); + printf("out: %i\n", out); + + normalized_helper(THUNK(mult(9, 7, 100, &out))); + printf("out: %i\n", out); + + return EXIT_SUCCESS; +} + -- cgit v1.2.3