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