]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/oo.qh
Doxygen: improve static members and zoned strings
[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 #else
10         #define NULL (0, world)
11 #endif
12
13 .vector origin;
14 .bool pure_data;
15 /** @deprecated use new_pure or NEW(class) */
16 #define make_pure(e) \
17         MACRO_BEGIN \
18         { \
19                 (e).pure_data = true; \
20         } MACRO_END
21 #define make_impure(e) \
22         MACRO_BEGIN \
23         { \
24                 (e).pure_data = false; \
25         } MACRO_END
26 #define is_pure(e) ((e).pure_data)
27
28 .string classname;
29 /** Location entity was spawned from in source */
30 .string sourceLoc;
31 entity _spawn();
32
33 #ifndef SPAWN_PURE
34 #define SPAWN_PURE 0
35 #endif
36
37 // pure entities: need no .origin
38 #if SPAWN_PURE
39 entity spawn_pure() = #600;
40 #else
41 #define spawn_pure() _spawn()
42 #endif
43
44 entity __spawn(string _classname, string _sourceLoc, bool pure)
45 {
46         entity this = pure ? spawn_pure() : _spawn();
47         this.classname = _classname;
48         this.sourceLoc = _sourceLoc;
49         if (pure) {
50                 make_pure(this);
51                 #ifdef CSQC
52                 setorigin(this, '0 0 10000');
53                 #endif
54                 #ifdef SVQC
55         setorigin(this, '0 0 -10000');
56         #endif
57         }
58         return this;
59 }
60
61
62 #define entityclass(...) EVAL_entityclass(OVERLOAD_(entityclass, __VA_ARGS__))
63 #define EVAL_entityclass(...) __VA_ARGS__
64 #define entityclass_1(name) entityclass_2(name, Object)
65 #ifndef QCC_SUPPORT_ENTITYCLASS
66         #define entityclass_2(name, base) USING(name, entity)
67         #define class(name)
68         #define _new(class, pure) __spawn( #class, __FILE__ ":" STR(__LINE__), pure)
69 #else
70         #define entityclass_2(name, base) entityclass name : base {}
71         #define class(name) [[class(name)]]
72         #define _new(class, pure) ((class) __spawn( #class, __FILE__ ":" STR(__LINE__), pure))
73 #endif
74 /** entities you care about seeing (.origin works) */
75 #define new(class) _new(class, false)
76 /** purely logical entities (.origin doesn't work) */
77 #define new_pure(class) _new(class, true)
78 #define spawn() __spawn("entity", __FILE__ ":" STR(__LINE__), false)
79
80 #define delete(this) MACRO_BEGIN { \
81     entity _this = (this); \
82     void(entity) _dtor = _this.dtor; \
83     if (_dtor) _dtor(_this); else remove(_this); \
84     /* this = NULL; */  \
85 } MACRO_END
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(_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 // Macros to hide this implementation detail:
109 #ifdef __STDC__
110         #define NEW(cname, ...) \
111                 OVERLOAD_(spawn##cname, new_pure(cname) P99_IF_EMPTY(__VA_ARGS__)()(, __VA_ARGS__))
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 CONSTRUCT(cname, ...) \
119                 OVERLOAD(spawn##cname, this,##__VA_ARGS__)
120 #endif
121
122 #define CONSTRUCTOR(cname, ...) \
123         cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__) \
124         { \
125                 return = this; \
126         } \
127         [[accumulate]] cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__)
128
129 .string vtblname;
130 .entity vtblbase;
131
132 void RegisterClasses() {}
133 STATIC_INIT(RegisterClasses)
134 {
135         RegisterClasses();
136 }
137
138 #define VTBL(cname, base) \
139         _INIT_STATIC(cname); \
140         entity cname##_vtbl; \
141         void cname##_vtbl_init() \
142         { \
143                 cname e = new_pure(vtbl); \
144                 spawn##cname##_static(e); \
145                 e.vtblname = #cname; \
146                 /* Top level objects refer to themselves */ \
147                 e.vtblbase = base##_vtbl ? base##_vtbl : e; \
148                 cname##_vtbl = e; \
149         } \
150         ACCUMULATE_FUNCTION(RegisterClasses, cname##_vtbl_init)
151
152 #define _INIT_STATIC(cname) [[accumulate]] void spawn##cname##_static(cname this)
153 #define INIT(cname) [[accumulate]] cname spawn##cname##_1(cname this)
154
155 #define CLASS(cname, base)                  \
156         entityclass(cname, base);               \
157         class(cname).bool instanceOf##cname;   \
158     bool is_##cname(entity e) { return e.instanceOf##cname; } \
159         VTBL(cname, base)                       \
160         _INIT_STATIC(cname) \
161         {                    \
162                 if (cname##_vtbl) \
163                 {                 \
164                         copyentity(cname##_vtbl, this); \
165                         return;                         \
166                 }                                   \
167                 spawn##base##_static(this);         \
168                 this.instanceOf##cname = true;      \
169         }                                       \
170         INIT(cname) \
171         {                           \
172                 /* Only statically initialize the current class, it contains everything it inherits */ \
173                 if (cname##_vtbl.vtblname == this.classname) \
174                 { \
175                         spawn##cname##_static(this);    \
176                         this.classname = #cname;        \
177                         this.vtblname = string_null;    \
178                         this.vtblbase = cname##_vtbl;   \
179                 }                                   \
180                 spawn##base##_1(this);              \
181         }
182
183 #define METHOD_REFERENCE(cname, name) \
184         cname##_##name
185
186 #define STATIC_METHOD(cname, name, prototype) \
187         prototype METHOD_REFERENCE(cname, name)
188
189 #define METHOD(cname, name, prototype) \
190         STATIC_METHOD(cname, name, prototype); \
191         class(cname) .prototype name; \
192         _INIT_STATIC(cname) \
193         { \
194                 this.name = METHOD_REFERENCE(cname, name); \
195         } \
196         STATIC_METHOD(cname, name, prototype)
197
198 #define DESTRUCTOR(cname) \
199         STATIC_METHOD(cname, dtorimpl, void(cname this)); \
200     METHOD(cname, dtor, void(cname this)) \
201     { \
202         METHOD_REFERENCE(cname, dtorimpl)(this); \
203         entity super = SUPER(cname); \
204         if (super != cname##_vtbl) super.dtor(this); \
205     } \
206         STATIC_METHOD(cname, dtorimpl, void(cname this))
207
208 #define ATTRIB(cname, name, type, val)      \
209         class(cname).type name;                \
210         INIT(cname) \
211         { \
212                 noref bool strzone; /* Error on strzone() calls. */ \
213                 this.name = val; \
214         }
215
216 #define STATIC_ATTRIB(cname, name, type, val) \
217         type cname##_##name; \
218         _INIT_STATIC(cname) \
219         { \
220                 noref bool strzone; /* Error on strzone() calls. */ \
221                 cname##_##name = val; \
222         }
223
224 // cleanup potentially zoned strings from base classes
225
226 #define ATTRIB_STRZONE(cname, name, type, val)      \
227         class(cname).type name;                \
228         INIT(cname) \
229         { \
230                 if (this.name) \
231                         strunzone(this.name); \
232                 this.name = strzone(val); \
233         }
234
235 #define STATIC_ATTRIB_STRZONE(cname, name, type, val) \
236         type cname##_##name; \
237         _INIT_STATIC(cname) \
238         { \
239         if (cname##_##name) \
240             strunzone(cname##_##name); \
241                 cname##_##name = val; \
242         }
243
244 #define ATTRIBARRAY(cname, name, type, cnt) \
245         class(cname).type name[cnt];
246
247 #define ENDCLASS(cname) \
248         INIT(cname) \
249         { \
250                 return this; \
251         }
252
253 #define SUPER(cname) (cname##_vtbl.vtblbase)
254
255 #define spawn_static(this)
256 #define spawn_1(this)
257 #define _vtbl NULL
258 CLASS(Object, );
259     DESTRUCTOR(Object) { remove(this); }
260     #define remove(this) delete(this)
261         METHOD(Object, describe, string(Object this))
262         {
263             TC(Object, this);
264                 string s = _("No description");
265                 if (cvar("developer"))
266                 {
267                         for (int i = 0, n = numentityfields(); i < n; ++i)
268                         {
269                                 string value = getentityfieldstring(i, this);
270                                 if (value != "") s = sprintf("%s\n%s = %s", s, entityfieldname(i), value);
271                         }
272                 }
273                 return s;
274         }
275         METHOD(Object, display, void(Object this, void(string name, string icon) returns))
276         {
277             TC(Object, this);
278                 returns(sprintf("entity %i", this), "nopreview_map");
279         }
280 ENDCLASS(Object)
281 #undef spawn_static
282 #undef spawn_1
283 #undef _vtbl