]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/registry.qh
cpp compat
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / registry.qh
1 #ifndef REGISTRY_H
2 #define REGISTRY_H
3
4 #include "oo.qh"
5
6 #if 1
7         #define _R_MAP(r, max) ArrayList r; STATIC_INIT(r) { AL_NEW(r, max, NULL, e); }
8         #define _R_GET(r, i) AL_gete(r, i)
9         #define _R_SET(r, i, e) AL_sete(r, i, e)
10         #define _R_DEL(r) AL_DELETE(r)
11 #else
12         #define _R_MAP(r, max) entity r[max]
13         #define _R_GET(r, i) r[i]
14         #define _R_SET(r, i, e) r[i] = e
15         #define _R_DEL(r)
16 #endif
17
18 /**
19  * Declare a new registry.
20  *
21  * Don't forget to call `REGISTER_REGISTRY`:
22  *     REGISTER_REGISTRY(Foos)
23  */
24 #define REGISTRY(id, max) \
25         void Register##id() {} \
26         const int id##_MAX = max; \
27         noref entity id##_first, id##_last; \
28         _R_MAP(_##id, id##_MAX); \
29         SHUTDOWN(id) { _R_DEL(_##id); } \
30         int id##_COUNT; \
31         entity _##id##_from(int i, entity null) { if (i >= 0 && i < id##_COUNT) { entity e = _R_GET(_##id, i); if (e) return e; } return null; }
32
33 REGISTRY(Registries, BITS(8))
34
35 /** registered item identifier */
36 .string registered_id;
37
38 /**
39  * Register a new entity with a registry.
40  * Must be followed by a semicolon or a function body with a `this` parameter.
41  * Wrapper macros may perform actions after user initialization like so:
42  *     #define REGISTER_FOO(id) \
43  *         REGISTER(Foos, FOO, id, m_id, NEW(Foo)); \
44  *         REGISTER_INIT_POST(FOO, id) { \
45  *             print("Registering foo #", this.m_id + 1, "\n"); \
46  *         } \
47  *         REGISTER_INIT(FOO, id)
48  *
49  *
50  * @param registry  The registry to add each entity to.
51  * @param ns        Short for namespace, prefix for each global (ns##_##id)
52  * @param id        The identifier of the current entity being registered
53  * @param fld       The field to store the locally unique unique entity id
54  * @param inst      An expression to create a new instance, invoked for every registration
55  */
56 #define REGISTER(...) EVAL_REGISTER(OVERLOAD(REGISTER, __VA_ARGS__))
57 #define EVAL_REGISTER(...) __VA_ARGS__
58 #define REGISTER_5(registry, ns, id, fld, inst) REGISTER_4(registry, ns##_##id, fld, inst)
59 #define REGISTER_4(registry, id, fld, inst) \
60         entity id; \
61         REGISTER_INIT(id) {} \
62         REGISTER_INIT_POST(id) {} \
63         void Register_##id() \
64         { \
65                 if (registry##_COUNT >= registry##_MAX) LOG_FATALF("Registry capacity exceeded (%s)", ftos(registry##_MAX)); \
66                 entity this = id = inst; \
67                 this.registered_id = #id; \
68                 REGISTRY_PUSH(registry, fld, this); \
69                 Register_##id##_init(this); \
70                 Register_##id##_init_post(this); \
71         } \
72         ACCUMULATE_FUNCTION(Register##registry, Register_##id) \
73         REGISTER_INIT(id)
74
75 #define REGISTRY_PUSH(registry, fld, it) MACRO_BEGIN { \
76         it.fld = registry##_COUNT; \
77         _R_SET(_##registry, registry##_COUNT, it); \
78         ++registry##_COUNT; \
79         if (!registry##_first) registry##_first = it; \
80         if (registry##_last)   registry##_last.REGISTRY_NEXT = it; \
81         registry##_last = it; \
82 } MACRO_END
83
84 #define REGISTRY_RESERVE(registry, fld, id, suffix) MACRO_BEGIN { \
85         entity e = new_pure(registry_reserved); \
86         e.registered_id = #id "/" #suffix; \
87         REGISTRY_PUSH(registry, fld, e); \
88 } MACRO_END
89
90 #define REGISTER_INIT(id) [[accumulate]] void Register_##id##_init(entity this)
91 #define REGISTER_INIT_POST(id) [[accumulate]] void Register_##id##_init_post(entity this)
92
93 /** internal next pointer */
94 #define REGISTRY_NEXT enemy
95 .entity REGISTRY_NEXT;
96
97 #define REGISTRY_SORT(...) EVAL_REGISTRY_SORT(OVERLOAD(REGISTRY_SORT, __VA_ARGS__))
98 #define EVAL_REGISTRY_SORT(...) __VA_ARGS__
99 #define REGISTRY_SORT_1(id) REGISTRY_SORT_2(id, 0)
100 #define REGISTRY_SORT_2(id, skip) \
101         void _REGISTRY_SWAP_##id(int i, int j, entity pass) \
102         { \
103                 i += skip; j += skip; \
104                 \
105                 entity a = _R_GET(_##id, i), b = _R_GET(_##id, j); \
106                 _R_SET(_##id, i, b); \
107                 _R_SET(_##id, j, a); \
108         \
109                 entity a_next = a.REGISTRY_NEXT, b_next = b.REGISTRY_NEXT; \
110                 a.REGISTRY_NEXT = b_next; \
111                 b.REGISTRY_NEXT = a_next; \
112         \
113                 if (i == 0) id##_first = b; \
114                 else _R_GET(_##id, i - 1).REGISTRY_NEXT = b; \
115         \
116                 if (j == 0) id##_first = a; \
117                 else _R_GET(_##id, j - 1).REGISTRY_NEXT = a; \
118         } \
119         int _REGISTRY_CMP_##id(int i, int j, entity pass) \
120         { \
121                 i += skip; j += skip; \
122                 string a = _R_GET(_##id, i).registered_id; \
123                 string b = _R_GET(_##id, j).registered_id; \
124                 return strcmp(a, b); \
125         } \
126         STATIC_INIT(Registry_sort_##id) \
127         { \
128                 heapsort(id##_COUNT - (skip), _REGISTRY_SWAP_##id, _REGISTRY_CMP_##id, NULL); \
129         }
130
131 #define REGISTRY_HASH(id) Registry_hash_##id
132
133 [[accumulate]] void Registry_check(string r, string server) { }
134 [[accumulate]] void Registry_send_all() { }
135
136 #ifdef SVQC
137 void Registry_send(string id, string hash);
138 #else
139 #define Registry_send(id, hash)
140 #endif
141
142 #define REGISTRY_CHECK(id) \
143         string REGISTRY_HASH(id); \
144         STATIC_INIT(Registry_check_##id) \
145         { \
146                 /* Note: SHA256 isn't always available, use MD4 instead */ \
147                 string algo = "MD4"; \
148                 string join = ":"; \
149                 string s = ""; \
150                 FOREACH(id, true, LAMBDA(s = strcat(s, join, it.registered_id))); \
151                 s = substring(s, strlen(join), -1); \
152                 string h = REGISTRY_HASH(id) = strzone(digest_hex(algo, s)); \
153                 LOG_TRACEF(#id ": %s\n[%s]\n", h, s); \
154         } \
155         void Registry_check(string r, string sv) \
156         { \
157                 if (r == #id) \
158                 { \
159                         string cl = REGISTRY_HASH(id); \
160                         if (cl != sv) \
161                         { \
162                                 LOG_FATALF("client/server mismatch (%s).\nCL: %s\nSV: %s\n", r, cl, sv); \
163                         } \
164                 } \
165         } \
166         void Registry_send_all() { Registry_send(#id, REGISTRY_HASH(id)); } \
167
168 #define REGISTER_REGISTRY(...) EVAL_REGISTER_REGISTRY(OVERLOAD(REGISTER_REGISTRY, __VA_ARGS__))
169 #define EVAL_REGISTER_REGISTRY(...) __VA_ARGS__
170 #define REGISTER_REGISTRY_1(id) REGISTER_REGISTRY_2(id, #id)
171 #define REGISTER_REGISTRY_2(id, str) \
172         ACCUMULATE_FUNCTION(__static_init, Register##id) \
173         CLASS(id##Registry, Object) \
174                 ATTRIB(id##Registry, m_name, string, str) \
175                 ATTRIB(id##Registry, REGISTRY_NEXT, entity, id##_first) \
176         ENDCLASS(id##Registry) \
177         REGISTER(Registries, REGISTRY, id, m_id, NEW(id##Registry));
178
179
180 #endif