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; \
15 #define make_impure(e) MACRO_BEGIN \
16 (e).pure_data = false; \
20 /** Location entity was spawned from in source */
28 // pure entities: need no .origin
30 entity spawn_pure() = #600;
32 #define spawn_pure() _spawn()
35 entity __spawn(string _classname, string _sourceLoc, bool pure)
37 entity this = pure ? spawn_pure() : _spawn();
38 this.classname = _classname;
39 this.sourceLoc = _sourceLoc;
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)
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))
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)
65 ACCUMULATE void ONREMOVE(entity this) {}
68 #define delete_fn builtin_remove
71 .void(entity this) dtor;
72 #define delete(this) MACRO_BEGIN \
73 entity _this = (this); \
74 void(entity) _dtor = _this.dtor; \
76 if (_dtor) _dtor(_this); else delete_fn(_this); \
80 void IL_REMOVE_RAW(entity it);
81 void copyentity_qc(entity src, entity dst)
83 copyentity(src, dst); // builtin function
87 entity _clearentity_ent;
88 STATIC_INIT(clearentity)
90 _clearentity_ent = new_pure(clearentity);
92 void clearentity(entity e)
97 bool was_pure = is_pure(e);
98 copyentity_qc(_clearentity_ent, e);
99 if (!was_pure) make_impure(e);
105 // Classes have a `spawn##cname(entity)` constructor
106 // The parameter is used across ACCUMULATE functions
110 // Macros to hide this implementation detail:
112 #define NEW(cname, ...) \
113 OVERLOAD_(spawn##cname, new_pure(cname) P99_IF_EMPTY(__VA_ARGS__)()(, __VA_ARGS__))
115 #define _TRANSMUTE(cname, this, ...) \
116 OVERLOAD_(spawn##cname, this P99_IF_EMPTY(__VA_ARGS__)()(, __VA_ARGS__))
118 #define CONSTRUCT(cname, ...) \
119 OVERLOAD_(spawn##cname, this P99_IF_EMPTY(__VA_ARGS__)()(, __VA_ARGS__))
121 #define NEW(cname, ...) \
122 OVERLOAD(spawn##cname, new_pure(cname),##__VA_ARGS__)
124 #define _TRANSMUTE(cname, this, ...) \
125 OVERLOAD(spawn##cname, this,##__VA_ARGS__)
127 #define CONSTRUCT(cname, ...) \
128 OVERLOAD(spawn##cname, this,##__VA_ARGS__)
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__); \
140 #define CLASS(...) EVAL_CLASS(OVERLOAD__(CLASS, __VA_ARGS__))
141 #define EVAL_CLASS(...) __VA_ARGS__
143 #define ATTRIB(...) EVAL_ATTRIB(OVERLOAD_(ATTRIB, __VA_ARGS__))
144 #define EVAL_ATTRIB(...) __VA_ARGS__
146 #ifdef QCC_SUPPORT_CLASS
148 #warning "QCC_SUPPORT_CLASS not implemented"
150 #define CLASS_1(name) CLASS_2(name, entity)
151 #define CLASS_2(name, base) class name : base {
153 #define INIT(class) void class::class()
154 #define CONSTRUCTOR(class, ...) void class::class(__VA_ARGS__)
155 #define DESTRUCTOR(class) class::~class()
157 #define SUPER(class) super
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
163 #define ATTRIB_STRZONE(class, name, T, val) T name = val
164 #define STATIC_ATTRIB_STRZONE(class, name, T, val) static T name = val
166 #define ATTRIBARRAY(class, name, T, val) T name[val]
168 #define METHOD(class, name, prototype) virtual void class::name()
169 #define STATIC_METHOD(class, name, prototype) static void class::name()
171 #define ENDCLASS(class) };
175 #define CLASS_1(cname) CLASS_2(cname, )
176 #define CLASS_2(cname, base) \
177 entityclass(cname, base); \
178 classfield(cname).bool instanceOf##cname; \
181 _INIT_STATIC(cname) \
183 if (cname##_vtbl && !this.transmute) \
185 copyentity_qc(cname##_vtbl, this); \
188 spawn##base##_static(this); \
189 this.instanceOf##cname = true; \
193 /* Only statically initialize the current class, it contains everything it inherits */ \
194 if (cname##_vtbl.vtblname == this.classname) \
196 spawn##cname##_static(this); \
197 this.transmute = false; \
198 this.classname = #cname; \
199 this.vtblname = string_null; \
200 this.vtblbase = cname##_vtbl; \
202 spawn##base##_1(this); \
205 #define INIT(cname) \
206 ACCUMULATE cname spawn##cname##_1(cname this)
208 #define CONSTRUCTOR(cname, ...) \
209 cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__) \
213 ACCUMULATE cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__)
215 #define DESTRUCTOR(cname) \
216 STATIC_METHOD(cname, dtorimpl, void(cname this)); \
217 METHOD(cname, dtor, void(cname this)) \
219 METHOD_REFERENCE(cname, dtorimpl)(this); \
220 this.instanceOf##cname = false; \
221 entity super = SUPER(cname); \
222 if (super != cname##_vtbl) super.dtor(this); \
224 STATIC_METHOD(cname, dtorimpl, void(cname this))
226 #define SUPER(cname) (cname##_vtbl.vtblbase)
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); \
233 noref bool strzone; /* Error on strzone() calls. */ \
236 ATTRIB_3(cname, name, type)
238 #define STATIC_ATTRIB(cname, name, type, val) \
239 type cname##_##name; \
240 _INIT_STATIC(cname) \
242 noref bool strzone; /* Error on strzone() calls. */ \
243 cname##_##name = val; \
246 // cleanup potentially zoned strings from base classes
247 #define ATTRIB_STRZONE(cname, name, type, val) \
248 classfield(cname).type name; \
251 strcpy(this.name, val); \
254 #define STATIC_ATTRIB_STRZONE(cname, name, type, val) \
255 type cname##_##name; \
256 _INIT_STATIC(cname) \
258 strcpy(cname##_##name, val); \
261 #define ATTRIBARRAY(cname, name, type, cnt) \
262 classfield(cname) .type name[cnt]
264 #define METHOD(cname, name, prototype) \
265 STATIC_METHOD(cname, name, prototype); \
266 classfield(cname) .prototype name; \
267 _INIT_STATIC(cname) \
269 this.name = METHOD_REFERENCE(cname, name); \
271 STATIC_METHOD(cname, name, prototype)
273 #define STATIC_METHOD(cname, name, prototype) \
274 prototype METHOD_REFERENCE(cname, name)
276 #define ENDCLASS(cname) \
287 void RegisterClasses() {}
288 STATIC_INIT(RegisterClasses)
293 #define VTBL(cname, base) \
294 _INIT_STATIC(cname); \
295 entity cname##_vtbl; \
296 void cname##_vtbl_init() \
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; \
305 ACCUMULATE_FUNCTION(RegisterClasses, cname##_vtbl_init)
307 #define _INIT_STATIC(cname) ACCUMULATE void spawn##cname##_static(cname this)
310 #define DEBUG_STUFF(cname)
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); }
317 #define METHOD_REFERENCE(cname, name) \
322 #define spawn_static(this)
323 #define spawn_1(this)
326 DESTRUCTOR(Object) { builtin_remove(this); }
327 #define remove(this) delete(this)
328 METHOD(Object, describe, string(Object this))
331 string s = _("No description");
332 if (cvar("developer") > 0)
334 for (int i = 0, n = numentityfields(); i < n; ++i)
336 string value = getentityfieldstring(i, this);
337 if (value != "") s = sprintf("%s\n%s = %s", s, entityfieldname(i), value);
342 METHOD(Object, display, void(Object this, void(string name, string icon) returns))
345 returns(sprintf("entity %i", this), "nopreview_map");