]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/oo.qh
Backport icon handling from TimePath/guide
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / oo.qh
1 #ifndef OO_H
2 #define OO_H
3
4 #include "registry.qh"
5
6 #ifdef MENUQC
7     #define NULL (null_entity)
8 #else
9     #define NULL (world)
10 #endif
11
12 .string classname;
13 /** Location entity was spawned from in source */
14 .string sourceLocFile;
15 .int sourceLocLine;
16 entity _spawn();
17 entity __spawn(string _classname, string _sourceFile, int _sourceLine) {
18     entity this = _spawn();
19     this.classname = _classname;
20     this.sourceLocFile = _sourceFile;
21     this.sourceLocLine = _sourceLine;
22     return this;
23 }
24
25
26
27 #define entityclass(...) OVERLOAD(entityclass, __VA_ARGS__)
28 #define entityclass_1(name) entityclass_2(name, Object)
29 #ifndef QCC_SUPPORT_ENTITYCLASS
30     #define entityclass_2(name, base) typedef entity name
31     #define class(name)
32     #define new(class) __spawn(#class, __FILE__, __LINE__)
33 #else
34     #define entityclass_2(name, base) entityclass name : base {}
35     #define class(name) [[class(name)]]
36     #define new(class) ((class) __spawn(#class, __FILE__, __LINE__))
37 #endif
38
39 // Classes have a `spawn##cname(entity)` constructor
40 // The parameter is used across [[accumulate]] functions
41
42 // Macro to hide this implementation detail
43 #define NEW(cname, ...) \
44     OVERLOAD(spawn##cname, new(cname), ##__VA_ARGS__)
45
46 #define CONSTRUCT(cname, ...) \
47     OVERLOAD(spawn##cname, this, ##__VA_ARGS__)
48
49 #define CONSTRUCTOR(cname, ...) \
50     cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__) { return = this; } \
51     [[accumulate]] cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__)
52
53 .string vtblname;
54 .entity vtblbase;
55
56 void RegisterClasses() { }
57 STATIC_INIT(RegisterClasses) { RegisterClasses(); }
58
59 #define VTBL(cname, base) \
60     INIT_STATIC(cname); \
61     entity cname##_vtbl; \
62     void cname##_vtbl_init() { \
63         cname e = new(vtbl); \
64         spawn##cname##_static(e); \
65         e.vtblname = #cname; \
66         /* Top level objects refer to themselves */ \
67         e.vtblbase = base##_vtbl ? base##_vtbl : e; \
68         cname##_vtbl = e; \
69     } \
70     ACCUMULATE_FUNCTION(RegisterClasses, cname##_vtbl_init)
71
72 #define INIT_STATIC(cname) [[accumulate]] void spawn##cname##_static(cname this)
73 #define INIT(cname) [[accumulate]] cname spawn##cname##_1(cname this)
74
75 #define CLASS(cname, base)                  \
76     entityclass(cname, base);               \
77     class(cname) .bool instanceOf##cname;   \
78     VTBL(cname, base)                       \
79     INIT_STATIC(cname) {                    \
80         if (cname##_vtbl) {                 \
81             copyentity(cname##_vtbl, this); \
82             return;                         \
83         }                                   \
84         spawn##base##_static(this);         \
85         this.instanceOf##cname = true;      \
86     }                                       \
87     INIT(cname) {                           \
88         /* Only statically initialize the current class, it contains everything it inherits */ \
89         if (cname##_vtbl.vtblname == this.classname) { \
90             spawn##cname##_static(this);    \
91             this.classname = #cname;        \
92             this.vtblname = string_null;    \
93             this.vtblbase = cname##_vtbl;   \
94         }                                   \
95         spawn##base##_1(this);              \
96     }
97
98 #define METHOD(cname, name, prototype)      \
99     class(cname) .prototype name;           \
100     prototype cname##_##name;               \
101     INIT_STATIC(cname) { this.name = cname##_##name; } \
102     prototype cname##_##name
103
104 #define ATTRIB(cname, name, type, val)      \
105     class(cname) .type name;                \
106     INIT(cname) { this.name = val; }
107
108 #define ATTRIBARRAY(cname, name, type, cnt) \
109     class(cname) .type name[cnt];
110
111 #define ENDCLASS(cname) \
112     [[last]] INIT(cname) { return this; }
113
114 #define SUPER(cname) (cname##_vtbl.vtblbase)
115 #define super (this.vtblbase.vtblbase)
116
117 #define spawn_static(this)
118 #define spawn_1(this)
119 #define _vtbl NULL
120 CLASS(Object, );
121     METHOD(Object, describe, string(entity this)) {
122         string s = _("No description");
123         if (cvar("developer")) {
124             for (int i = 0, n = numentityfields(); i < n; ++i) {
125                 string value = getentityfieldstring(i, this);
126                 if (value != "") s = sprintf("%s\n%s = %s", s, entityfieldname(i), value);
127             }
128         }
129         return s;
130     }
131     METHOD(Object, display, void(entity this, void(string name, string icon) returns)) {
132         returns(sprintf("entity %i", this), "nopreview_map");
133     }
134 ENDCLASS(Object)
135 #undef spawn_static
136 #undef spawn_1
137 #undef _vtbl
138
139 #endif