-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmacros.odin
293 lines (234 loc) · 7.46 KB
/
macros.odin
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// File contains implementations of the Orca API that are defined as macros in Orca.
package orca
import "core:fmt"
////////////////////////////////////////////////////////////////////////////////
// Helpers for logging, asserting and aborting.
////////////////////////////////////////////////////////////////////////////////
log_error :: proc "contextless" (msg: cstring, loc := #caller_location) {
log_ext(
.ERROR,
cstring(raw_data(loc.procedure)),
cstring(raw_data(loc.file_path)),
loc.line,
msg,
)
}
log_errorf :: proc(format: string, args: ..any, loc := #caller_location) {
log_ext(
.ERROR,
cstring(raw_data(loc.procedure)),
cstring(raw_data(loc.file_path)),
loc.line,
fmt.ctprintf(format, ..args),
)
}
log_warning :: proc "contextless" (msg: cstring, loc := #caller_location) {
log_ext(
.WARNING,
cstring(raw_data(loc.procedure)),
cstring(raw_data(loc.file_path)),
loc.line,
msg,
)
}
log_warningf :: proc(format: string, args: ..any, loc := #caller_location) {
log_ext(
.WARNING,
cstring(raw_data(loc.procedure)),
cstring(raw_data(loc.file_path)),
loc.line,
fmt.ctprintf(format, ..args),
)
}
log_info :: proc "contextless" (msg: cstring, loc := #caller_location) {
log_ext(
.INFO,
cstring(raw_data(loc.procedure)),
cstring(raw_data(loc.file_path)),
loc.line,
msg,
)
}
log_infof :: proc(format: string, args: ..any, loc := #caller_location) {
log_ext(
.INFO,
cstring(raw_data(loc.procedure)),
cstring(raw_data(loc.file_path)),
loc.line,
fmt.ctprintf(format, ..args),
)
}
abort :: proc "contextless" (msg: cstring, loc := #caller_location) {
abort_ext(
cstring(raw_data(loc.procedure)),
cstring(raw_data(loc.file_path)),
loc.line,
msg,
)
}
abortf :: proc(format: string, args: ..any, loc := #caller_location) {
abort_ext(
cstring(raw_data(loc.procedure)),
cstring(raw_data(loc.file_path)),
loc.line,
fmt.ctprintf(format, ..args),
)
}
////////////////////////////////////////////////////////////////////////////////
// Types and helpers for doubly-linked lists.
////////////////////////////////////////////////////////////////////////////////
// Get the entry for a given list element.
list_entry :: proc "contextless" (elt: ^list_elt, $T: typeid, $member: string) -> ^T {
return container_of(elt, T, member)
}
// Get the next entry in a list.
list_next_entry :: proc "contextless" (list: ^list, elt: ^list_elt, $T: typeid, $member: string) -> ^T {
if elt.next != list.last {
return list_entry(elt.next, T, member)
}
return nil
}
// Get the previous entry in a list.
list_prev_entry :: proc "contextless" (list: ^list, elt: ^list_elt, $T: typeid, $member: string) -> ^T {
if elt.prev != list.last {
return list_entry(elt.prev, T, member)
}
return nil
}
// Same as `list_entry` but `elt` might be `nil`.
list_checked_entry :: proc "contextless" (elt: ^list_elt, $T: typeid, $member: string) -> ^T {
if elt != nil {
return list_entry(elt, T, member)
}
return nil
}
list_first_entry :: proc "contextless" (list: ^list, $T: typeid, $member: string) -> ^T {
return list_checked_entry(list.first, T, member)
}
list_last_entry :: proc "contextless" (list: ^list, $T: typeid, $member: string) -> ^T {
return list_checked_entry(list.last, T, member)
}
// Example:
//
// _elt: ^list_elt
// for elt in oc.list_for(list, &_elt, int, "elt") {
// }
list_for :: proc "contextless" (list: ^list, elt: ^^list_elt, $T: typeid, $member: string) -> (^T, bool) {
if elt == nil {
assert_fail(#file, #procedure, #line, "elt != nil", "misuse of `list_for`, expected `elt` to not be nil")
}
if elt^ == nil {
elt^ = list.first
entry := list_checked_entry(elt^, T, member)
return entry, entry != nil
}
elt^ = elt^.next
entry := list_checked_entry(elt^, T, member)
return entry, entry != nil
}
list_iter :: list_for
list_for_reverse :: proc "contextless" (list: ^list, elt: ^^list_elt, $T: typeid, $member: string) -> (^T, bool) {
if elt^ == nil {
elt^ = list.last
entry := list_checked_entry(elt^, T, member)
return entry, entry != nil
}
elt^ = elt^.prev
entry := list_checked_entry(elt^, T, member)
return entry, entry != nil
}
list_iter_reverse :: list_for_reverse
list_pop_front_entry :: proc "contextless" (list: ^list, $T: typeid, $member: string) -> ^T {
if list_empty(list^) {
return nil
}
return list_entry(list_pop_front(list), T, member)
}
list_pop_back_entry :: proc "contextless" (list: ^list, $T: typeid, $member: string) -> ^T {
if list_empty(list^) {
return nil
}
return list_entry(list_pop_back(list), T, member)
}
////////////////////////////////////////////////////////////////////////////////
// Base allocator and memory arenas.
////////////////////////////////////////////////////////////////////////////////
arena_push_type :: proc "contextless" (arena: ^arena, $T: typeid) -> ^T {
return (^T)(arena_push_aligned(arena, size_of(T), align_of(T)))
}
arena_push_array :: proc "contextless" (arena: ^arena, $T: typeid, count: u64) -> []T {
return ([^]T)(arena_push_aligned(arena, size_of(T) * count, align_of(T)))[:count]
}
scratch_end :: arena_scope_end
////////////////////////////////////////////////////////////////////////////////
// String slices and string lists.
////////////////////////////////////////////////////////////////////////////////
str8_list_first :: proc "contextless" (sl: ^str8_list) -> str8 {
if list_empty(sl.list) {
return ""
}
return list_first_entry(&sl.list, str8_elt, "listElt").string
}
str8_list_last :: proc "contextless" (sl: ^str8_list) -> str8 {
if list_empty(sl.list) {
return ""
}
return list_last_entry(&sl.list, str8_elt, "listElt").string
}
str8_list_for :: proc "contextless" (list: ^str8_list, elt: ^^list_elt) -> (^str8_elt, bool) {
return list_for(&list.list, elt, str8_elt, "listElt")
}
str8_list_iter :: str8_list_for
str8_list_empty :: proc "contextless" (list: str8_list) -> bool {
return list_empty(list.list)
}
str16_list_first :: proc "contextless" (sl: ^str16_list) -> str16 {
if list_empty(sl.list) {
return {}
}
return list_first_entry(&sl.list, str16_elt, "listElt").string
}
str16_list_last :: proc "contextless" (sl: ^str16_list) -> str16 {
if list_empty(sl.list) {
return {}
}
return list_last_entry(&sl.list, str16_elt, "listElt").string
}
str16_list_for :: proc "contextless" (list: ^str16_list, elt: ^^list_elt) -> (^str16_elt, bool) {
return list_for(&list.list, elt, str16_elt, "listElt")
}
str32_list_first :: proc "contextless" (sl: ^str32_list) -> str32 {
if list_empty(sl.list) {
return {}
}
return list_first_entry(&sl.list, str32_elt, "listElt").string
}
str32_list_last :: proc "contextless" (sl: ^str32_list) -> str32 {
if list_empty(sl.list) {
return {}
}
return list_last_entry(&sl.list, str32_elt, "listElt").string
}
str32_list_for :: proc "contextless" (list: ^str32_list, elt: ^^list_elt) -> (^str32_elt, bool) {
return list_for(&list.list, elt, str32_elt, "listElt")
}
@(deferred_none=ui_box_end)
ui_container :: proc "contextless" (name: string, flags: ui_flags = {}) -> ^ui_box {
return ui_box_begin_str8(name, flags)
}
@(deferred_none=ui_end_frame)
ui_frame :: proc "contextless" (frame_size: [2]f32, style: ui_style, mask: ui_style_mask) {
ui_begin_frame(frame_size, style, mask)
}
@(deferred_none=ui_panel_end)
ui_panel :: proc "contextless" (name: cstring, flags: ui_flags) {
ui_panel_begin(name, flags)
}
@(deferred_none=ui_menu_end)
ui_menu :: proc "contextless" (name: cstring) {
ui_menu_begin(name)
}
@(deferred_none=ui_menu_bar_end)
ui_menu_bar :: proc "contextless" (name: cstring) {
ui_menu_bar_begin(name)
}