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