/
vtb.h
386 lines (263 loc) · 8.52 KB
/
vtb.h
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
vtb.h - Vino's Tool Box
This software is dual-licensed to the public domain and under the
following license: you are granted a perpetual, irrevocable license
to copy, modify, publish, and distribute this file as you see fit.
This header file has a collection of useful utilities that I use often in my
projects. You'll find most of this stuff in the header of any large project.
I'm putting it all in one place for code reuse reasons.
See https://github.com/nothings/stb for the inspiration
See https://www.youtube.com/watch?v=eAhWIO1Ra6M for the motivation
COMPILING AND LINKING
You must
#define VTB_IMPLEMENTATION
in exactly one C or C++ file that includes this header, before the include
like this:
#define VTB_IMPLEMENTATION
#include "vtb.h"
All other files can be just #include "vtb.h" without the #define
*/
#ifndef VTB_H
#define VTB_H
#ifdef VTB_STATIC
#define VTBDEF static
#else
#define VTBDEF extern
#endif
#ifdef __clang__
#define VINLINE __attribute__((always_inline)) inline
#else
#error "Unsupported compiler"
#endif
// VDebugBreak - Forces a trap to break to the debugger
#ifdef __GNUC__
#if defined(__i386__) || defined(__x86_64__)
#define VDebugBreak() __asm__ __volatile__ ( "int $3\n\t" )
#else
#include <csignal>
#define VDebugBreak() \
::raise(SIGTRAP); \
#endif
#else
#define VDebugBreak() \
__debugbreak(); \
#endif
#ifdef _MSC_VER
#define VPRAGMA_WARNING_PUSH __pragma(warning(push))
#define VPRAGMA_WARNING_DISABLE(n) __pragma(warning(disable:n))
#define VPRAGMA_WARNING_POP __pragma(warning(pop))
#else
#define VPRAGMA_WARNING_PUSH
#define VPRAGMA_WARNING_DISABLE(n)
#define VPRAGMA_WARNING_POP
#endif
// vtb_debug_print - Forwards the text to the debugger's output pane, if it exists.
// The Windows equivalent is OutputDebugString.
VTBDEF void vtb_debug_print(const char* text);
// VAssert(x) - Triggers a breakpoint if x is false.
#if !defined(VTB_DISABLE_ASSERTS)
#include <stdio.h>
#define VAssert(x) \
do { \
VPRAGMA_WARNING_PUSH \
VPRAGMA_WARNING_DISABLE(4127) /* conditional expression is constant */ \
if (!(x)) \
VPRAGMA_WARNING_POP \
{ \
char vbuf[1024]; \
sprintf(vbuf, "Assert failed: %s (%s:%d)\n", #x, __FILE__, __LINE__); \
vtb_debug_print(vbuf); \
VDebugBreak(); \
} \
VPRAGMA_WARNING_PUSH \
VPRAGMA_WARNING_DISABLE(4127) /* conditional expression is constant */ \
} while (0) \
VPRAGMA_WARNING_POP \
#else
#define VAssert(x) do { \
VPRAGMA_WARNING_PUSH \
VPRAGMA_WARNING_DISABLE(4127) /* conditional expression is constant */ \
if (!(x)) /* Still use the value so that the compiler doesn't complain about unused values. */ \
VPRAGMA_WARNING_POP \
{} \
VPRAGMA_WARNING_PUSH \
VPRAGMA_WARNING_DISABLE(4127) /* conditional expression is constant */ \
} while (0) \
VPRAGMA_WARNING_POP \
#endif
// VCheck and VAssert - These are the same thing internally, but VAssert means program
// state is definitely wrong and probably unrecoverable (eg algorithm
// invariants), while VCheck just means something less serious but still
// unexpected happened. Generally use VCheck for things like checking function
// parameters, and other things that you can recover from, and VAssert for
// things that the program should die if they go wrong.
#define VCheck(x) VAssert(x)
// VUnimplemented - Indicates code that has not been written yet. Generally I don't
// write code that I don't immediately need, and in branches that need to exist
// for completeness but that I currently don't use, I place a VUnimplemented().
// This saves time so that I don't write things I don't use, and it reduces bugs
// because I don't write code that has no clients and so is not well enough tested.
//
// Example:
// if (a.is_empty())
// a.push(foo);
// else
// VUnimplemented(); // I don't currently use the non-empty case.
//
#if defined(__ANDROID__)
// If you hit this, the code is incomple
#define VUnimplemented() do { \
char s[1000]; \
sprintf(s, "VUnimplemented file " __FILE__ " line %d\n", __LINE__); \
DebugPrint(s); \
TAssertNoMsg(false); \
} while (0)
#else
// If you hit this, the code is incomple
#define VUnimplemented() VAssert(false)
#endif
// VUntested - Sometimes where I would put a VUnimplemented, instead I will write
// a general outline for what the code should look like, but drop a VUntested()
// in front. It's often easier to write the code while I still have the context
// in mind, but I don't want to invest the time to debug the code. If you see
// this, it means you shouldn't take for granted that the code has been debugged.
#define VUntested() VAssert(false)
// VStubbed - For porting large sections of code. Instead of trying to write
// all of the ported code at once, I comment anything that doesn't compile and
// write: VStubbed("Font Loading") or whatever it is that's not ported yet.
// This way I can get code running faster.
#ifdef _DEBUG
// Ruthlessly remove this if you see it.
#define VStubbed(x) \
do { \
static bool seen = false; \
if (!seen) { \
seen = true; \
vtb_debug_print("STUBBED: " x "\n"); \
} \
} while (false); \
#else
// Code will not build in release mode until stubs are removed.
#define VStubbed(x) ERROR!
#endif
#ifdef __ANDROID__
// A platform whose major input method is touch, and does not have a mouse.
#define V_PLATFORM_TOUCH
// A platform whose rendering capabilities are limited.
#define V_PLATFORM_MOBILE
#endif
#include <algorithm>
#define vmin(a, b) std::min((a), (b))
#define vmax(a, b) std::max((a), (b))
// VInvalid - I often use unsigned data types as handles. The very last representable
// number is reserved as an invalid value for error checking purposes. VInvalid is a
// type- safe way of handling those invalid values. Example:
//
// typedef uint32 ListHandle;
//
// ListHandle a = VInvalid(ListHandle);
// if (a == VInvalid(ListHandle))
// ...
//
#define VInvalid(type) ((type)~0)
// VStackAllocate - Allocate data on the stack
#ifdef _MSC_VER
// No VLA's. Use alloca()
#include <malloc.h>
#define VStackAllocate(type, name, size) type* name = (type*)alloca(size*sizeof(type))
#else
#define VStackAllocate(type, name, size) type name[size]
#endif
// VDefer - A defer statement for C++
// Usage:
// {
// void* p = malloc(123);
// VDefer(free(p));
// ... stuff
// } <-- free(p) is run here at scope end.
// http://the-witness.net/news/2012/11/scopeexit-in-c11/
template <typename F>
struct VDeferrer {
VDeferrer(F f) : f(f) {}
~VDeferrer() { f(); }
F f;
};
template <typename F>
VDeferrer<F> MakeVDeferrer(F f) {
return VDeferrer<F>(f);
};
// No idea why this much indirection is required.
#define VStringConcat2(arg1, arg2) VStringJoin(arg1, arg2)
#define VStringJoin(arg1, arg2) arg1 ## arg2
#define VDefer(code) \
auto VStringConcat2(scope_exit_, __LINE__) = MakeVDeferrer([&](){code;})
// VArraySize - A macro for anything defined in the format: type name[size];
// VArraySize(name) will return size.
// Note: It only works on the original array, not on a pointer to that array.
#define VArraySize(x) (sizeof(x)/sizeof(x[0]))
// Rounds x up to the nearest multiple of n. Useful for aligning memory.
template<typename T>
T VAlign(T x, T n)
{
T over = x%n;
return over?(x+n-over):x;
}
// Rounds v up to the next highest multiple of 2.
inline int VPo2(int v)
{
// From https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
v--;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
v++;
return v;
}
// A snprintf implementation for VS versions before 2015.
// From http://stackoverflow.com/questions/2915672/snprintf-and-visual-studio-2010
#if defined(_MSC_VER) && _MSC_VER < 1900
#include <stdarg.h>
inline int vtb__vsnprintf(char* outBuf, size_t size, const char* format, va_list ap)
{
int count = -1;
if (size != 0)
count = _vsnprintf_s(outBuf, size, _TRUNCATE, format, ap);
if (count == -1)
count = _vscprintf(format, ap);
return count;
}
inline int vtb__snprintf(char *outBuf, size_t size, const char *format, ...)
{
int count;
va_list ap;
va_start(ap, format);
count = vtb__vsnprintf(outBuf, size, format, ap);
va_end(ap);
return count;
}
#define snprintf vtb__snprintf
#define vsnprintf vtb__vsnprintf
#endif
#endif // VTB_H
#ifdef VTB_IMPLEMENTATION
#ifdef __ANDROID__
VTBDEF void vtb_debug_print(const char* text)
{
__android_log_print(ANDROID_LOG_INFO, "Debug", "%s", text);
}
#elif defined(_MSC_VER)
#include <windows.h>
VTBDEF void vtb_debug_print(const char* text)
{
OutputDebugStringA(text);
}
#else
#include <stdio.h>
VTBDEF void vtb_debug_print(const char* text)
{
puts(text);
}
#endif
#endif