-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoption.c.m4
More file actions
51 lines (42 loc) · 1.16 KB
/
option.c.m4
File metadata and controls
51 lines (42 loc) · 1.16 KB
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
#include <stdlib.h>
#include <stdio.h>
include(option.m4);
OPTION(int) side_effects(int num) {
printf("I'm side effect n°%d\n", num);
//return SOME(int, 101);
return NONE(int);
}
OPTION(void*) omalloc(size_t size) {
void* ptr = malloc(size);
return ptr ? SOME(void*, ptr) : NONE(void*);
}
OPTION(char *) *test_string() {
OPTION(char *) *r = OPT_UNWRAP(omalloc(sizeof(OPTION(char *))));
*r = SOME(char *, "Hello World");
return r;
}
OPTION(int) test() {
//return SOME(int, 5);
return NONE(int);
}
int main() {
int c = 20;
int k = OPT_UNWRAP_OR(side_effects(1), {printf("Assigning c to k\n"); c;});
printf("k: %d\n", k);
int* i = OPT_UNWRAP_OR(omalloc(sizeof(int)), {_Exit(1); NULL;});
OPTION(int) x = test();
if (IS_SOME(x)) {
printf("Got some: %d\n", OPT_UNWRAP(x));
} else {
printf("Got none, default: %d\n", OPT_UNWRAP_OR(x, 99));
}
OPTION(char *) *str = test_string();
if (IS_SOME_P(str)) {
printf("Got some: %s\n", OPT_UNWRAP_P(str));
}
OPTION(char) ch = SOME(char, 'a');
printf("ch: %c\n", OPT_UNWRAP(ch));
free(i);
free(str);
return 0;
}