summaryrefslogtreecommitdiff
path: root/src/runtime/c/gu/fun.h
blob: 5c14de6e96b6a4d0b5b583d0298840e494ec2320 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#ifndef GU_FUN_H_
#define GU_FUN_H_

#include <gu/defs.h>

typedef void (*GuFn)();
typedef void (*GuFn0)(GuFn* clo);
typedef void (*GuFn1)(GuFn* clo, void* arg1);
typedef void (*GuFn2)(GuFn* clo, void* arg1, void* arg2);

#define gu_fn(fn_) (&(GuFn){ fn_ })

static inline void
gu_apply0(GuFn* fn) {
	((GuFn0)(*fn))(fn);
}

static inline void
gu_apply1(GuFn* fn, void* arg1) {
	((GuFn1)(*fn))(fn, arg1);
}

static inline void
gu_apply2(GuFn* fn, void* arg1, void* arg2) {
	((GuFn2)(*fn))(fn, arg1, arg2);
}

#define gu_apply(fn_, ...)			\
	((fn_)->fn((fn_), __VA_ARGS__))

typedef struct GuClo0 GuClo0;

struct GuClo0 {
	GuFn fn;
};

typedef struct GuClo1 GuClo1;

struct GuClo1 {
	GuFn fn;
	void *env1;
};

typedef struct GuClo2 GuClo2;
struct GuClo2 {
	GuFn fn;
	void *env1;
	void *env2;
};

typedef struct GuClo3 GuClo3;
struct GuClo3 {
	GuFn fn;
	void *env1;
	void *env2;
	void *env3;
};

typedef const struct GuEquality GuEquality;

struct GuEquality {
	bool (*is_equal)(GuEquality* self, const void* a, const void* b);
};

typedef const struct GuOrder GuOrder;

struct GuOrder {
	int (*compare)(GuOrder* self, const void* a, const void* b);
};

#endif // GU_FUN_H_