558a8731992bbc458ae41530572e928a174108d8
[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 // Classes have a `spawn##cname(entity)` constructor
53 // The parameter is used across [[accumulate]] functions
54
55 // Macros to hide this implementation detail:
56 #ifdef GMQCC
57         #define NEW(cname, ...) \
58                 OVERLOAD(spawn##cname, new(cname),##__VA_ARGS__)
59
60         #define CONSTRUCT(cname, ...) \
61                 OVERLOAD(spawn##cname, this,##__VA_ARGS__)
62 #else
63         #define NEW_(cname, ...) \
64                 OVERLOAD_(spawn##cname, __VA_ARGS__)
65         #define NEW(cname, ...) \
66                 NEW_(cname, new(cname),##__VA_ARGS__)(new(cname),##__VA_ARGS__)
67
68         #define CONSTRUCT_(cname, ...) \
69                 OVERLOAD_(spawn##cname, __VA_ARGS__)
70         #define CONSTRUCT(cname, ...) \
71                 CONSTRUCT_(cname, this,##__VA_ARGS__)(this,##__VA_ARGS__)
72 #endif
73
74 #define CONSTRUCTOR(cname, ...) \
75         cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__) \
76         { \
77                 return = this; \
78         } \
79         [[accumulate]] cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__)
80
81 .string vtblname;
82 .entity vtblbase;
83
84 void RegisterClasses() {}
85 STATIC_INIT(RegisterClasses)
86 {
87         RegisterClasses();
88 }
89
90 #define VTBL(cname, base) \
91         INIT_STATIC(cname); \
92         entity cname##_vtbl; \
93         void cname##_vtbl_init() \
94         { \
95                 cname e = new(vtbl); \
96                 make_pure(e); \
97                 spawn##cname##_static(e); \
98                 e.vtblname = #cname; \
99                 /* Top level objects refer to themselves */ \
100                 e.vtblbase = base##_vtbl ? base##_vtbl : e; \
101                 cname##_vtbl = e; \
102         } \
103         ACCUMULATE_FUNCTION(RegisterClasses, cname##_vtbl_init)
104
105 #define INIT_STATIC(cname) [[accumulate]] void spawn##cname##_static(cname this)
106 #define INIT(cname) [[accumulate]] cname spawn##cname##_1(cname this)
107
108 #define CLASS(cname, base)                  \
109         entityclass(cname, base);               \
110         class(cname).bool instanceOf##cname;   \
111         VTBL(cname, base)                       \
112         INIT_STATIC(cname) \
113         {                    \
114                 if (cname##_vtbl) \
115                 {                 \
116                         copyentity(cname##_vtbl, this); \
117                         return;                         \
118                 }                                   \
119                 spawn##base##_static(this);         \
120                 this.instanceOf##cname = true;      \
121         }                                       \
122         INIT(cname) \
123         {                           \
124                 /* Only statically initialize the current class, it contains everything it inherits */ \
125                 if (cname##_vtbl.vtblname == this.classname) \
126                 { \
127                         spawn##cname##_static(this);    \
128                         this.classname = #cname;        \
129                         this.vtblname = string_null;    \
130                         this.vtblbase = cname##_vtbl;   \
131                 }                                   \
132                 spawn##base##_1(this);              \
133         }
134
135 #define METHOD(cname, name, prototype)      \
136         class(cname).prototype name;           \
137         prototype cname##_##name;               \
138         INIT_STATIC(cname) \
139         { \
140                 this.name = cname##_##name; \
141         } \
142         prototype cname##_##name
143
144 #define ATTRIB(cname, name, type, val)      \
145         class(cname).type name;                \
146         INIT(cname) \
147         { \
148                 this.name = val; \
149         }
150
151 #define ATTRIBARRAY(cname, name, type, cnt) \
152         class(cname).type name[cnt];
153
154 #define ENDCLASS(cname) \
155         INIT(cname) \
156         { \
157                 return this; \
158         }
159
160 #define SUPER(cname) (cname##_vtbl.vtblbase)
161
162 #define spawn_static(this)
163 #define spawn_1(this)
164 #define _vtbl NULL
165 CLASS(Object, );
166         METHOD(Object, describe, string(entity this))
167         {
168                 string s = _("No description");
169                 if (cvar("developer"))
170                 {
171                         for (int i = 0, n = numentityfields(); i < n; ++i)
172                         {
173                                 string value = getentityfieldstring(i, this);
174                                 if (value != "") s = sprintf("%s\n%s = %s", s, entityfieldname(i), value);
175                         }
176                 }
177                 return s;
178         }
179         METHOD(Object, display, void(entity this, void(string name, string icon) returns))
180         {
181                 returns(sprintf("entity %i", this), "nopreview_map");
182         }
183 ENDCLASS(Object)
184 #undef spawn_static
185 #undef spawn_1
186 #undef _vtbl
187
188 #endif