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 entity _clearentity_ent;
81 STATIC_INIT(clearentity)
83 _clearentity_ent = new_pure(clearentity);
85 void clearentity(entity e)
90 bool was_pure = is_pure(e);
91 copyentity(_clearentity_ent, e);
92 if (!was_pure) make_impure(e);
98 // Classes have a `spawn##cname(entity)` constructor
99 // The parameter is used across ACCUMULATE functions
103 // Macros to hide this implementation detail:
105 #define NEW(cname, ...) \
106 OVERLOAD_(spawn##cname, new_pure(cname) P99_IF_EMPTY(__VA_ARGS__)()(, __VA_ARGS__))
108 #define _TRANSMUTE(cname, this, ...) \
109 OVERLOAD_(spawn##cname, this P99_IF_EMPTY(__VA_ARGS__)()(, __VA_ARGS__))
111 #define CONSTRUCT(cname, ...) \
112 OVERLOAD_(spawn##cname, this P99_IF_EMPTY(__VA_ARGS__)()(, __VA_ARGS__))
114 #define NEW(cname, ...) \
115 OVERLOAD(spawn##cname, new_pure(cname),##__VA_ARGS__)
117 #define _TRANSMUTE(cname, this, ...) \
118 OVERLOAD(spawn##cname, this,##__VA_ARGS__)
120 #define CONSTRUCT(cname, ...) \
121 OVERLOAD(spawn##cname, this,##__VA_ARGS__)
124 #define TRANSMUTE(cname, this, ...) MACRO_BEGIN \
125 entity _e = (this); \
126 if (_e.vtblbase != cname##_vtbl) { \
127 _e.transmute = true; \
128 _e.classname = #cname; \
129 _TRANSMUTE(cname, _e, __VA_ARGS__); \
133 #define CLASS(...) EVAL_CLASS(OVERLOAD__(CLASS, __VA_ARGS__))
134 #define EVAL_CLASS(...) __VA_ARGS__
136 #define ATTRIB(...) EVAL_ATTRIB(OVERLOAD_(ATTRIB, __VA_ARGS__))
137 #define EVAL_ATTRIB(...) __VA_ARGS__
139 #ifdef QCC_SUPPORT_CLASS
141 #warning "QCC_SUPPORT_CLASS not implemented"
143 #define CLASS_1(name) CLASS_2(name, entity)
144 #define CLASS_2(name, base) class name : base {
146 #define INIT(class) void class::class()
147 #define CONSTRUCTOR(class, ...) void class::class(__VA_ARGS__)
148 #define DESTRUCTOR(class) class::~class()
150 #define SUPER(class) super
152 #define ATTRIB_3(class, name, T) T name
153 #define ATTRIB_4(class, name, T, val) ATTRIB_3(class, name, T) = val
154 #define STATIC_ATTRIB(class, name, T, val) static T name = val
156 #define ATTRIB_STRZONE(class, name, T, val) T name = val
157 #define STATIC_ATTRIB_STRZONE(class, name, T, val) static T name = val
159 #define ATTRIBARRAY(class, name, T, val) T name[val]
161 #define METHOD(class, name, prototype) virtual void class::name()
162 #define STATIC_METHOD(class, name, prototype) static void class::name()
164 #define ENDCLASS(class) };
168 #define CLASS_1(cname) CLASS_2(cname, )
169 #define CLASS_2(cname, base) \
170 entityclass(cname, base); \
171 classfield(cname).bool instanceOf##cname; \
174 _INIT_STATIC(cname) \
176 if (cname##_vtbl && !this.transmute) \
178 copyentity(cname##_vtbl, this); \
181 spawn##base##_static(this); \
182 this.instanceOf##cname = true; \
186 /* Only statically initialize the current class, it contains everything it inherits */ \
187 if (cname##_vtbl.vtblname == this.classname) \
189 spawn##cname##_static(this); \
190 this.transmute = false; \
191 this.classname = #cname; \
192 this.vtblname = string_null; \
193 this.vtblbase = cname##_vtbl; \
195 spawn##base##_1(this); \
198 #define INIT(cname) \
199 ACCUMULATE cname spawn##cname##_1(cname this)
201 #define CONSTRUCTOR(cname, ...) \
202 cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__) \
206 ACCUMULATE cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__)
208 #define DESTRUCTOR(cname) \
209 STATIC_METHOD(cname, dtorimpl, void(cname this)); \
210 METHOD(cname, dtor, void(cname this)) \
212 METHOD_REFERENCE(cname, dtorimpl)(this); \
213 this.instanceOf##cname = false; \
214 entity super = SUPER(cname); \
215 if (super != cname##_vtbl) super.dtor(this); \
217 STATIC_METHOD(cname, dtorimpl, void(cname this))
219 #define SUPER(cname) (cname##_vtbl.vtblbase)
221 #define ATTRIB_3(cname, name, type) classfield(cname) .type name
222 #define ATTRIB_4(cname, name, type, val) \
223 ATTRIB_3(cname, name, type); \
226 noref bool strzone; /* Error on strzone() calls. */ \
229 ATTRIB_3(cname, name, type)
231 #define STATIC_ATTRIB(cname, name, type, val) \
232 type cname##_##name; \
233 _INIT_STATIC(cname) \
235 noref bool strzone; /* Error on strzone() calls. */ \
236 cname##_##name = val; \
239 // cleanup potentially zoned strings from base classes
240 #define ATTRIB_STRZONE(cname, name, type, val) \
241 classfield(cname).type name; \
244 strcpy(this.name, val); \
247 #define STATIC_ATTRIB_STRZONE(cname, name, type, val) \
248 type cname##_##name; \
249 _INIT_STATIC(cname) \
251 strcpy(cname##_##name, val); \
254 #define ATTRIBARRAY(cname, name, type, cnt) \
255 classfield(cname) .type name[cnt]
257 #define METHOD(cname, name, prototype) \
258 STATIC_METHOD(cname, name, prototype); \
259 classfield(cname) .prototype name; \
260 _INIT_STATIC(cname) \
262 this.name = METHOD_REFERENCE(cname, name); \
264 STATIC_METHOD(cname, name, prototype)
266 #define STATIC_METHOD(cname, name, prototype) \
267 prototype METHOD_REFERENCE(cname, name)
269 #define ENDCLASS(cname) \
280 void RegisterClasses() {}
281 STATIC_INIT(RegisterClasses)
286 #define VTBL(cname, base) \
287 _INIT_STATIC(cname); \
288 entity cname##_vtbl; \
289 void cname##_vtbl_init() \
291 cname e = new_pure(vtbl); \
292 spawn##cname##_static(e); \
293 e.vtblname = #cname; \
294 /* Top level objects refer to themselves */ \
295 e.vtblbase = base##_vtbl ? base##_vtbl : e; \
298 ACCUMULATE_FUNCTION(RegisterClasses, cname##_vtbl_init)
300 #define _INIT_STATIC(cname) ACCUMULATE void spawn##cname##_static(cname this)
303 #define DEBUG_STUFF(cname)
305 #define DEBUG_STUFF(cname) \
306 ERASEABLE bool is_##cname(entity e) { return e.instanceOf##cname; } \
307 ERASEABLE void isnt_##cname(entity e) { eprint(e); }
310 #define METHOD_REFERENCE(cname, name) \
315 #define spawn_static(this)
316 #define spawn_1(this)
319 DESTRUCTOR(Object) { builtin_remove(this); }
320 #define remove(this) delete(this)
321 METHOD(Object, describe, string(Object this))
324 string s = _("No description");
325 if (cvar("developer") > 0)
327 for (int i = 0, n = numentityfields(); i < n; ++i)
329 string value = getentityfieldstring(i, this);
330 if (value != "") s = sprintf("%s\n%s = %s", s, entityfieldname(i), value);
335 METHOD(Object, display, void(Object this, void(string name, string icon) returns))
338 returns(sprintf("entity %i", this), "nopreview_map");