6 #define _R_MAP(r, max) ArrayList r; STATIC_INIT(r) { AL_NEW(r, max, NULL, e); }
7 #define _R_GET(r, i) AL_gete(r, i)
8 #define _R_SET(r, i, e) AL_sete(r, i, e)
9 #define _R_DEL(r) AL_DELETE(r)
11 #define _R_MAP(r, max) entity r[max]
12 #define _R_GET(r, i) r[i]
13 #define _R_SET(r, i, e) r[i] = e
18 * Declare a new registry.
20 * Don't forget to call `REGISTER_REGISTRY`:
21 * REGISTER_REGISTRY(Foos)
23 #define REGISTRY(id, max) \
24 void Register##id(); \
25 [[accumulate]] REGISTRY_BEGIN(id) {} \
26 [[accumulate]] REGISTRY_END(id) {} \
27 void _Register##id() {} \
28 void Register##id() { REGISTRY_BEGIN_(id); _Register##id(); REGISTRY_END_(id); } \
29 const int id##_MAX = max; \
31 noref entity id##_first, id##_last; \
32 _R_MAP(_##id, id##_MAX); \
33 SHUTDOWN(id) { _R_DEL(_##id); } \
34 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; }
36 /** Called before initializing a registry. */
37 #define REGISTRY_BEGIN(id) [[accumulate]] void REGISTRY_BEGIN_(id) { noref void() f = Register##id; } void REGISTRY_BEGIN_(id)
38 #define REGISTRY_BEGIN_(id) Register##id##_First()
40 /** Called after initializing a registry. */
41 #define REGISTRY_END(id) [[accumulate]] void REGISTRY_END_(id) { noref void() f = Register##id; } void REGISTRY_END_(id)
42 #define REGISTRY_END_(id) Register##id##_Done()
44 REGISTRY(Registries, BITS(8))
46 /** registered item identifier */
47 .string registered_id;
50 * Register a new entity with a registry.
51 * Must be followed by a semicolon or a function body with a `this` parameter.
52 * Wrapper macros may perform actions after user initialization like so:
53 * #define REGISTER_FOO(id) \
54 * REGISTER(Foos, FOO, id, m_id, NEW(Foo)) { \
55 * print("Registering foo #", this.m_id + 1, "\n"); \
57 * REGISTER_INIT(FOO, id)
60 * @param registry The registry to add each entity to.
61 * @param ns Short for namespace, prefix for each global (ns##_##id)
62 * @param id The identifier of the current entity being registered
63 * @param fld The field to store the locally unique unique entity id
64 * @param inst An expression to create a new instance, invoked for every registration
66 #define REGISTER(...) EVAL_REGISTER(OVERLOAD_(REGISTER, __VA_ARGS__))
67 #define EVAL_REGISTER(...) __VA_ARGS__
68 #define REGISTER_5(registry, ns, id, fld, inst) REGISTER_4(registry, ns##_##id, fld, inst)
69 #define REGISTER_4(registry, id, fld, inst) \
71 REGISTER_INIT(id) {} \
72 void Register_##id() \
74 if (registry##_COUNT >= registry##_MAX) LOG_FATALF("Registry capacity exceeded (%d)", registry##_MAX); \
75 entity this = id = inst; \
76 this.registered_id = #id; \
77 REGISTRY_PUSH(registry, fld, this); \
78 Register_##id##_init(this); \
80 ACCUMULATE_FUNCTION(_Register##registry, Register_##id) \
83 #define REGISTRY_PUSH(registry, fld, it) MACRO_BEGIN { \
84 it.fld = registry##_COUNT; \
85 _R_SET(_##registry, registry##_COUNT, it); \
87 if (!registry##_first) registry##_first = it; \
88 if (registry##_last) registry##_last.REGISTRY_NEXT = it; \
89 registry##_last = it; \
92 #define REGISTRY_RESERVE(registry, fld, id, suffix) MACRO_BEGIN { \
93 entity e = new_pure(registry_reserved); \
94 e.registered_id = #id "/" #suffix; \
95 REGISTRY_PUSH(registry, fld, e); \
98 #define REGISTER_INIT(id) [[accumulate]] void Register_##id##_init(entity this)
100 /** internal next pointer */
101 #define REGISTRY_NEXT enemy
102 .entity REGISTRY_NEXT;
104 #define REGISTRY_SORT(...) EVAL_REGISTRY_SORT(OVERLOAD(REGISTRY_SORT, __VA_ARGS__))
105 #define EVAL_REGISTRY_SORT(...) __VA_ARGS__
106 #define REGISTRY_SORT_1(id) REGISTRY_SORT_2(id, 0)
107 #define REGISTRY_SORT_2(id, skip) \
108 void _REGISTRY_SWAP_##id(int i, int j, entity pass) \
110 i += skip; j += skip; \
112 entity a = _R_GET(_##id, i), b = _R_GET(_##id, j); \
113 _R_SET(_##id, i, b); \
114 _R_SET(_##id, j, a); \
116 entity a_next = a.REGISTRY_NEXT, b_next = b.REGISTRY_NEXT; \
117 a.REGISTRY_NEXT = b_next; \
118 b.REGISTRY_NEXT = a_next; \
120 if (i == 0) id##_first = b; \
121 else _R_GET(_##id, i - 1).REGISTRY_NEXT = b; \
123 if (j == 0) id##_first = a; \
124 else _R_GET(_##id, j - 1).REGISTRY_NEXT = a; \
126 int _REGISTRY_CMP_##id(int i, int j, entity pass) \
128 i += skip; j += skip; \
129 string a = _R_GET(_##id, i).registered_id; \
130 string b = _R_GET(_##id, j).registered_id; \
131 return strcmp(a, b); \
133 STATIC_INIT(Registry_sort_##id) \
135 heapsort(id##_COUNT - (skip), _REGISTRY_SWAP_##id, _REGISTRY_CMP_##id, NULL); \
138 #define REGISTRY_HASH(id) Registry_hash_##id
140 [[accumulate]] void Registry_check(string r, string server) { }
141 [[accumulate]] void Registry_send_all() { }
144 void Registry_send(string id, string hash);
146 #define Registry_send(id, hash)
149 #define REGISTRY_CHECK(id) \
150 string REGISTRY_HASH(id); \
151 STATIC_INIT(Registry_check_##id) \
153 /* Note: SHA256 isn't always available, use MD4 instead */ \
154 string algo = "MD4"; \
157 FOREACH(id, true, s = strcat(s, join, it.registered_id)); \
158 s = substring(s, strlen(join), -1); \
159 string h = REGISTRY_HASH(id) = strzone(digest_hex(algo, s)); \
160 LOG_DEBUGF(#id ": %s\n[%s]\n", h, s); \
162 void Registry_check(string r, string sv) \
166 string cl = REGISTRY_HASH(id); \
169 LOG_FATALF("client/server mismatch (%s).\nCL: %s\nSV: %s\n", r, cl, sv); \
173 void Registry_send_all() { Registry_send(#id, REGISTRY_HASH(id)); } \
175 #define REGISTER_REGISTRY(...) EVAL_REGISTER_REGISTRY(OVERLOAD(REGISTER_REGISTRY, __VA_ARGS__))
176 #define EVAL_REGISTER_REGISTRY(...) __VA_ARGS__
177 #define REGISTER_REGISTRY_1(id) REGISTER_REGISTRY_2(id, #id)
178 #define REGISTER_REGISTRY_2(id, str) \
179 ACCUMULATE_FUNCTION(__static_init, Register##id) \
180 CLASS(id##Registry, Object) \
181 ATTRIB(id##Registry, m_name, string, str) \
182 ATTRIB(id##Registry, REGISTRY_NEXT, entity, id##_first) \
183 ENDCLASS(id##Registry) \
184 REGISTER(Registries, REGISTRY, id, m_id, NEW(id##Registry));