summaryrefslogtreecommitdiff
path: root/example.c
diff options
context:
space:
mode:
authorVito Caputo <vcaputo@gnugeneration.com>2016-06-04 02:23:02 -0700
committerVito Caputo <vcaputo@gnugeneration.com>2016-06-04 02:23:02 -0700
commit38be3528bb544bd2fa0101b096186014a1161cc5 (patch)
treeffd067f36aa3c50d2d476d75a7a8ab92fb758d76 /example.c
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.
Diffstat (limited to 'example.c')
-rw-r--r--example.c39
1 files changed, 39 insertions, 0 deletions
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 <stdio.h>
+#include <stdlib.h>
+
+#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;
+}
+
© All Rights Reserved