]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/registry.qh
Registry: overflow check
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / registry.qh
1 #ifndef REGISTRY_H
2 #define REGISTRY_H
3
4 #include "oo.qh"
5
6 #define REGISTER_INIT(ns, id) [[accumulate]] void Register_##ns##_##id##_init(entity this)
7 #define REGISTER_INIT_POST(ns, id) [[accumulate]] void Register_##ns##_##id##_init_post(entity this)
8
9 #define REGISTRY(id, max) \
10     void Register##id() {} \
11     const int id##_MAX = max; \
12     noref entity id[id##_MAX], id##_first, id##_last; \
13     int id##_COUNT;
14
15 /**
16  * Register a new entity with a global constructor.
17  * Must be followed by a semicolon or a function body with a `this` parameter.
18  * Wrapper macros may perform actions after user initialization like so:
19  *     #define REGISTER_FOO(id) \
20  *         REGISTER(RegisterFoos, FOO, FOOS, id, m_id, NEW(Foo)); \
21  *         REGISTER_INIT_POST(FOO, id) { \
22  *             print("Registering foo #", this.m_id + 1, "\n"); \
23  *         } \
24  *         REGISTER_INIT(FOO, id)
25  *
26  * Don't forget to forward declare `initfunc` and call `REGISTER_REGISTRY`:
27  *     void RegisterFoos();
28  *     REGISTER_REGISTRY(RegisterFoos)
29  *
30  * @param initfunc  The global constructor to accumulate into
31  * @param ns        Short for namespace, prefix for each global (ns##_##id)
32  * @param array     The array to add each entity to. Also requires `array##_first` and `array##_last` to be defined
33  * @param id        The identifier of the current entity being registered
34  * @param fld       The field to store the current count into
35  * @param inst      An expression to create a new instance, invoked for every registration
36  */
37 #define REGISTER(initfunc, ns, array, id, fld, inst)            \
38     entity ns##_##id;                                           \
39     REGISTER_INIT(ns, id) { }                                   \
40     REGISTER_INIT_POST(ns, id) { }                              \
41     .entity enemy; /* internal next pointer */                  \
42     void Register_##ns##_##id() {                               \
43         if (array##_COUNT >= array##_MAX) LOG_FATALF("Registry capacity exceeded (%s)", ftos(array##_MAX)); \
44         entity this = inst;                                     \
45         ns##_##id = this;                                       \
46         this.fld = array##_COUNT;                               \
47         array[array##_COUNT++] = this;                          \
48         if (!array##_first)    array##_first = this;            \
49         if ( array##_last)     array##_last.enemy = this;       \
50         array##_last = this;                                    \
51         Register_##ns##_##id##_init(this);                      \
52         Register_##ns##_##id##_init_post(this);                 \
53     }                                                           \
54     ACCUMULATE_FUNCTION(initfunc, Register_##ns##_##id)         \
55     REGISTER_INIT(ns, id)
56
57 #define REGISTRY_SORT(id, field, skip)                          \
58     void _REGISTRY_SWAP_##id(int i, int j, entity pass) {       \
59         i += skip; j += skip;                                   \
60         entity e = id[i];                                       \
61         id[i] = id[j];                                          \
62         id[j] = e;                                              \
63     }                                                           \
64     float _REGISTRY_CMP_##id(int i, int j, entity pass) {       \
65         i += skip; j += skip;                                   \
66         string a = id[i].field;                                 \
67         string b = id[j].field;                                 \
68         return strcasecmp(a, b);                                \
69     }                                                           \
70     STATIC_INIT(Registry_sort_##id) {                           \
71         heapsort(id##_COUNT - (skip), _REGISTRY_SWAP_##id, _REGISTRY_CMP_##id, NULL); \
72     }
73
74 #endif