]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/oo.qh
Merge branch 'master' into Mario/hagar_notfixed
[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 #ifdef MENUQC
8         #define NULL (0, null_entity)
9         #define world NULL
10 #else
11         #define NULL (0, world)
12 #endif
13
14 .vector origin;
15 .bool pure_data;
16 /** @deprecated use new_pure or NEW(class) */
17 #define make_pure(e) \
18         MACRO_BEGIN \
19         { \
20                 (e).pure_data = true; \
21         } MACRO_END
22 #define make_impure(e) \
23         MACRO_BEGIN \
24         { \
25                 (e).pure_data = false; \
26         } MACRO_END
27 #define is_pure(e) ((e).pure_data)
28
29 .string classname;
30 /** Location entity was spawned from in source */
31 .string sourceLoc;
32 entity _spawn();
33
34 #ifndef SPAWN_PURE
35 #define SPAWN_PURE 0
36 #endif
37
38 // pure entities: need no .origin
39 #if SPAWN_PURE
40 entity spawn_pure() = #600;
41 #else
42 #define spawn_pure() _spawn()
43 #endif
44
45 entity __spawn(string _classname, string _sourceLoc, bool pure)
46 {
47         entity this = pure ? spawn_pure() : _spawn();
48         this.classname = _classname;
49         this.sourceLoc = _sourceLoc;
50         if (pure) {
51                 make_pure(this);
52                 #ifdef CSQC
53                 setorigin(this, '0 0 10000');
54                 #endif
55                 #ifdef SVQC
56         setorigin(this, '0 0 -10000');
57         #endif
58         }
59         return this;
60 }
61
62
63 #define entityclass(...) EVAL_entityclass(OVERLOAD_(entityclass, __VA_ARGS__))
64 #define EVAL_entityclass(...) __VA_ARGS__
65 #define entityclass_1(name) entityclass_2(name, Object)
66 #ifndef QCC_SUPPORT_ENTITYCLASS
67         #define entityclass_2(name, base) USING(name, entity)
68         #define class(name)
69         #define _new(class, pure) __spawn( #class, __FILE__ ":" STR(__LINE__), pure)
70 #else
71         #define entityclass_2(name, base) entityclass name : base {}
72         #define class(name) [[class(name)]]
73         #define _new(class, pure) ((class) __spawn( #class, __FILE__ ":" STR(__LINE__), pure))
74 #endif
75 /** entities you care about seeing (.origin works) */
76 #define new(class) _new(class, false)
77 /** purely logical entities (.origin doesn't work) */
78 #define new_pure(class) _new(class, true)
79 #define spawn() __spawn("entity", __FILE__ ":" STR(__LINE__), false)
80
81 #define delete(this) MACRO_BEGIN { \
82     entity _this = (this); \
83     void(entity) _dtor = _this.dtor; \
84     if (_dtor) _dtor(_this); else remove(_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 #define CLASS(cname, base)                  \
175         entityclass(cname, base);               \
176         class(cname).bool instanceOf##cname;    \
177     bool is_##cname(entity e) { return e.instanceOf##cname; } \
178         VTBL(cname, base)                       \
179         _INIT_STATIC(cname)                     \
180         {                                       \
181                 if (cname##_vtbl && !this.transmute)\
182                 {                                   \
183                         copyentity(cname##_vtbl, this); \
184                         return;                         \
185                 }                                   \
186                 spawn##base##_static(this);         \
187                 this.instanceOf##cname = true;      \
188         }                                       \
189         INIT(cname)                             \
190         {                                       \
191                 /* Only statically initialize the current class, it contains everything it inherits */ \
192                 if (cname##_vtbl.vtblname == this.classname) \
193                 {                                   \
194                         spawn##cname##_static(this);    \
195                         this.transmute = false;         \
196                         this.classname = #cname;        \
197                         this.vtblname = string_null;    \
198                         this.vtblbase = cname##_vtbl;   \
199                 }                                   \
200                 spawn##base##_1(this);              \
201         }
202
203 #define METHOD_REFERENCE(cname, name) \
204         cname##_##name
205
206 #define STATIC_METHOD(cname, name, prototype) \
207         prototype METHOD_REFERENCE(cname, name)
208
209 #define METHOD(cname, name, prototype) \
210         STATIC_METHOD(cname, name, prototype); \
211         class(cname) .prototype name; \
212         _INIT_STATIC(cname) \
213         { \
214                 this.name = METHOD_REFERENCE(cname, name); \
215         } \
216         STATIC_METHOD(cname, name, prototype)
217
218 #define DESTRUCTOR(cname) \
219         STATIC_METHOD(cname, dtorimpl, void(cname this)); \
220     METHOD(cname, dtor, void(cname this)) \
221     { \
222         METHOD_REFERENCE(cname, dtorimpl)(this); \
223         this.instanceOf##cname = false; \
224         entity super = SUPER(cname); \
225         if (super != cname##_vtbl) super.dtor(this); \
226     } \
227         STATIC_METHOD(cname, dtorimpl, void(cname this))
228
229 #define ATTRIB(cname, name, type, val)      \
230         class(cname).type name;                \
231         INIT(cname) \
232         { \
233                 noref bool strzone; /* Error on strzone() calls. */ \
234                 this.name = val; \
235         }
236
237 #define STATIC_ATTRIB(cname, name, type, val) \
238         type cname##_##name; \
239         _INIT_STATIC(cname) \
240         { \
241                 noref bool strzone; /* Error on strzone() calls. */ \
242                 cname##_##name = val; \
243         }
244
245 // cleanup potentially zoned strings from base classes
246
247 #define ATTRIB_STRZONE(cname, name, type, val)      \
248         class(cname).type name;                \
249         INIT(cname) \
250         { \
251                 if (this.name) \
252                         strunzone(this.name); \
253                 this.name = strzone(val); \
254         }
255
256 #define STATIC_ATTRIB_STRZONE(cname, name, type, val) \
257         type cname##_##name; \
258         _INIT_STATIC(cname) \
259         { \
260         if (cname##_##name) \
261             strunzone(cname##_##name); \
262                 cname##_##name = val; \
263         }
264
265 #define ATTRIBARRAY(cname, name, type, cnt) \
266         class(cname).type name[cnt];
267
268 #define ENDCLASS(cname) \
269         INIT(cname) \
270         { \
271                 return this; \
272         }
273
274 #define SUPER(cname) (cname##_vtbl.vtblbase)
275
276 #define spawn_static(this)
277 #define spawn_1(this)
278 #define _vtbl NULL
279 CLASS(Object, );
280     DESTRUCTOR(Object) { remove(this); }
281     #define remove(this) delete(this)
282         METHOD(Object, describe, string(Object this))
283         {
284             TC(Object, this);
285                 string s = _("No description");
286                 if (cvar("developer"))
287                 {
288                         for (int i = 0, n = numentityfields(); i < n; ++i)
289                         {
290                                 string value = getentityfieldstring(i, this);
291                                 if (value != "") s = sprintf("%s\n%s = %s", s, entityfieldname(i), value);
292                         }
293                 }
294                 return s;
295         }
296         METHOD(Object, display, void(Object this, void(string name, string icon) returns))
297         {
298             TC(Object, this);
299                 returns(sprintf("entity %i", this), "nopreview_map");
300         }
301 ENDCLASS(Object)
302 #undef spawn_static
303 #undef spawn_1
304 #undef _vtbl