8 #define NULL (null_entity)
14 /** Location entity was spawned from in source */
15 .string sourceLocFile;
18 entity __spawn(string _classname, string _sourceFile, int _sourceLine) {
19 entity this = _spawn();
20 this.classname = _classname;
21 this.sourceLocFile = _sourceFile;
22 this.sourceLocLine = _sourceLine;
28 #define entityclass(...) EVAL(OVERLOAD(entityclass, __VA_ARGS__))
29 #define entityclass_1(name) entityclass_2(name, Object)
30 #ifndef QCC_SUPPORT_ENTITYCLASS
31 #define entityclass_2(name, base) typedef entity name
33 #define new(class) __spawn(#class, __FILE__, __LINE__)
35 #define entityclass_2(name, base) entityclass name : base {}
36 #define class(name) [[class(name)]]
37 #define new(class) ((class) __spawn(#class, __FILE__, __LINE__))
39 #define spawn() new(entity)
41 // Classes have a `spawn##cname(entity)` constructor
42 // The parameter is used across [[accumulate]] functions
44 // Macros to hide this implementation detail:
46 #define NEW(cname, ...) \
47 OVERLOAD(spawn##cname, new(cname), ##__VA_ARGS__)
49 #define CONSTRUCT(cname, ...) \
50 OVERLOAD(spawn##cname, this, ##__VA_ARGS__)
52 #define NEW_(cname, ...) \
53 OVERLOAD_(spawn##cname, __VA_ARGS__)
54 #define NEW(cname, ...) \
55 NEW_(cname, new(cname), ##__VA_ARGS__)(new(cname), ##__VA_ARGS__)
57 #define CONSTRUCT_(cname, ...) \
58 OVERLOAD_(spawn##cname, __VA_ARGS__)
59 #define CONSTRUCT(cname, ...) \
60 CONSTRUCT_(cname, this, ##__VA_ARGS__)(this, ##__VA_ARGS__)
63 #define CONSTRUCTOR(cname, ...) \
64 cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__) { return = this; } \
65 [[accumulate]] cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__)
70 void RegisterClasses() { }
71 STATIC_INIT(RegisterClasses) { RegisterClasses(); }
73 #define VTBL(cname, base) \
75 entity cname##_vtbl; \
76 void cname##_vtbl_init() { \
77 cname e = new(vtbl); \
78 spawn##cname##_static(e); \
79 e.vtblname = #cname; \
80 /* Top level objects refer to themselves */ \
81 e.vtblbase = base##_vtbl ? base##_vtbl : e; \
84 ACCUMULATE_FUNCTION(RegisterClasses, cname##_vtbl_init)
86 #define INIT_STATIC(cname) [[accumulate]] void spawn##cname##_static(cname this)
87 #define INIT(cname) [[accumulate]] cname spawn##cname##_1(cname this)
89 #define CLASS(cname, base) \
90 entityclass(cname, base); \
91 class(cname) .bool instanceOf##cname; \
93 INIT_STATIC(cname) { \
95 copyentity(cname##_vtbl, this); \
98 spawn##base##_static(this); \
99 this.instanceOf##cname = true; \
102 /* Only statically initialize the current class, it contains everything it inherits */ \
103 if (cname##_vtbl.vtblname == this.classname) { \
104 spawn##cname##_static(this); \
105 this.classname = #cname; \
106 this.vtblname = string_null; \
107 this.vtblbase = cname##_vtbl; \
109 spawn##base##_1(this); \
112 #define METHOD(cname, name, prototype) \
113 class(cname) .prototype name; \
114 prototype cname##_##name; \
115 INIT_STATIC(cname) { this.name = cname##_##name; } \
116 prototype cname##_##name
118 #define ATTRIB(cname, name, type, val) \
119 class(cname) .type name; \
120 INIT(cname) { this.name = val; }
122 #define ATTRIBARRAY(cname, name, type, cnt) \
123 class(cname) .type name[cnt];
125 #define ENDCLASS(cname) \
126 [[last]] INIT(cname) { return this; }
128 #define SUPER(cname) (cname##_vtbl.vtblbase)
129 #define super (this.vtblbase.vtblbase)
131 #define spawn_static(this)
132 #define spawn_1(this)
135 METHOD(Object, describe, string(entity this)) {
136 string s = _("No description");
137 if (cvar("developer")) {
138 for (int i = 0, n = numentityfields(); i < n; ++i) {
139 string value = getentityfieldstring(i, this);
140 if (value != "") s = sprintf("%s\n%s = %s", s, entityfieldname(i), value);
145 METHOD(Object, display, void(entity this, void(string name, string icon) returns)) {
146 returns(sprintf("entity %i", this), "nopreview_map");