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