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