9 #define NULL (null_entity)
16 #define make_pure(e) \
19 (e).pure_data = true; \
21 #define make_impure(e) \
24 (e).pure_data = false; \
26 #define is_pure(e) ((e).pure_data)
29 /** Location entity was spawned from in source */
30 .string sourceLocFile;
33 entity __spawn(string _classname, string _sourceFile, int _sourceLine, bool pure)
35 entity this = _spawn();
36 this.classname = _classname;
37 this.sourceLocFile = _sourceFile;
38 this.sourceLocLine = _sourceLine;
39 if (pure) make_pure(this);
44 #define entityclass(...) EVAL(OVERLOAD(entityclass, __VA_ARGS__))
45 #define entityclass_1(name) entityclass_2(name, Object)
46 #ifndef QCC_SUPPORT_ENTITYCLASS
47 #define entityclass_2(name, base) typedef entity name
49 #define new(class) __spawn( #class, __FILE__, __LINE__, false)
51 #define entityclass_2(name, base) entityclass name : base {}
52 #define class(name) [[class(name)]]
53 #define new(class) ((class) __spawn( #class, __FILE__, __LINE__, false))
55 #define spawn() __spawn("entity", __FILE__, __LINE__, false)
57 entity _clearentity_ent;
58 STATIC_INIT(clearentity)
60 _clearentity_ent = new(clearentity);
61 make_pure(_clearentity_ent);
63 void clearentity(entity e)
68 bool was_pure = is_pure(e);
69 copyentity(_clearentity_ent, e);
70 if (!was_pure) make_impure(e);
76 // Classes have a `spawn##cname(entity)` constructor
77 // The parameter is used across [[accumulate]] functions
79 // Macros to hide this implementation detail:
81 #define NEW(cname, ...) \
82 OVERLOAD(spawn##cname, new(cname),##__VA_ARGS__)
84 #define CONSTRUCT(cname, ...) \
85 OVERLOAD(spawn##cname, this,##__VA_ARGS__)
87 #define NEW_(cname, ...) \
88 OVERLOAD_(spawn##cname, __VA_ARGS__)
89 #define NEW(cname, ...) \
90 NEW_(cname, new(cname),##__VA_ARGS__)(new(cname),##__VA_ARGS__)
92 #define CONSTRUCT_(cname, ...) \
93 OVERLOAD_(spawn##cname, __VA_ARGS__)
94 #define CONSTRUCT(cname, ...) \
95 CONSTRUCT_(cname, this,##__VA_ARGS__)(this,##__VA_ARGS__)
98 #define CONSTRUCTOR(cname, ...) \
99 cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__) \
103 [[accumulate]] cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__)
108 void RegisterClasses() {}
109 STATIC_INIT(RegisterClasses)
114 #define VTBL(cname, base) \
115 INIT_STATIC(cname); \
116 entity cname##_vtbl; \
117 void cname##_vtbl_init() \
119 cname e = new(vtbl); \
121 spawn##cname##_static(e); \
122 e.vtblname = #cname; \
123 /* Top level objects refer to themselves */ \
124 e.vtblbase = base##_vtbl ? base##_vtbl : e; \
127 ACCUMULATE_FUNCTION(RegisterClasses, cname##_vtbl_init)
129 #define INIT_STATIC(cname) [[accumulate]] void spawn##cname##_static(cname this)
130 #define INIT(cname) [[accumulate]] cname spawn##cname##_1(cname this)
132 #define CLASS(cname, base) \
133 entityclass(cname, base); \
134 class(cname).bool instanceOf##cname; \
140 copyentity(cname##_vtbl, this); \
143 spawn##base##_static(this); \
144 this.instanceOf##cname = true; \
148 /* Only statically initialize the current class, it contains everything it inherits */ \
149 if (cname##_vtbl.vtblname == this.classname) \
151 spawn##cname##_static(this); \
152 this.classname = #cname; \
153 this.vtblname = string_null; \
154 this.vtblbase = cname##_vtbl; \
156 spawn##base##_1(this); \
159 #define METHOD_REFERENCE(cname, name) \
162 #define STATIC_METHOD(cname, name, prototype) \
163 prototype METHOD_REFERENCE(cname, name)
165 #define METHOD(cname, name, prototype) \
166 STATIC_METHOD(cname, name, prototype); \
167 class(cname) .prototype name; \
170 this.name = METHOD_REFERENCE(cname, name); \
172 STATIC_METHOD(cname, name, prototype)
174 #define ATTRIB(cname, name, type, val) \
175 class(cname).type name; \
178 noref bool strzone; /* Error on strzone() calls. */ \
182 #define ATTRIB_STRZONE(cname, name, type, val) \
183 class(cname).type name; \
187 strunzone(this.name); \
188 this.name = strzone(val); \
191 #define ATTRIBARRAY(cname, name, type, cnt) \
192 class(cname).type name[cnt];
194 #define ENDCLASS(cname) \
200 #define SUPER(cname) (cname##_vtbl.vtblbase)
202 #define spawn_static(this)
203 #define spawn_1(this)
206 METHOD(Object, describe, string(entity this))
208 string s = _("No description");
209 if (cvar("developer"))
211 for (int i = 0, n = numentityfields(); i < n; ++i)
213 string value = getentityfieldstring(i, this);
214 if (value != "") s = sprintf("%s\n%s = %s", s, entityfieldname(i), value);
219 METHOD(Object, display, void(entity this, void(string name, string icon) returns))
221 returns(sprintf("entity %i", this), "nopreview_map");