-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathd_buf_macro.h
More file actions
183 lines (181 loc) · 14.1 KB
/
d_buf_macro.h
File metadata and controls
183 lines (181 loc) · 14.1 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#pragma once
// this macro's been pulled into its own header for readability's sake
// i wanted to invoke the dbuf_decl macro for some of the types in `derp.h`
// but i didn't want to paste the entire macro into the middle of the file
// so instead, it lives here.
//
// be warned though that this macro depends on many of the types and macros defined
// in `derp.h`, so it must never be included on it's own.
// T is the base type, N is the name for the outputted datatype ( eg for when you have a char* vec, but you want to call it a string vec )
#define dbuf_decl(T, N) \
typedef struct dbuf_##N { \
isize cur, len; \
T *data; \
d_allocator alloc; \
} dbuf_##N; \
\
static DATTR_UNUSED inline dbuf_##N dbuf_##N##_make(isize init) { \
dbuf_data buf = {0}; \
dbuf_##N n = {0}; \
\
buf.data = (void **)&n.data; \
buf.cur = &n.cur; \
buf.len = &n.len; \
buf.alloc = &n.alloc; \
buf.elem_size = sizeof(T); \
_dbuf_make(init, buf); \
return n; \
} \
\
static DATTR_UNUSED inline dbuf_##N dbuf_##N##_init(T *data, isize init) { \
dbuf_data buf = {0}; \
dbuf_##N n = {0}; \
\
buf.data = (void **)&n.data; \
buf.cur = &n.cur; \
buf.len = &n.len; \
buf.alloc = &n.alloc; \
buf.elem_size = sizeof(T); \
_dbuf_init((void **)&data, init, buf); \
return n; \
} \
\
static DATTR_UNUSED inline dbuf_##N dbuf_##N##_make_alloc( \
isize init, d_allocator alloc) { \
dbuf_data buf = {0}; \
dbuf_##N n = {0}; \
\
buf.data = (void **)&n.data; \
buf.cur = &n.cur; \
buf.len = &n.len; \
buf.alloc = &alloc; \
buf.elem_size = sizeof(T); \
_dbuf_make_alloc(init, alloc, buf); \
return n; \
} \
\
static DATTR_UNUSED inline dbuf_##N dbuf_##N##_init_alloc( \
T *data, isize init, d_allocator alloc) { \
dbuf_data buf = {0}; \
dbuf_##N n = {0}; \
\
buf.data = (void **)&n.data; \
buf.cur = &n.cur; \
buf.len = &n.len; \
buf.alloc = &alloc; \
buf.elem_size = sizeof(T); \
_dbuf_init_alloc((void **)&data, init, alloc, buf); \
return n; \
} \
\
static DATTR_UNUSED inline void dbuf_##N##_push(dbuf_##N *d, T i) { \
dbuf_data buf = {0}; \
buf.data = (void **)&d->data; \
buf.cur = &d->cur; \
buf.len = &d->len; \
buf.elem_size = sizeof(T); \
buf.alloc = &d->alloc; \
_dbuf_push(&i, buf); \
} \
\
static DATTR_UNUSED inline void dbuf_##N##_insert(dbuf_##N *d, T i, \
isize id) { \
dbuf_data buf = {0}; \
buf.data = (void **)&d->data; \
buf.cur = &d->cur; \
buf.len = &d->len; \
buf.elem_size = sizeof(T); \
buf.alloc = &d->alloc; \
_dbuf_insert(&i, id, buf); \
} \
\
static DATTR_UNUSED inline void dbuf_##N##_remove(dbuf_##N *d, isize id) { \
dbuf_data buf = {0}; \
buf.data = (void **)&d->data; \
buf.cur = &d->cur; \
buf.len = &d->len; \
buf.elem_size = sizeof(T); \
buf.alloc = &d->alloc; \
_dbuf_remove(id, buf); \
} \
\
static DATTR_UNUSED inline T dbuf_##N##_pop(dbuf_##N *d) { \
dbuf_data buf = {0}; \
buf.data = (void **)&d->data; \
buf.cur = &d->cur; \
buf.len = &d->len; \
buf.elem_size = sizeof(T); \
buf.alloc = &d->alloc; \
T *x = (T *)_dbuf_pop(buf); \
return *x; \
} \
\
static DATTR_UNUSED inline void dbuf_##N##_append(dbuf_##N *d, T *arr, \
isize len) { \
dbuf_data buf = {0}; \
buf.data = (void **)&d->data; \
buf.cur = &d->cur; \
buf.len = &d->len; \
buf.elem_size = sizeof(T); \
buf.alloc = &d->alloc; \
_dbuf_append((void **)arr, len, buf); \
} \
\
static DATTR_UNUSED inline void dbuf_##N##_prepend(dbuf_##N *d, T *arr, \
isize len) { \
dbuf_data buf = {0}; \
buf.data = (void **)&d->data; \
buf.cur = &d->cur; \
buf.len = &d->len; \
buf.elem_size = sizeof(T); \
buf.alloc = &d->alloc; \
_dbuf_prepend((void **)arr, len, buf); \
} \
\
static DATTR_UNUSED inline T dbuf_##N##_get(dbuf_##N *d, isize id) { \
dbuf_data buf = {0}; \
buf.data = (void **)&d->data; \
buf.cur = &d->cur; \
buf.len = &d->len; \
buf.elem_size = sizeof(T); \
buf.alloc = &d->alloc; \
T *x = (T *)_dbuf_get(id, buf); \
return *x; \
} \
\
static DATTR_UNUSED inline void dbuf_##N##_set(dbuf_##N *d, isize id, T i) { \
dbuf_data buf = {0}; \
buf.data = (void **)&d->data; \
buf.cur = &d->cur; \
buf.len = &d->len; \
buf.elem_size = sizeof(T); \
buf.alloc = &d->alloc; \
_dbuf_set(id, &i, buf); \
} \
\
static DATTR_UNUSED inline void dbuf_##N##_reserve(dbuf_##N *d, isize len) { \
dbuf_data buf = {0}; \
buf.data = (void **)&d->data; \
buf.cur = &d->cur; \
buf.len = &d->len; \
buf.elem_size = sizeof(T); \
buf.alloc = &d->alloc; \
_dbuf_reserve(len, buf); \
} \
\
static DATTR_UNUSED inline dbuf_##N dbuf_##N##_clone(dbuf_##N src) { \
dbuf_##N dest = {0}; \
dbuf_data buf = {0}; \
buf.data = (void **)&dest.data; \
buf.cur = &dest.cur; \
buf.len = &dest.len; \
buf.elem_size = sizeof(T); \
buf.alloc = &dest.alloc; \
dbuf _src = (dbuf){.data = (void **)src.data, \
.len = src.len, \
.cur = src.cur, \
.alloc = &src.alloc, \
.elem_size = sizeof(T)}; \
_dbuf_clone(buf, _src); \
return dest; \
}\