]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/oo.qh
#pragma once
[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 (null_entity)
9 #else
10         #define NULL (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 sourceLocFile;
31 .int sourceLocLine;
32 entity _spawn();
33
34 #ifndef SPAWN_PURE
35 #define SPAWN_PURE 0
36 #endif
37
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 _sourceFile, int _sourceLine, bool pure)
45 {
46         entity this = pure ? spawn_pure() : _spawn();
47         this.classname = _classname;
48         this.sourceLocFile = _sourceFile;
49         this.sourceLocLine = _sourceLine;
50         if (pure) {
51                 make_pure(this);
52                 #ifdef CSQC
53                 setorigin(this, '0 0 10000');
54                 #endif
55         }
56         return this;
57 }
58
59
60 #define entityclass(...) EVAL_entityclass(OVERLOAD(entityclass, __VA_ARGS__))
61 #define EVAL_entityclass(...) __VA_ARGS__
62 #define entityclass_1(name) entityclass_2(name, Object)
63 #ifndef QCC_SUPPORT_ENTITYCLASS
64         #define entityclass_2(name, base) typedef entity name
65         #define class(name)
66         #define _new(class, pure) __spawn( #class, __FILE__, __LINE__, pure)
67 #else
68         #define entityclass_2(name, base) entityclass name : base {}
69         #define class(name) [[class(name)]]
70         #define _new(class, pure) ((class) __spawn( #class, __FILE__, __LINE__, pure))
71 #endif
72 /** entities you care about seeing (.origin works) */
73 #define new(class) _new(class, false)
74 /** purely logical entities (.origin doesn't work) */
75 #define new_pure(class) _new(class, true)
76 #define spawn() __spawn("entity", __FILE__, __LINE__, false)
77
78 entity _clearentity_ent;
79 STATIC_INIT(clearentity)
80 {
81         _clearentity_ent = new_pure(clearentity);
82         make_pure(_clearentity_ent);
83 }
84 void clearentity(entity e)
85 {
86 #ifdef CSQC
87                 int n = e.entnum;
88 #endif
89         bool was_pure = is_pure(e);
90         copyentity(_clearentity_ent, e);
91         if (!was_pure) make_impure(e);
92 #ifdef CSQC
93                 e.entnum = n;
94 #endif
95 }
96
97 // Classes have a `spawn##cname(entity)` constructor
98 // The parameter is used across [[accumulate]] functions
99
100 // Macros to hide this implementation detail:
101 #ifdef __STDC__
102         #define NEW_(cname, ...) \
103                 OVERLOAD_(spawn##cname, __VA_ARGS__)
104         #define NEW(cname, ...) \
105                 NEW_(cname, new_pure(cname),##__VA_ARGS__)(new_pure(cname),##__VA_ARGS__)
106
107         #define CONSTRUCT_(cname, ...) \
108                 OVERLOAD_(spawn##cname, __VA_ARGS__)
109         #define CONSTRUCT(cname, ...) \
110                 CONSTRUCT_(cname, this,##__VA_ARGS__)(this,##__VA_ARGS__)
111 #else
112         #define NEW(cname, ...) \
113                 OVERLOAD(spawn##cname, new_pure(cname),##__VA_ARGS__)
114
115         #define CONSTRUCT(cname, ...) \
116                 OVERLOAD(spawn##cname, this,##__VA_ARGS__)
117 #endif
118
119 #define CONSTRUCTOR(cname, ...) \
120         cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__) \
121         { \
122                 return = this; \
123         } \
124         [[accumulate]] cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__)
125
126 .string vtblname;
127 .entity vtblbase;
128
129 void RegisterClasses() {}
130 STATIC_INIT(RegisterClasses)
131 {
132         RegisterClasses();
133 }
134
135 #define VTBL(cname, base) \
136         INIT_STATIC(cname); \
137         entity cname##_vtbl; \
138         void cname##_vtbl_init() \
139         { \
140                 cname e = new_pure(vtbl); \
141                 spawn##cname##_static(e); \
142                 e.vtblname = #cname; \
143                 /* Top level objects refer to themselves */ \
144                 e.vtblbase = base##_vtbl ? base##_vtbl : e; \
145                 cname##_vtbl = e; \
146         } \
147         ACCUMULATE_FUNCTION(RegisterClasses, cname##_vtbl_init)
148
149 #define INIT_STATIC(cname) [[accumulate]] void spawn##cname##_static(cname this)
150 #define INIT(cname) [[accumulate]] cname spawn##cname##_1(cname this)
151
152 #define CLASS(cname, base)                  \
153         entityclass(cname, base);               \
154         class(cname).bool instanceOf##cname;   \
155         VTBL(cname, base)                       \
156         INIT_STATIC(cname) \
157         {                    \
158                 if (cname##_vtbl) \
159                 {                 \
160                         copyentity(cname##_vtbl, this); \
161                         return;                         \
162                 }                                   \
163                 spawn##base##_static(this);         \
164                 this.instanceOf##cname = true;      \
165         }                                       \
166         INIT(cname) \
167         {                           \
168                 /* Only statically initialize the current class, it contains everything it inherits */ \
169                 if (cname##_vtbl.vtblname == this.classname) \
170                 { \
171                         spawn##cname##_static(this);    \
172                         this.classname = #cname;        \
173                         this.vtblname = string_null;    \
174                         this.vtblbase = cname##_vtbl;   \
175                 }                                   \
176                 spawn##base##_1(this);              \
177         }
178
179 #define METHOD_REFERENCE(cname, name) \
180         cname##_##name
181
182 #define STATIC_METHOD(cname, name, prototype) \
183         prototype METHOD_REFERENCE(cname, name)
184
185 #define METHOD(cname, name, prototype) \
186         STATIC_METHOD(cname, name, prototype); \
187         class(cname) .prototype name; \
188         INIT_STATIC(cname) \
189         { \
190                 this.name = METHOD_REFERENCE(cname, name); \
191         } \
192         STATIC_METHOD(cname, name, prototype)
193
194 #define ATTRIB(cname, name, type, val)      \
195         class(cname).type name;                \
196         INIT(cname) \
197         { \
198                 noref bool strzone; /* Error on strzone() calls. */ \
199                 this.name = val; \
200         }
201
202 #define ATTRIB_STRZONE(cname, name, type, val)      \
203         class(cname).type name;                \
204         INIT(cname) \
205         { \
206                 if (this.name) \
207                         strunzone(this.name); \
208                 this.name = strzone(val); \
209         }
210
211 #define ATTRIBARRAY(cname, name, type, cnt) \
212         class(cname).type name[cnt];
213
214 #define ENDCLASS(cname) \
215         INIT(cname) \
216         { \
217                 return this; \
218         }
219
220 #define SUPER(cname) (cname##_vtbl.vtblbase)
221
222 #define spawn_static(this)
223 #define spawn_1(this)
224 #define _vtbl NULL
225 CLASS(Object, );
226         METHOD(Object, describe, string(entity this))
227         {
228                 string s = _("No description");
229                 if (cvar("developer"))
230                 {
231                         for (int i = 0, n = numentityfields(); i < n; ++i)
232                         {
233                                 string value = getentityfieldstring(i, this);
234                                 if (value != "") s = sprintf("%s\n%s = %s", s, entityfieldname(i), value);
235                         }
236                 }
237                 return s;
238         }
239         METHOD(Object, display, void(entity this, void(string name, string icon) returns))
240         {
241                 returns(sprintf("entity %i", this), "nopreview_map");
242         }
243 ENDCLASS(Object)
244 #undef spawn_static
245 #undef spawn_1
246 #undef _vtbl