]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/oo.qh
Fix memory leaks in ATTRIB(..., strzone(...)) calls.
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / oo.qh
1 #ifndef OO_H
2 #define OO_H
3
4 #include "misc.qh"
5 #include "nil.qh"
6 #include "static.qh"
7
8 #ifdef MENUQC
9         #define NULL (null_entity)
10 #else
11         #define NULL (world)
12 #endif
13
14 .vector origin;
15 .bool pure_data;
16 #define make_pure(e) \
17         do \
18         { \
19                 (e).pure_data = true; \
20         } \
21         while (0)
22 #define is_pure(e) ((e).pure_data)
23
24 .string classname;
25 /** Location entity was spawned from in source */
26 .string sourceLocFile;
27 .int sourceLocLine;
28 entity _spawn();
29 entity __spawn(string _classname, string _sourceFile, int _sourceLine, bool pure)
30 {
31         entity this = _spawn();
32         this.classname = _classname;
33         this.sourceLocFile = _sourceFile;
34         this.sourceLocLine = _sourceLine;
35         if (pure) make_pure(this);
36         return this;
37 }
38
39
40 #define entityclass(...) EVAL(OVERLOAD(entityclass, __VA_ARGS__))
41 #define entityclass_1(name) entityclass_2(name, Object)
42 #ifndef QCC_SUPPORT_ENTITYCLASS
43         #define entityclass_2(name, base) typedef entity name
44         #define class(name)
45         #define new(class) __spawn( #class, __FILE__, __LINE__, false)
46 #else
47         #define entityclass_2(name, base) entityclass name : base {}
48         #define class(name) [[class(name)]]
49         #define new(class) ((class) __spawn( #class, __FILE__, __LINE__, false))
50 #endif
51 #define spawn() __spawn("entity", __FILE__, __LINE__, false)
52
53 entity _clearentity_ent;
54 STATIC_INIT(clearentity)
55 {
56         _clearentity_ent = new(clearentity);
57 }
58 void clearentity(entity e)
59 {
60 #ifdef CSQC
61                 int n = e.entnum;
62 #endif
63         copyentity(_clearentity_ent, e);
64 #ifdef CSQC
65                 e.entnum = n;
66 #endif
67 }
68
69 // Classes have a `spawn##cname(entity)` constructor
70 // The parameter is used across [[accumulate]] functions
71
72 // Macros to hide this implementation detail:
73 #ifdef GMQCC
74         #define NEW(cname, ...) \
75                 OVERLOAD(spawn##cname, new(cname),##__VA_ARGS__)
76
77         #define CONSTRUCT(cname, ...) \
78                 OVERLOAD(spawn##cname, this,##__VA_ARGS__)
79 #else
80         #define NEW_(cname, ...) \
81                 OVERLOAD_(spawn##cname, __VA_ARGS__)
82         #define NEW(cname, ...) \
83                 NEW_(cname, new(cname),##__VA_ARGS__)(new(cname),##__VA_ARGS__)
84
85         #define CONSTRUCT_(cname, ...) \
86                 OVERLOAD_(spawn##cname, __VA_ARGS__)
87         #define CONSTRUCT(cname, ...) \
88                 CONSTRUCT_(cname, this,##__VA_ARGS__)(this,##__VA_ARGS__)
89 #endif
90
91 #define CONSTRUCTOR(cname, ...) \
92         cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__) \
93         { \
94                 return = this; \
95         } \
96         [[accumulate]] cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__)
97
98 .string vtblname;
99 .entity vtblbase;
100
101 void RegisterClasses() {}
102 STATIC_INIT(RegisterClasses)
103 {
104         RegisterClasses();
105 }
106
107 #define VTBL(cname, base) \
108         INIT_STATIC(cname); \
109         entity cname##_vtbl; \
110         void cname##_vtbl_init() \
111         { \
112                 cname e = new(vtbl); \
113                 make_pure(e); \
114                 spawn##cname##_static(e); \
115                 e.vtblname = #cname; \
116                 /* Top level objects refer to themselves */ \
117                 e.vtblbase = base##_vtbl ? base##_vtbl : e; \
118                 cname##_vtbl = e; \
119         } \
120         ACCUMULATE_FUNCTION(RegisterClasses, cname##_vtbl_init)
121
122 #define INIT_STATIC(cname) [[accumulate]] void spawn##cname##_static(cname this)
123 #define INIT(cname) [[accumulate]] cname spawn##cname##_1(cname this)
124
125 #define CLASS(cname, base)                  \
126         entityclass(cname, base);               \
127         class(cname).bool instanceOf##cname;   \
128         VTBL(cname, base)                       \
129         INIT_STATIC(cname) \
130         {                    \
131                 if (cname##_vtbl) \
132                 {                 \
133                         copyentity(cname##_vtbl, this); \
134                         return;                         \
135                 }                                   \
136                 spawn##base##_static(this);         \
137                 this.instanceOf##cname = true;      \
138         }                                       \
139         INIT(cname) \
140         {                           \
141                 /* Only statically initialize the current class, it contains everything it inherits */ \
142                 if (cname##_vtbl.vtblname == this.classname) \
143                 { \
144                         spawn##cname##_static(this);    \
145                         this.classname = #cname;        \
146                         this.vtblname = string_null;    \
147                         this.vtblbase = cname##_vtbl;   \
148                 }                                   \
149                 spawn##base##_1(this);              \
150         }
151
152 #define METHOD(cname, name, prototype)      \
153         class(cname).prototype name;           \
154         prototype cname##_##name;               \
155         INIT_STATIC(cname) \
156         { \
157                 this.name = cname##_##name; \
158         } \
159         prototype cname##_##name
160
161 #define ATTRIB(cname, name, type, val)      \
162         class(cname).type name;                \
163         INIT(cname) \
164         { \
165                 noref bool strzone; /* Error on strzone() calls. */ \
166                 this.name = val; \
167         }
168
169 #define ATTRIB_STRZONE(cname, name, type, val)      \
170         class(cname).type name;                \
171         INIT(cname) \
172         { \
173                 if (this.name) \
174                         strunzone(this.name); \
175                 this.name = strzone(val); \
176         }
177
178 #define ATTRIBARRAY(cname, name, type, cnt) \
179         class(cname).type name[cnt];
180
181 #define ENDCLASS(cname) \
182         INIT(cname) \
183         { \
184                 return this; \
185         }
186
187 #define SUPER(cname) (cname##_vtbl.vtblbase)
188
189 #define spawn_static(this)
190 #define spawn_1(this)
191 #define _vtbl NULL
192 CLASS(Object, );
193         METHOD(Object, describe, string(entity this))
194         {
195                 string s = _("No description");
196                 if (cvar("developer"))
197                 {
198                         for (int i = 0, n = numentityfields(); i < n; ++i)
199                         {
200                                 string value = getentityfieldstring(i, this);
201                                 if (value != "") s = sprintf("%s\n%s = %s", s, entityfieldname(i), value);
202                         }
203                 }
204                 return s;
205         }
206         METHOD(Object, display, void(entity this, void(string name, string icon) returns))
207         {
208                 returns(sprintf("entity %i", this), "nopreview_map");
209         }
210 ENDCLASS(Object)
211 #undef spawn_static
212 #undef spawn_1
213 #undef _vtbl
214
215 #endif