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;
43 setorigin(this, (world.mins + world.maxs) * 0.5);
46 setorigin(this, (world.mins + world.maxs) * 0.5);
53 #define entityclass(...) EVAL_entityclass(OVERLOAD_(entityclass, __VA_ARGS__))
54 #define EVAL_entityclass(...) __VA_ARGS__
55 #define entityclass_1(name) entityclass_2(name, Object)
56 #ifndef QCC_SUPPORT_ENTITYCLASS
57 #define entityclass_2(name, base) USING(name, entity)
58 #define classfield(name)
59 #define _new(class, pure) __spawn(#class, __FILE__ ":" STR(__LINE__), pure)
61 #define entityclass_2(name, base) entityclass name : base {}
62 #define classfield(name) [[class(name)]]
63 #define _new(class, pure) ((class) __spawn(#class, __FILE__ ":" STR(__LINE__), pure))
65 /** entities you care about seeing (.origin works) */
66 #define new(class) _new(class, false)
67 /** purely logical entities (.origin doesn't work) */
68 #define new_pure(class) _new(class, true)
69 #define spawn() __spawn("entity", __FILE__ ":" STR(__LINE__), false)
71 ACCUMULATE void ONREMOVE(entity this) {}
74 #define delete_fn builtin_remove
77 .void(entity this) dtor;
78 #define delete(this) MACRO_BEGIN \
79 entity _this = (this); \
80 void(entity) _dtor = _this.dtor; \
82 if (_dtor) _dtor(_this); else delete_fn(_this); \
86 entity _clearentity_ent;
87 STATIC_INIT(clearentity)
89 _clearentity_ent = new_pure(clearentity);
91 void clearentity(entity e)
96 bool was_pure = is_pure(e);
97 copyentity(_clearentity_ent, e);
98 if (!was_pure) make_impure(e);
104 // Classes have a `spawn##cname(entity)` constructor
105 // The parameter is used across ACCUMULATE functions
109 // Macros to hide this implementation detail:
111 #define NEW(cname, ...) \
112 OVERLOAD_(spawn##cname, new_pure(cname) P99_IF_EMPTY(__VA_ARGS__)()(, __VA_ARGS__))
114 #define _TRANSMUTE(cname, this, ...) \
115 OVERLOAD_(spawn##cname, this P99_IF_EMPTY(__VA_ARGS__)()(, __VA_ARGS__))
117 #define CONSTRUCT(cname, ...) \
118 OVERLOAD_(spawn##cname, this P99_IF_EMPTY(__VA_ARGS__)()(, __VA_ARGS__))
120 #define NEW(cname, ...) \
121 OVERLOAD(spawn##cname, new_pure(cname),##__VA_ARGS__)
123 #define _TRANSMUTE(cname, this, ...) \
124 OVERLOAD(spawn##cname, this,##__VA_ARGS__)
126 #define CONSTRUCT(cname, ...) \
127 OVERLOAD(spawn##cname, this,##__VA_ARGS__)
130 #define TRANSMUTE(cname, this, ...) MACRO_BEGIN \
131 entity _e = (this); \
132 if (_e.vtblbase != cname##_vtbl) { \
133 _e.transmute = true; \
134 _e.classname = #cname; \
135 _TRANSMUTE(cname, _e, __VA_ARGS__); \
139 #define CLASS(...) EVAL_CLASS(OVERLOAD__(CLASS, __VA_ARGS__))
140 #define EVAL_CLASS(...) __VA_ARGS__
142 #define ATTRIB(...) EVAL_ATTRIB(OVERLOAD_(ATTRIB, __VA_ARGS__))
143 #define EVAL_ATTRIB(...) __VA_ARGS__
145 #ifdef QCC_SUPPORT_CLASS
147 #warning "QCC_SUPPORT_CLASS not implemented"
149 #define CLASS_1(name) CLASS_2(name, entity)
150 #define CLASS_2(name, base) class name : base {
152 #define INIT(class) void class::class()
153 #define CONSTRUCTOR(class, ...) void class::class(__VA_ARGS__)
154 #define DESTRUCTOR(class) class::~class()
156 #define SUPER(class) super
158 #define ATTRIB_3(class, name, T) T name
159 #define ATTRIB_4(class, name, T, val) ATTRIB_3(class, name, T) = val
160 #define STATIC_ATTRIB(class, name, T, val) static T name = val
162 #define ATTRIB_STRZONE(class, name, T, val) T name = val
163 #define STATIC_ATTRIB_STRZONE(class, name, T, val) static T name = val
165 #define ATTRIBARRAY(class, name, T, val) T name[val]
167 #define METHOD(class, name, prototype) virtual void class::name()
168 #define STATIC_METHOD(class, name, prototype) static void class::name()
170 #define ENDCLASS(class) };
174 #define CLASS_1(cname) CLASS_2(cname, )
175 #define CLASS_2(cname, base) \
176 entityclass(cname, base); \
177 classfield(cname).bool instanceOf##cname; \
180 _INIT_STATIC(cname) \
182 if (cname##_vtbl && !this.transmute) \
184 copyentity(cname##_vtbl, this); \
187 spawn##base##_static(this); \
188 this.instanceOf##cname = true; \
192 /* Only statically initialize the current class, it contains everything it inherits */ \
193 if (cname##_vtbl.vtblname == this.classname) \
195 spawn##cname##_static(this); \
196 this.transmute = false; \
197 this.classname = #cname; \
198 this.vtblname = string_null; \
199 this.vtblbase = cname##_vtbl; \
201 spawn##base##_1(this); \
204 #define INIT(cname) \
205 ACCUMULATE cname spawn##cname##_1(cname this)
207 #define CONSTRUCTOR(cname, ...) \
208 cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__) \
212 ACCUMULATE cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__)
214 #define DESTRUCTOR(cname) \
215 STATIC_METHOD(cname, dtorimpl, void(cname this)); \
216 METHOD(cname, dtor, void(cname this)) \
218 METHOD_REFERENCE(cname, dtorimpl)(this); \
219 this.instanceOf##cname = false; \
220 entity super = SUPER(cname); \
221 if (super != cname##_vtbl) super.dtor(this); \
223 STATIC_METHOD(cname, dtorimpl, void(cname this))
225 #define SUPER(cname) (cname##_vtbl.vtblbase)
227 #define ATTRIB_3(cname, name, type) classfield(cname) .type name
228 #define ATTRIB_4(cname, name, type, val) \
229 ATTRIB_3(cname, name, type); \
232 noref bool strzone; /* Error on strzone() calls. */ \
235 ATTRIB_3(cname, name, type)
237 #define STATIC_ATTRIB(cname, name, type, val) \
238 type cname##_##name; \
239 _INIT_STATIC(cname) \
241 noref bool strzone; /* Error on strzone() calls. */ \
242 cname##_##name = val; \
245 // cleanup potentially zoned strings from base classes
246 #define ATTRIB_STRZONE(cname, name, type, val) \
247 classfield(cname).type name; \
250 strcpy(this.name, val); \
253 #define STATIC_ATTRIB_STRZONE(cname, name, type, val) \
254 type cname##_##name; \
255 _INIT_STATIC(cname) \
257 strcpy(cname##_##name, val); \
260 #define ATTRIBARRAY(cname, name, type, cnt) \
261 classfield(cname) .type name[cnt]
263 #define METHOD(cname, name, prototype) \
264 STATIC_METHOD(cname, name, prototype); \
265 classfield(cname) .prototype name; \
266 _INIT_STATIC(cname) \
268 this.name = METHOD_REFERENCE(cname, name); \
270 STATIC_METHOD(cname, name, prototype)
272 #define STATIC_METHOD(cname, name, prototype) \
273 prototype METHOD_REFERENCE(cname, name)
275 #define ENDCLASS(cname) \
286 void RegisterClasses() {}
287 STATIC_INIT(RegisterClasses)
292 #define VTBL(cname, base) \
293 _INIT_STATIC(cname); \
294 entity cname##_vtbl; \
295 void cname##_vtbl_init() \
297 cname e = new_pure(vtbl); \
298 spawn##cname##_static(e); \
299 e.vtblname = #cname; \
300 /* Top level objects refer to themselves */ \
301 e.vtblbase = base##_vtbl ? base##_vtbl : e; \
304 ACCUMULATE_FUNCTION(RegisterClasses, cname##_vtbl_init)
306 #define _INIT_STATIC(cname) ACCUMULATE void spawn##cname##_static(cname this)
309 #define DEBUG_STUFF(cname)
311 #define DEBUG_STUFF(cname) \
312 ERASEABLE bool is_##cname(entity e) { return e.instanceOf##cname; } \
313 ERASEABLE void isnt_##cname(entity e) { eprint(e); }
316 #define METHOD_REFERENCE(cname, name) \
321 #define spawn_static(this)
322 #define spawn_1(this)
325 DESTRUCTOR(Object) { builtin_remove(this); }
326 #define remove(this) delete(this)
327 METHOD(Object, describe, string(Object this))
330 string s = _("No description");
331 if (cvar("developer"))
333 for (int i = 0, n = numentityfields(); i < n; ++i)
335 string value = getentityfieldstring(i, this);
336 if (value != "") s = sprintf("%s\n%s = %s", s, entityfieldname(i), value);
341 METHOD(Object, display, void(Object this, void(string name, string icon) returns))
344 returns(sprintf("entity %i", this), "nopreview_map");