]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/oo.qh
Linked entities: simplify registration
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / oo.qh
1 #ifndef OO_H
2 #define OO_H
3
4 #include "nil.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(...) EVAL(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 // Macros to hide this implementation detail:
43 #ifdef GMQCC
44 #define NEW(cname, ...) \
45     OVERLOAD(spawn##cname, new(cname), ##__VA_ARGS__)
46
47 #define CONSTRUCT(cname, ...) \
48     OVERLOAD(spawn##cname, this, ##__VA_ARGS__)
49 #else
50 #define NEW_(cname, ...) \
51     OVERLOAD_(spawn##cname, __VA_ARGS__)
52 #define NEW(cname, ...) \
53     NEW_(cname, new(cname), ##__VA_ARGS__)(new(cname), ##__VA_ARGS__)
54
55 #define CONSTRUCT_(cname, ...) \
56     OVERLOAD_(spawn##cname, __VA_ARGS__)
57 #define CONSTRUCT(cname, ...) \
58     CONSTRUCT_(cname, this, ##__VA_ARGS__)(this, ##__VA_ARGS__)
59 #endif
60
61 #define CONSTRUCTOR(cname, ...) \
62     cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__) { return = this; } \
63     [[accumulate]] cname OVERLOAD(spawn##cname, cname this, __VA_ARGS__)
64
65 .string vtblname;
66 .entity vtblbase;
67
68 void RegisterClasses() { }
69 STATIC_INIT(RegisterClasses) { RegisterClasses(); }
70
71 #define VTBL(cname, base) \
72     INIT_STATIC(cname); \
73     entity cname##_vtbl; \
74     void cname##_vtbl_init() { \
75         cname e = new(vtbl); \
76         spawn##cname##_static(e); \
77         e.vtblname = #cname; \
78         /* Top level objects refer to themselves */ \
79         e.vtblbase = base##_vtbl ? base##_vtbl : e; \
80         cname##_vtbl = e; \
81     } \
82     ACCUMULATE_FUNCTION(RegisterClasses, cname##_vtbl_init)
83
84 #define INIT_STATIC(cname) [[accumulate]] void spawn##cname##_static(cname this)
85 #define INIT(cname) [[accumulate]] cname spawn##cname##_1(cname this)
86
87 #define CLASS(cname, base)                  \
88     entityclass(cname, base);               \
89     class(cname) .bool instanceOf##cname;   \
90     VTBL(cname, base)                       \
91     INIT_STATIC(cname) {                    \
92         if (cname##_vtbl) {                 \
93             copyentity(cname##_vtbl, this); \
94             return;                         \
95         }                                   \
96         spawn##base##_static(this);         \
97         this.instanceOf##cname = true;      \
98     }                                       \
99     INIT(cname) {                           \
100         /* Only statically initialize the current class, it contains everything it inherits */ \
101         if (cname##_vtbl.vtblname == this.classname) { \
102             spawn##cname##_static(this);    \
103             this.classname = #cname;        \
104             this.vtblname = string_null;    \
105             this.vtblbase = cname##_vtbl;   \
106         }                                   \
107         spawn##base##_1(this);              \
108     }
109
110 #define METHOD(cname, name, prototype)      \
111     class(cname) .prototype name;           \
112     prototype cname##_##name;               \
113     INIT_STATIC(cname) { this.name = cname##_##name; } \
114     prototype cname##_##name
115
116 #define ATTRIB(cname, name, type, val)      \
117     class(cname) .type name;                \
118     INIT(cname) { this.name = val; }
119
120 #define ATTRIBARRAY(cname, name, type, cnt) \
121     class(cname) .type name[cnt];
122
123 #define ENDCLASS(cname) \
124     [[last]] INIT(cname) { return this; }
125
126 #define SUPER(cname) (cname##_vtbl.vtblbase)
127 #define super (this.vtblbase.vtblbase)
128
129 #define spawn_static(this)
130 #define spawn_1(this)
131 #define _vtbl NULL
132 CLASS(Object, );
133     METHOD(Object, describe, string(entity this)) {
134         string s = _("No description");
135         if (cvar("developer")) {
136             for (int i = 0, n = numentityfields(); i < n; ++i) {
137                 string value = getentityfieldstring(i, this);
138                 if (value != "") s = sprintf("%s\n%s = %s", s, entityfieldname(i), value);
139             }
140         }
141         return s;
142     }
143     METHOD(Object, display, void(entity this, void(string name, string icon) returns)) {
144         returns(sprintf("entity %i", this), "nopreview_map");
145     }
146 ENDCLASS(Object)
147 #undef spawn_static
148 #undef spawn_1
149 #undef _vtbl
150
151 #endif