7 #define NULL (null_entity)
13 /** Location entity was spawned from in source */
14 .string sourceLocFile;
17 entity __spawn(string _classname, string _sourceFile, int _sourceLine) {
18 entity this = _spawn();
19 this.classname = _classname;
20 this.sourceLocFile = _sourceFile;
21 this.sourceLocLine = _sourceLine;
27 #define entityclass(...) OVERLOAD(entityclass, __VA_ARGS__)
28 #define entityclass_1(name) entityclass_2(name, Object)
29 #ifndef QCC_SUPPORT_ENTITYCLASS
30 #define entityclass_2(name, base) typedef entity name
32 #define new(class) __spawn(#class, __FILE__, __LINE__)
34 #define entityclass_2(name, base) entityclass name : base {}
35 #define class(name) [[class(name)]]
36 #define new(class) ((class) __spawn(#class, __FILE__, __LINE__))
39 // Classes have a `spawn##cname(entity)` constructor
40 // The parameter is used across [[accumulate]] functions
42 // Macro to hide this implementation detail
43 #define NEW(cname, ...) \
44 OVERLOAD(spawn##cname, new(cname), ##__VA_ARGS__)
46 #define CONSTRUCT(cname, ...) \
47 OVERLOAD(spawn##cname, this, ##__VA_ARGS__)
49 #define CONSTRUCTOR(cname, ...) \
50 cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__) { return = this; } \
51 [[accumulate]] cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__)
56 void RegisterClasses() { }
57 STATIC_INIT(RegisterClasses) { RegisterClasses(); }
59 #define VTBL(cname, base) \
61 entity cname##_vtbl; \
62 void cname##_vtbl_init() { \
63 cname e = new(vtbl); \
64 spawn##cname##_static(e); \
65 e.vtblname = #cname; \
66 /* Top level objects refer to themselves */ \
67 e.vtblbase = base##_vtbl ? base##_vtbl : e; \
70 ACCUMULATE_FUNCTION(RegisterClasses, cname##_vtbl_init)
72 #define INIT_STATIC(cname) [[accumulate]] void spawn##cname##_static(cname this)
73 #define INIT(cname) [[accumulate]] cname spawn##cname##_1(cname this)
75 #define CLASS(cname, base) \
76 entityclass(cname, base); \
77 class(cname) .bool instanceOf##cname; \
79 INIT_STATIC(cname) { \
81 copyentity(cname##_vtbl, this); \
84 spawn##base##_static(this); \
85 this.instanceOf##cname = true; \
88 /* Only statically initialize the current class, it contains everything it inherits */ \
89 if (cname##_vtbl.vtblname == this.classname) { \
90 spawn##cname##_static(this); \
91 this.classname = #cname; \
92 this.vtblname = string_null; \
93 this.vtblbase = cname##_vtbl; \
95 spawn##base##_1(this); \
98 #define METHOD(cname, name, prototype) \
99 class(cname) .prototype name; \
100 prototype cname##_##name; \
101 INIT_STATIC(cname) { this.name = cname##_##name; } \
102 prototype cname##_##name
104 #define ATTRIB(cname, name, type, val) \
105 class(cname) .type name; \
106 INIT(cname) { this.name = val; }
108 #define ATTRIBARRAY(cname, name, type, cnt) \
109 class(cname) .type name[cnt];
111 #define ENDCLASS(cname) \
112 [[last]] INIT(cname) { return this; }
114 #define SUPER(cname) (cname##_vtbl.vtblbase)
115 #define super (this.vtblbase.vtblbase)
117 #define spawn_static(this)
118 #define spawn_1(this)
121 METHOD(Object, describe, string(entity this)) {
122 string s = _("No description");
123 if (cvar("developer")) {
124 for (int i = 0, n = numentityfields(); i < n; ++i) {
125 string value = getentityfieldstring(i, this);
126 if (value != "") s = sprintf("%s\n%s = %s", s, entityfieldname(i), value);
131 METHOD(Object, display, void(entity this, void(string name, string icon) returns)) {
132 returns(sprintf("entity %i", this), "nopreview_map");