]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/oo.qh
Remove uses of WITHSELF
[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 #define delete(this) MACRO_BEGIN { \
75     entity _this = (this); \
76     void(entity) _dtor = _this.dtor; \
77     if (_dtor) _dtor(_this); else remove(_this); \
78     /* this = NULL; */  \
79 } MACRO_END
80
81 entity _clearentity_ent;
82 STATIC_INIT(clearentity)
83 {
84         _clearentity_ent = new_pure(clearentity);
85 }
86 void clearentity(entity e)
87 {
88 #ifdef CSQC
89                 int n = e.entnum;
90 #endif
91         bool was_pure = is_pure(e);
92         copyentity(_clearentity_ent, e);
93         if (!was_pure) make_impure(e);
94 #ifdef CSQC
95                 e.entnum = n;
96 #endif
97 }
98
99 // Classes have a `spawn##cname(entity)` constructor
100 // The parameter is used across [[accumulate]] functions
101
102 .bool transmute;
103
104 // Macros to hide this implementation detail:
105 #ifdef __STDC__
106         #define NEW(cname, ...) \
107                 OVERLOAD_(spawn##cname, new_pure(cname) P99_IF_EMPTY(__VA_ARGS__)()(, __VA_ARGS__))
108
109     #define _TRANSMUTE(cname, this, ...) \
110         OVERLOAD_(spawn##cname, this P99_IF_EMPTY(__VA_ARGS__)()(, __VA_ARGS__))
111
112         #define CONSTRUCT(cname, ...) \
113                 OVERLOAD_(spawn##cname, this P99_IF_EMPTY(__VA_ARGS__)()(, __VA_ARGS__))
114 #else
115         #define NEW(cname, ...) \
116                 OVERLOAD(spawn##cname, new_pure(cname),##__VA_ARGS__)
117
118     #define _TRANSMUTE(cname, this, ...) \
119         OVERLOAD(spawn##cname, this,##__VA_ARGS__)
120
121         #define CONSTRUCT(cname, ...) \
122                 OVERLOAD(spawn##cname, this,##__VA_ARGS__)
123 #endif
124
125 #define TRANSMUTE(cname, this, ...) MACRO_BEGIN \
126     entity _e = (this); \
127     if (_e.vtblbase != cname##_vtbl) { \
128         _e.transmute = true; \
129         _e.classname = #cname; \
130         _TRANSMUTE(cname, _e, __VA_ARGS__); \
131     } \
132     MACRO_END
133
134 #define CONSTRUCTOR(cname, ...) \
135         cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__) \
136         { \
137                 return = this; \
138         } \
139         [[accumulate]] cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__)
140
141 .string vtblname;
142 .entity vtblbase;
143
144 void RegisterClasses() {}
145 STATIC_INIT(RegisterClasses)
146 {
147         RegisterClasses();
148 }
149
150 #define VTBL(cname, base) \
151         _INIT_STATIC(cname); \
152         entity cname##_vtbl; \
153         void cname##_vtbl_init() \
154         { \
155                 cname e = new_pure(vtbl); \
156                 spawn##cname##_static(e); \
157                 e.vtblname = #cname; \
158                 /* Top level objects refer to themselves */ \
159                 e.vtblbase = base##_vtbl ? base##_vtbl : e; \
160                 cname##_vtbl = e; \
161         } \
162         ACCUMULATE_FUNCTION(RegisterClasses, cname##_vtbl_init)
163
164 #define _INIT_STATIC(cname) [[accumulate]] void spawn##cname##_static(cname this)
165 #define INIT(cname) [[accumulate]] cname spawn##cname##_1(cname this)
166
167 #define CLASS(cname, base)                  \
168         entityclass(cname, base);               \
169         class(cname).bool instanceOf##cname;    \
170     bool is_##cname(entity e) { return e.instanceOf##cname; } \
171     void isnt_##cname(entity e) { eprint(e); } \
172         VTBL(cname, base)                       \
173         _INIT_STATIC(cname)                     \
174         {                                       \
175                 if (cname##_vtbl && !this.transmute)\
176                 {                                   \
177                         copyentity(cname##_vtbl, this); \
178                         return;                         \
179                 }                                   \
180                 spawn##base##_static(this);         \
181                 this.instanceOf##cname = true;      \
182         }                                       \
183         INIT(cname)                             \
184         {                                       \
185                 /* Only statically initialize the current class, it contains everything it inherits */ \
186                 if (cname##_vtbl.vtblname == this.classname) \
187                 {                                   \
188                         spawn##cname##_static(this);    \
189                         this.transmute = false;         \
190                         this.classname = #cname;        \
191                         this.vtblname = string_null;    \
192                         this.vtblbase = cname##_vtbl;   \
193                 }                                   \
194                 spawn##base##_1(this);              \
195         }
196
197 #define METHOD_REFERENCE(cname, name) \
198         cname##_##name
199
200 #define STATIC_METHOD(cname, name, prototype) \
201         prototype METHOD_REFERENCE(cname, name)
202
203 #define METHOD(cname, name, prototype) \
204         STATIC_METHOD(cname, name, prototype); \
205         class(cname) .prototype name; \
206         _INIT_STATIC(cname) \
207         { \
208                 this.name = METHOD_REFERENCE(cname, name); \
209         } \
210         STATIC_METHOD(cname, name, prototype)
211
212 #define DESTRUCTOR(cname) \
213         STATIC_METHOD(cname, dtorimpl, void(cname this)); \
214     METHOD(cname, dtor, void(cname this)) \
215     { \
216         METHOD_REFERENCE(cname, dtorimpl)(this); \
217         this.instanceOf##cname = false; \
218         entity super = SUPER(cname); \
219         if (super != cname##_vtbl) super.dtor(this); \
220     } \
221         STATIC_METHOD(cname, dtorimpl, void(cname this))
222
223 #define ATTRIB(cname, name, type, val)      \
224         class(cname).type name;                \
225         INIT(cname) \
226         { \
227                 noref bool strzone; /* Error on strzone() calls. */ \
228                 this.name = val; \
229         }
230
231 #define STATIC_ATTRIB(cname, name, type, val) \
232         type cname##_##name; \
233         _INIT_STATIC(cname) \
234         { \
235                 noref bool strzone; /* Error on strzone() calls. */ \
236                 cname##_##name = val; \
237         }
238
239 // cleanup potentially zoned strings from base classes
240
241 #define ATTRIB_STRZONE(cname, name, type, val)      \
242         class(cname).type name;                \
243         INIT(cname) \
244         { \
245                 if (this.name) \
246                         strunzone(this.name); \
247                 this.name = strzone(val); \
248         }
249
250 #define STATIC_ATTRIB_STRZONE(cname, name, type, val) \
251         type cname##_##name; \
252         _INIT_STATIC(cname) \
253         { \
254         if (cname##_##name) \
255             strunzone(cname##_##name); \
256                 cname##_##name = val; \
257         }
258
259 #define ATTRIBARRAY(cname, name, type, cnt) \
260         class(cname).type name[cnt];
261
262 #define ENDCLASS(cname) \
263         INIT(cname) \
264         { \
265                 return this; \
266         }
267
268 #define SUPER(cname) (cname##_vtbl.vtblbase)
269
270 #define spawn_static(this)
271 #define spawn_1(this)
272 #define _vtbl NULL
273 CLASS(Object, );
274     DESTRUCTOR(Object) { remove(this); }
275     #define remove(this) delete(this)
276         METHOD(Object, describe, string(Object this))
277         {
278             TC(Object, this);
279                 string s = _("No description");
280                 if (cvar("developer"))
281                 {
282                         for (int i = 0, n = numentityfields(); i < n; ++i)
283                         {
284                                 string value = getentityfieldstring(i, this);
285                                 if (value != "") s = sprintf("%s\n%s = %s", s, entityfieldname(i), value);
286                         }
287                 }
288                 return s;
289         }
290         METHOD(Object, display, void(Object this, void(string name, string icon) returns))
291         {
292             TC(Object, this);
293                 returns(sprintf("entity %i", this), "nopreview_map");
294         }
295 ENDCLASS(Object)
296 #undef spawn_static
297 #undef spawn_1
298 #undef _vtbl