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