]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/oo.qh
Merge branch 'master' into Lyberta/StandaloneOverkillWeapons
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / oo.qh
1 #pragma once
2
3 #include "misc.qh"
4 #include "nil.qh"
5 #include "static.qh"
6
7 .vector origin;
8 .bool pure_data;
9 /** @deprecated use new_pure or NEW(class) */
10 #define make_pure(e) \
11         MACRO_BEGIN \
12         { \
13                 (e).pure_data = true; \
14         } MACRO_END
15 #define make_impure(e) \
16         MACRO_BEGIN \
17         { \
18                 (e).pure_data = false; \
19         } MACRO_END
20 #define is_pure(e) ((e).pure_data)
21
22 .string classname;
23 /** Location entity was spawned from in source */
24 .string sourceLoc;
25 entity _spawn();
26
27 #ifndef SPAWN_PURE
28 #define SPAWN_PURE 0
29 #endif
30
31 // pure entities: need no .origin
32 #if SPAWN_PURE
33 entity spawn_pure() = #600;
34 #else
35 #define spawn_pure() _spawn()
36 #endif
37
38 entity __spawn(string _classname, string _sourceLoc, bool pure)
39 {
40         entity this = pure ? spawn_pure() : _spawn();
41         this.classname = _classname;
42         this.sourceLoc = _sourceLoc;
43         if (pure) {
44                 make_pure(this);
45                 #ifdef CSQC
46                 setorigin(this, (world.mins + world.maxs) * 0.5);
47                 #endif
48                 #ifdef SVQC
49         setorigin(this, (world.mins + world.maxs) * 0.5);
50         #endif
51         }
52         return this;
53 }
54
55
56 #define entityclass(...) EVAL_entityclass(OVERLOAD_(entityclass, __VA_ARGS__))
57 #define EVAL_entityclass(...) __VA_ARGS__
58 #define entityclass_1(name) entityclass_2(name, Object)
59 #ifndef QCC_SUPPORT_ENTITYCLASS
60         #define entityclass_2(name, base) USING(name, entity)
61         #define class(name)
62         #define _new(class, pure) __spawn( #class, __FILE__ ":" STR(__LINE__), pure)
63 #else
64         #define entityclass_2(name, base) entityclass name : base {}
65         #define class(name) [[class(name)]]
66         #define _new(class, pure) ((class) __spawn( #class, __FILE__ ":" STR(__LINE__), pure))
67 #endif
68 /** entities you care about seeing (.origin works) */
69 #define new(class) _new(class, false)
70 /** purely logical entities (.origin doesn't work) */
71 #define new_pure(class) _new(class, true)
72 #define spawn() __spawn("entity", __FILE__ ":" STR(__LINE__), false)
73
74 [[accumulate]] void ONREMOVE(entity this) {}
75
76 #ifndef SVQC
77         #define delete_fn builtin_remove
78 #endif
79
80 #define delete(this) MACRO_BEGIN { \
81     entity _this = (this); \
82     void(entity) _dtor = _this.dtor; \
83     ONREMOVE(this); \
84     if (_dtor) _dtor(_this); else delete_fn(_this); \
85     /* this = NULL; */  \
86 } MACRO_END
87
88 entity _clearentity_ent;
89 STATIC_INIT(clearentity)
90 {
91         _clearentity_ent = new_pure(clearentity);
92 }
93 void clearentity(entity e)
94 {
95 #ifdef CSQC
96                 int n = e.entnum;
97 #endif
98         bool was_pure = is_pure(e);
99         copyentity(_clearentity_ent, e);
100         if (!was_pure) make_impure(e);
101 #ifdef CSQC
102                 e.entnum = n;
103 #endif
104 }
105
106 // Classes have a `spawn##cname(entity)` constructor
107 // The parameter is used across [[accumulate]] functions
108
109 .bool transmute;
110
111 // Macros to hide this implementation detail:
112 #ifdef __STDC__
113         #define NEW(cname, ...) \
114                 OVERLOAD_(spawn##cname, new_pure(cname) P99_IF_EMPTY(__VA_ARGS__)()(, __VA_ARGS__))
115
116     #define _TRANSMUTE(cname, this, ...) \
117         OVERLOAD_(spawn##cname, this P99_IF_EMPTY(__VA_ARGS__)()(, __VA_ARGS__))
118
119         #define CONSTRUCT(cname, ...) \
120                 OVERLOAD_(spawn##cname, this P99_IF_EMPTY(__VA_ARGS__)()(, __VA_ARGS__))
121 #else
122         #define NEW(cname, ...) \
123                 OVERLOAD(spawn##cname, new_pure(cname),##__VA_ARGS__)
124
125     #define _TRANSMUTE(cname, this, ...) \
126         OVERLOAD(spawn##cname, this,##__VA_ARGS__)
127
128         #define CONSTRUCT(cname, ...) \
129                 OVERLOAD(spawn##cname, this,##__VA_ARGS__)
130 #endif
131
132 #define TRANSMUTE(cname, this, ...) MACRO_BEGIN \
133     entity _e = (this); \
134     if (_e.vtblbase != cname##_vtbl) { \
135         _e.transmute = true; \
136         _e.classname = #cname; \
137         _TRANSMUTE(cname, _e, __VA_ARGS__); \
138     } \
139     MACRO_END
140
141 #define CONSTRUCTOR(cname, ...) \
142         cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__) \
143         { \
144                 return = this; \
145         } \
146         [[accumulate]] cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__)
147
148 .string vtblname;
149 .entity vtblbase;
150
151 void RegisterClasses() {}
152 STATIC_INIT(RegisterClasses)
153 {
154         RegisterClasses();
155 }
156
157 #define VTBL(cname, base) \
158         _INIT_STATIC(cname); \
159         entity cname##_vtbl; \
160         void cname##_vtbl_init() \
161         { \
162                 cname e = new_pure(vtbl); \
163                 spawn##cname##_static(e); \
164                 e.vtblname = #cname; \
165                 /* Top level objects refer to themselves */ \
166                 e.vtblbase = base##_vtbl ? base##_vtbl : e; \
167                 cname##_vtbl = e; \
168         } \
169         ACCUMULATE_FUNCTION(RegisterClasses, cname##_vtbl_init)
170
171 #define _INIT_STATIC(cname) [[accumulate]] void spawn##cname##_static(cname this)
172 #define INIT(cname) [[accumulate]] cname spawn##cname##_1(cname this)
173
174 #if NDEBUG
175         #define DEBUG_STUFF(cname)
176 #else
177         #define DEBUG_STUFF(cname) \
178                 bool is_##cname(entity e) { return e.instanceOf##cname; } \
179                 void isnt_##cname(entity e) { eprint(e); }
180 #endif
181
182
183 #define CLASS(cname, base)                  \
184         entityclass(cname, base);               \
185         class(cname).bool instanceOf##cname;    \
186         DEBUG_STUFF(cname)                      \
187         VTBL(cname, base)                       \
188         _INIT_STATIC(cname)                     \
189         {                                       \
190                 if (cname##_vtbl && !this.transmute)\
191                 {                                   \
192                         copyentity(cname##_vtbl, this); \
193                         return;                         \
194                 }                                   \
195                 spawn##base##_static(this);         \
196                 this.instanceOf##cname = true;      \
197         }                                       \
198         INIT(cname)                             \
199         {                                       \
200                 /* Only statically initialize the current class, it contains everything it inherits */ \
201                 if (cname##_vtbl.vtblname == this.classname) \
202                 {                                   \
203                         spawn##cname##_static(this);    \
204                         this.transmute = false;         \
205                         this.classname = #cname;        \
206                         this.vtblname = string_null;    \
207                         this.vtblbase = cname##_vtbl;   \
208                 }                                   \
209                 spawn##base##_1(this);              \
210         }
211
212 #define METHOD_REFERENCE(cname, name) \
213         cname##_##name
214
215 #define STATIC_METHOD(cname, name, prototype) \
216         prototype METHOD_REFERENCE(cname, name)
217
218 #define METHOD(cname, name, prototype) \
219         STATIC_METHOD(cname, name, prototype); \
220         class(cname) .prototype name; \
221         _INIT_STATIC(cname) \
222         { \
223                 this.name = METHOD_REFERENCE(cname, name); \
224         } \
225         STATIC_METHOD(cname, name, prototype)
226
227 #define DESTRUCTOR(cname) \
228         STATIC_METHOD(cname, dtorimpl, void(cname this)); \
229     METHOD(cname, dtor, void(cname this)) \
230     { \
231         METHOD_REFERENCE(cname, dtorimpl)(this); \
232         this.instanceOf##cname = false; \
233         entity super = SUPER(cname); \
234         if (super != cname##_vtbl) super.dtor(this); \
235     } \
236         STATIC_METHOD(cname, dtorimpl, void(cname this))
237
238 #define ATTRIB(...) EVAL_ATTRIB(OVERLOAD_(ATTRIB, __VA_ARGS__))
239 #define EVAL_ATTRIB(...) __VA_ARGS__
240 #define ATTRIB_3(cname, name, type) INIT(cname) {} class(cname) .type name
241 #define ATTRIB_4(cname, name, type, val) \
242         ATTRIB_3(cname, name, type); \
243         INIT(cname) \
244         { \
245                 noref bool strzone; /* Error on strzone() calls. */ \
246                 this.name = val; \
247         } \
248         ATTRIB_3(cname, name, type)
249
250 #define STATIC_ATTRIB(cname, name, type, val) \
251         type cname##_##name; \
252         _INIT_STATIC(cname) \
253         { \
254                 noref bool strzone; /* Error on strzone() calls. */ \
255                 cname##_##name = val; \
256         }
257
258 // cleanup potentially zoned strings from base classes
259
260 #define ATTRIB_STRZONE(cname, name, type, val)      \
261         class(cname).type name;                \
262         INIT(cname) \
263         { \
264                 if (this.name) \
265                         strunzone(this.name); \
266                 this.name = strzone(val); \
267         }
268
269 #define STATIC_ATTRIB_STRZONE(cname, name, type, val) \
270         type cname##_##name; \
271         _INIT_STATIC(cname) \
272         { \
273         if (cname##_##name) \
274             strunzone(cname##_##name); \
275                 cname##_##name = val; \
276         }
277
278 #define ATTRIBARRAY(cname, name, type, cnt) \
279         class(cname) .type name[cnt]
280
281 #define ENDCLASS(cname) \
282         INIT(cname) \
283         { \
284                 return this; \
285         }
286
287 #define SUPER(cname) (cname##_vtbl.vtblbase)
288
289 #define spawn_static(this)
290 #define spawn_1(this)
291 #define _vtbl NULL
292 CLASS(Object, );
293     DESTRUCTOR(Object) { builtin_remove(this); }
294     #define remove(this) delete(this)
295         METHOD(Object, describe, string(Object this))
296         {
297             TC(Object, this);
298                 string s = _("No description");
299                 if (cvar("developer"))
300                 {
301                         for (int i = 0, n = numentityfields(); i < n; ++i)
302                         {
303                                 string value = getentityfieldstring(i, this);
304                                 if (value != "") s = sprintf("%s\n%s = %s", s, entityfieldname(i), value);
305                         }
306                 }
307                 return s;
308         }
309         METHOD(Object, display, void(Object this, void(string name, string icon) returns))
310         {
311             TC(Object, this);
312                 returns(sprintf("entity %i", this), "nopreview_map");
313         }
314 ENDCLASS(Object)
315 #undef spawn_static
316 #undef spawn_1
317 #undef _vtbl