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