Last active
October 4, 2019 11:27
-
-
Save rpoisel/75ea8f19e8f62e41017fb704438a85c3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stddef.h> | |
#include <stdio.h> | |
typedef void MyFunc(void); | |
typedef struct { | |
int a; | |
int b; | |
MyFunc *c; | |
} MyTransition; | |
typedef struct { | |
size_t x; | |
size_t y; | |
} MyDim; | |
#define MyTableXY(n, dim_y, dim_x, ...) \ | |
struct { \ | |
MyDim dim; \ | |
MyTransition transitions[dim_y][dim_x]; \ | |
} n = {.dim = {.x = dim_x, .y = dim_y}, .transitions = {__VA_ARGS__}} | |
#define MyTableXYToArg(t) &t.dim, &t.transitions[0][0] | |
#define MyTableXYArg(d, t) MyDim const *d, MyTransition const *t | |
#define MyTableXYTrans(t, y, x) t.transitions[y][x] | |
static void printTable(MyTableXYArg(dim, trans)) { | |
printf("Dim x = %lu, Dim y = %lu\n\n", dim->x, dim->y); | |
for (size_t cnt = 0; cnt < dim->x * dim->y; cnt++) { | |
printf("{%d, %d}", trans[cnt].a, trans[cnt].b); | |
if ((cnt + 1) % dim->x == 0) { | |
printf("\n"); | |
} else { | |
printf(", "); | |
} | |
} | |
printf("\n"); | |
} | |
static void foo(void) { printf("foo\n"); } | |
static void bar(void) { printf("bar\n"); } | |
int main(void) { | |
MyTableXY(myTable, 3, 2, {{0, 1, &foo}, {2, 3, &bar}}); | |
printTable(MyTableXYToArg(myTable)); | |
MyTableXYTrans(myTable, 0, 0).c(); | |
MyTableXYTrans(myTable, 0, 1).c(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment