This repository has been archived by the owner on Sep 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
class.h
447 lines (379 loc) · 10.4 KB
/
class.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
//-----------------------------------------------------------------------------
//
// $Logfile:: /Quake 2 Engine/Sin/code/game/class.h $
// $Revision:: 17 $
// $Author:: Jimdose $
// $Date:: 10/25/98 11:53p $
//
// Copyright (C) 1997 by Ritual Entertainment, Inc.
// All rights reserved.
//
// This source is may not be distributed and/or modified without
// expressly written permission by Ritual Entertainment, Inc.
//
// $Log:: /Quake 2 Engine/Sin/code/game/class.h $
//
// 17 10/25/98 11:53p Jimdose
// added EXPORT_TEMPLATE
//
// 16 10/19/98 12:07a Jimdose
// made all code use fast checks for inheritance (no text lookups when
// possible)
// isSubclassOf no longer requires ::_classinfo()
//
// 15 10/07/98 11:42p Jimdose
// Got savegames working
//
// 14 9/24/98 1:49a Jimdose
// Added DisplayMemoryUsage
//
// 13 9/21/98 2:15a Jimdose
// Moved non-type specific code in SafePtr to SafePtrBase to help with save
// games
//
// 12 9/03/98 9:08p Jimdose
// Overrided == and != for SafePtr
//
// 11 6/27/98 9:18p Jimdose
// Made lookup for event responses for faster processing
//
// 10 5/24/98 4:48p Jimdose
// Made char *'s const
//
// 9 5/11/98 8:06p Jimdose
// Added SafePtr
//
// 8 5/08/98 2:51p Jimdose
// Added archiving functions
//
// 7 3/23/98 1:33p Jimdose
// Revamped event and command system
//
// 6 3/02/98 8:49p Jimdose
// Changed CLASS_PROTOTYPE to only take the classname
//
// 5 2/03/98 10:53a Jimdose
// Updated to work with Quake 2 engine
// Made class registration automatic
//
// 4 1/22/98 6:50p Jimdose
// Made Q2 compatible
//
// 2 9/26/97 6:13p Jimdose
// Added standard Ritual headers
//
// DESCRIPTION:
// Base class that all classes that are used in conjunction with Sin should
// be based off of. Class gives run-time type information about any class
// derived from it. This is really handy when you have a pointer to an object
// that you need to know if it supports certain behaviour.
//
#ifndef __CLASS_H__
#define __CLASS_H__
#include "g_local.h"
class Class;
class Event;
class Archiver;
typedef void ( Class::*Response )( Event *event );
typedef struct
{
Event *event;
Response response;
} ResponseDef;
/***********************************************************************
ClassDef
***********************************************************************/
class EXPORT_FROM_DLL ClassDef
{
public:
const char *classname;
const char *classID;
const char *superclass;
void *( *newInstance )( void );
int classSize;
ResponseDef *responses;
int numEvents;
Response **responseLookup;
ClassDef *super;
ClassDef *next;
ClassDef *prev;
ClassDef();
~ClassDef();
ClassDef( const char *classname, const char *classID, const char *superclass,
ResponseDef *responses, void *( *newInstance )( void ), int classSize );
void BuildResponseList( void );
};
/***********************************************************************
SafePtr
***********************************************************************/
class SafePtrBase;
class Class;
class EXPORT_FROM_DLL SafePtrBase
{
private:
void AddReference( Class *ptr );
void RemoveReference( Class *ptr );
protected:
SafePtrBase *prevSafePtr;
SafePtrBase *nextSafePtr;
Class *ptr;
public:
SafePtrBase();
virtual ~SafePtrBase();
void InitSafePtr( Class *newptr );
void Clear( void );
};
/***********************************************************************
Class
***********************************************************************/
#define CLASS_DECLARATION( nameofsuperclass, nameofclass, classid ) \
ClassDef nameofclass::ClassInfo \
( \
#nameofclass, classid, #nameofsuperclass, \
nameofclass::Responses, nameofclass::_newInstance, \
sizeof( nameofclass ) \
); \
EXPORT_FROM_DLL void *nameofclass::_newInstance( void ) \
{ \
return new nameofclass; \
} \
ClassDef *nameofclass::classinfo( void ) \
{ \
return &( nameofclass::ClassInfo ); \
}
#define CLASS_PROTOTYPE( nameofclass ) \
public: \
static ClassDef ClassInfo; \
static void *nameofclass::_newInstance( void ); \
virtual ClassDef *nameofclass::classinfo( void ); \
static ResponseDef nameofclass::Responses[];
class EXPORT_FROM_DLL Class
{
private:
SafePtrBase *SafePtrList;
friend class SafePtrBase;
public:
CLASS_PROTOTYPE( Class );
void * operator new( size_t );
void operator delete( void * );
Class();
virtual ~Class();
virtual void Archive( Archiver &arc );
virtual void Unarchive( Archiver &arc );
void warning( const char *function, const char *fmt, ... );
void error( const char *function, const char *fmt, ... );
qboolean inheritsFrom( ClassDef *c );
qboolean inheritsFrom( const char *name );
qboolean isInheritedBy( const char *name );
qboolean isInheritedBy( ClassDef *c );
const char *getClassname( void );
const char *getClassID( void );
const char *getSuperclass( void );
void *newInstance( void );
};
EXPORT_FROM_DLL void BuildEventResponses( void );
EXPORT_FROM_DLL ClassDef *getClassForID( const char *name );
EXPORT_FROM_DLL ClassDef *getClass( const char *name );
EXPORT_FROM_DLL ClassDef *getClassList( void );
EXPORT_FROM_DLL void listAllClasses( void );
EXPORT_FROM_DLL void listInheritanceOrder( const char *classname );
EXPORT_FROM_DLL qboolean checkInheritance( ClassDef *superclass, ClassDef *subclass );
EXPORT_FROM_DLL qboolean checkInheritance( ClassDef *superclass, const char *subclass );
EXPORT_FROM_DLL qboolean checkInheritance( const char *superclass, const char *subclass );
EXPORT_FROM_DLL void DisplayMemoryUsage( void );
inline EXPORT_FROM_DLL qboolean Class::inheritsFrom
(
ClassDef *c
)
{
return checkInheritance( c, classinfo() );
}
inline EXPORT_FROM_DLL qboolean Class::isInheritedBy
(
ClassDef *c
)
{
return checkInheritance( classinfo(), c );
}
// The lack of a space between the ")" and "inheritsFrom" is intentional.
// It allows the macro to compile like a function call. However, this
// may cause problems in some compilers (like gcc), so we may have to
// change this to work like a normal macro with the object passed in
// as a parameter to the macro.
#define isSubclassOf( classname )inheritsFrom( &classname::ClassInfo )
#define isSuperclassOf( classname )isInheritedBy( &classname::ClassInfo )
/***********************************************************************
SafePtr
***********************************************************************/
inline EXPORT_FROM_DLL SafePtrBase::SafePtrBase()
{
prevSafePtr = NULL;
nextSafePtr = NULL;
ptr = NULL;
}
inline EXPORT_FROM_DLL SafePtrBase::~SafePtrBase()
{
Clear();
}
inline EXPORT_FROM_DLL void SafePtrBase::Clear
(
void
)
{
if ( ptr )
{
RemoveReference( ptr );
ptr = NULL;
}
}
inline EXPORT_FROM_DLL void SafePtrBase::InitSafePtr
(
Class *newptr
)
{
if ( ptr != newptr )
{
if ( ptr )
{
RemoveReference( ptr );
}
ptr = newptr;
if ( ptr == NULL )
{
return;
}
AddReference( ptr );
}
}
inline EXPORT_FROM_DLL void SafePtrBase::AddReference
(
Class *ptr
)
{
if ( !ptr->SafePtrList )
{
ptr->SafePtrList = this;
LL_Reset( this, nextSafePtr, prevSafePtr );
}
else
{
LL_Add( ptr->SafePtrList, this, nextSafePtr, prevSafePtr );
}
}
inline EXPORT_FROM_DLL void SafePtrBase::RemoveReference
(
Class *ptr
)
{
if ( ptr->SafePtrList == this )
{
if ( ptr->SafePtrList->nextSafePtr == this )
{
ptr->SafePtrList = NULL;
}
else
{
ptr->SafePtrList = nextSafePtr;
LL_Remove( this, nextSafePtr, prevSafePtr );
}
}
else
{
LL_Remove( this, nextSafePtr, prevSafePtr );
}
}
template<class T>
class EXPORT_FROM_DLL SafePtr : public SafePtrBase
{
public:
SafePtr( T* objptr = 0 );
SafePtr( const SafePtr& obj );
SafePtr& operator=( const SafePtr& obj );
SafePtr& operator=( T * const obj );
friend EXPORT_FROM_DLL int operator==( SafePtr<T> a, T *b );
friend EXPORT_FROM_DLL int operator!=( SafePtr<T> a, T *b );
friend EXPORT_FROM_DLL int operator==( T *a, SafePtr<T> b );
friend EXPORT_FROM_DLL int operator!=( T *a, SafePtr<T> b );
operator T*() const;
T* operator->() const;
T& operator*() const;
};
template<class T>
inline EXPORT_FROM_DLL SafePtr<T>::SafePtr( T* objptr )
{
InitSafePtr( objptr );
}
template<class T>
inline EXPORT_FROM_DLL SafePtr<T>::SafePtr( const SafePtr& obj )
{
InitSafePtr( obj.ptr );
}
template<class T>
inline EXPORT_FROM_DLL SafePtr<T>& SafePtr<T>::operator=( const SafePtr& obj )
{
InitSafePtr( obj.ptr );
return *this;
}
template<class T>
inline EXPORT_FROM_DLL SafePtr<T>& SafePtr<T>::operator=( T * const obj )
{
InitSafePtr( obj );
return *this;
}
template<class T>
inline EXPORT_FROM_DLL int operator==
(
SafePtr<T> a,
T* b
)
{
return a.ptr == b;
}
template<class T>
inline EXPORT_FROM_DLL int operator!=
(
SafePtr<T> a,
T* b
)
{
return a.ptr != b;
}
template<class T>
inline EXPORT_FROM_DLL int operator==
(
T* a,
SafePtr<T> b
)
{
return a == b.ptr;
}
template<class T>
inline EXPORT_FROM_DLL int operator!=
(
T* a,
SafePtr<T> b
)
{
return a != b.ptr;
}
template<class T>
inline SafePtr<T>::operator T*() const
{
return ( T * )ptr;
}
template<class T>
inline T* SafePtr<T>::operator->() const
{
return ( T * )ptr;
}
template<class T>
inline T& SafePtr<T>::operator*() const
{
return *( T * )ptr;
}
#ifdef EXPORT_TEMPLATE
template class EXPORT_FROM_DLL SafePtr<Class>;
#endif
typedef SafePtr<Class> ClassPtr;
#include "archive.h"
#endif /* class.h */