OpenMoHAA 0.82.0
Loading...
Searching...
No Matches
class.h
1/*
2===========================================================================
3Copyright (C) 2025 the OpenMoHAA team
4
5This file is part of OpenMoHAA source code.
6
7OpenMoHAA source code is free software; you can redistribute it
8and/or modify it under the terms of the GNU General Public License as
9published by the Free Software Foundation; either version 2 of the License,
10or (at your option) any later version.
11
12OpenMoHAA source code is distributed in the hope that it will be
13useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with OpenMoHAA source code; if not, write to the Free Software
19Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20===========================================================================
21*/
22
23// class.h: General class
24
25#pragma once
26
27#include "con_set.h"
28#include "container.h"
29#include "q_shared.h"
30#include "str.h"
31#include "lightclass.h"
32
33#include "const_str.h"
34
35class Class;
36class Event;
37
38#define isSubclassOf(classname) inheritsFrom(&classname::ClassInfo)
39#define isSuperclassOf(classname) isInheritedBy(&classname::ClassInfo)
40
41#ifdef WITH_SCRIPT_ENGINE
42
43# define CLASS_DECLARATION(parentclass, classname, classid) \
44 ClassDef classname::ClassInfo( \
45 #classname, \
46 classid, \
47 #parentclass, \
48 (ResponseDef<Class> *)classname::Responses, \
49 classname::_newInstance, \
50 sizeof(classname) \
51 ); \
52 void *classname::_newInstance(void) \
53 { \
54 return new classname; \
55 } \
56 ClassDef *classname::classinfo(void) const \
57 { \
58 return &(classname::ClassInfo); \
59 } \
60 ClassDef *classname::classinfostatic(void) \
61 { \
62 return &(classname::ClassInfo); \
63 } \
64 void classname::AddWaitTill(str s) \
65 { \
66 classname::ClassInfo.AddWaitTill(s); \
67 } \
68 void classname::AddWaitTill(const_str s) \
69 { \
70 classname::ClassInfo.AddWaitTill(s); \
71 } \
72 void classname::RemoveWaitTill(str s) \
73 { \
74 classname::ClassInfo.RemoveWaitTill(s); \
75 } \
76 void classname::RemoveWaitTill(const_str s) \
77 { \
78 classname::ClassInfo.RemoveWaitTill(s); \
79 } \
80 bool classname::WaitTillDefined(str s) \
81 { \
82 return classname::ClassInfo.WaitTillDefined(s); \
83 } \
84 bool classname::WaitTillDefined(const_str s) \
85 { \
86 return classname::ClassInfo.WaitTillDefined(s); \
87 } \
88 bool classname::WaitTillAllowed(str s) \
89 { \
90 for (ClassDef *c = classinfo(); c; c = c->super) { \
91 if (c->WaitTillDefined(s)) { \
92 return true; \
93 } \
94 } \
95 return false; \
96 } \
97 bool classname::WaitTillAllowed(const_str s) \
98 { \
99 for (ClassDef *c = classinfo(); c; c = c->super) { \
100 if (c->WaitTillDefined(s)) { \
101 return true; \
102 } \
103 } \
104 return false; \
105 } \
106 ResponseDef<classname> classname::Responses[] =
107
108# define CLASS_PROTOTYPE(classname) \
109 \
110 public: \
111 static ClassDef ClassInfo; \
112 static ClassDefHook _ClassInfo_; \
113 static void *_newInstance(void); \
114 static ClassDef *classinfostatic(void); \
115 ClassDef *classinfo(void) const override; \
116 static void AddWaitTill(str s); \
117 static void AddWaitTill(const_str s); \
118 static void RemoveWaitTill(str s); \
119 static void RemoveWaitTill(const_str s); \
120 static bool WaitTillDefined(str s); \
121 static bool WaitTillDefined(const_str s); \
122 bool WaitTillAllowed(str s); \
123 bool WaitTillAllowed(const_str s); \
124 static ResponseDef<classname> Responses[]
125
126#else
127
128# define CLASS_DECLARATION(parentclass, classname, classid) \
129 ClassDef classname::ClassInfo( \
130 #classname, \
131 classid, \
132 #parentclass, \
133 (ResponseDef<Class> *)classname::Responses, \
134 classname::_newInstance, \
135 sizeof(classname) \
136 ); \
137 void *classname::_newInstance(void) \
138 { \
139 return new classname; \
140 } \
141 ClassDef *classname::classinfo(void) const \
142 { \
143 return &(classname::ClassInfo); \
144 } \
145 ClassDef *classname::classinfostatic(void) \
146 { \
147 return &(classname::ClassInfo); \
148 } \
149 ResponseDef<classname> classname::Responses[] =
150
151# define CLASS_PROTOTYPE(classname) \
152 \
153 public: \
154 static ClassDef ClassInfo; \
155 static ClassDefHook _ClassInfo_; \
156 static void *_newInstance(void); \
157 static ClassDef *classinfostatic(void); \
158 ClassDef *classinfo(void) const override; \
159 static ResponseDef<classname> Responses[]
160
161#endif
162
163typedef void (Class::*Response)(Event *ev);
164
165class EventDef;
166
167template<class Type>
169 Event *event;
170 void (Type::*response)(Event *ev);
171 EventDef *def;
172};
173
174class ClassDef
175{
176public:
177 const char *classname;
178 const char *classID;
179 const char *superclass;
180 void *(*newInstance)(void);
181 int classSize;
182 ResponseDef<Class> *responses;
183 ResponseDef<Class> **responseLookup;
184 ClassDef *super;
185 ClassDef *next;
186 ClassDef *prev;
187
188#ifdef WITH_SCRIPT_ENGINE
190#endif
191
192 int numEvents;
193
194 static ClassDef *classlist;
195 static ClassDef *classroot;
196 static int numclasses;
197
198 static int dump_numclasses;
199 static int dump_numevents;
200 static Container<int> sortedList;
201 static Container<ClassDef *> sortedClassList;
202
203public:
204 ClassDef();
205 ~ClassDef();
206
207 static int compareClasses(const void *arg1, const void *arg2);
208 static void SortClassList(Container<ClassDef *> *sortedList);
209
210#ifdef WITH_SCRIPT_ENGINE
211 void AddWaitTill(str s);
212 void AddWaitTill(const_str s);
213 void RemoveWaitTill(str s);
214 void RemoveWaitTill(const_str s);
215 bool WaitTillDefined(str s);
216 bool WaitTillDefined(const_str s);
217#endif
218
219 /* Create-a-class function */
220 ClassDef(
221 const char *classname,
222 const char *classID,
223 const char *superclass,
224 ResponseDef<Class> *responses,
225 void *(*newInstance)(void),
226 int classSize
227 );
228
229 EventDef *GetDef(int eventnum);
230 EventDef *GetDef(Event *ev);
231 int GetFlags(Event *event);
232
233 void Destroy();
234
235public:
236 static void BuildEventResponses();
237
238 void BuildResponseList();
239};
240
241ClassDef *getClassList(void);
242qboolean checkInheritance(const ClassDef *superclass, const ClassDef *subclass);
243qboolean checkInheritance(ClassDef *superclass, const char *subclass);
244qboolean checkInheritance(const char *superclass, const char *subclass);
245void CLASS_Print(FILE *class_file, const char *fmt, ...);
246void ClassEvents(const char *classname, qboolean print_to_disk);
247void DumpClass(FILE *class_file, const char *className);
248void DumpAllClasses(void);
249
250class ClassDefHook
251{
252private:
253 ClassDef *classdef;
254
255public:
256 // void * operator new( size_t );
257 // void operator delete( void * );
258
259 ClassDefHook();
260 ~ClassDefHook();
261
262 /* Hook-a-class function */
263 ClassDefHook(ClassDef *classdef, ResponseDef<Class> *responses);
264};
265
266ClassDef *getClassForID(const char *name);
267ClassDef *getClass(const char *name);
268ClassDef *getClassList(void);
269void listAllClasses(void);
270void listInheritanceOrder(const char *classname);
271
272class SafePtrBase;
273class Archiver;
274
275class Class : public LightClass
276{
277private:
278 friend class SafePtrBase;
279 SafePtrBase *SafePtrList;
280
281public:
282 static ClassDef ClassInfo;
283 static ClassDefHook _ClassInfo_; // Openmohaa addition
284 static ResponseDef<Class> Responses[];
285
286protected:
287 void ClearSafePointers();
288
289public:
290 Class();
291 virtual ~Class();
292 virtual ClassDef *classinfo(void) const;
293
294 static void *_newInstance(void);
295 static ClassDef *classinfostatic(void);
296
297 void warning(const char *function, const char *format, ...) const;
298 void error(const char *function, const char *format, ...) const;
299
300 qboolean inheritsFrom(ClassDef *c) const;
301 qboolean inheritsFrom(const char *name) const;
302 qboolean isInheritedBy(const char *name) const;
303 qboolean isInheritedBy(ClassDef *c) const;
304 const char *getClassname(void) const;
305 const char *getClassID(void) const;
306 const char *getSuperclass(void) const;
307
308 virtual void Archive(Archiver& arc);
309};
310
311#include "safeptr.h"
Definition archive.h:86
Definition class.h:251
Definition class.h:175
Definition class.h:276
Definition container.h:85
Definition listener.h:196
Definition listener.h:246
Definition lightclass.h:30
Definition safeptr.h:32
Definition con_set.h:119
Definition str.h:77
Definition class.h:168