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.cpp
643 lines (542 loc) · 11.7 KB
/
class.cpp
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
//-----------------------------------------------------------------------------
//
// $Logfile:: /Quake 2 Engine/Sin/code/game/class.cpp $
// $Revision:: 20 $
// $Author:: Jimdose $
// $Date:: 10/19/98 12:07a $
//
// 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.cpp $
//
// 20 10/19/98 12:07a Jimdose
// made all code use fast checks for inheritance (no text lookups when
// possible)
// isSubclassOf no longer requires ::_classinfo()
//
// 19 10/07/98 11:42p Jimdose
// Got savegames working
//
// 18 9/24/98 1:49a Jimdose
// Added DisplayMemoryUsage
//
// 17 9/21/98 2:15a Jimdose
// Moved non-type specific code in SafePtr to SafePtrBase to help with save
// games
//
// 16 8/27/98 9:04p Jimdose
// NumEventCommands is now a member of Event
//
// 15 6/27/98 9:18p Jimdose
// Made lookup for event responses for faster processing
//
// 14 6/15/98 9:09p Aldie
// Fixed checkInheritance printfs
//
// 13 5/24/98 4:48p Jimdose
// Made char *'s const
//
// 12 5/11/98 8:06p Jimdose
// Added SafePtr
//
// 11 5/08/98 2:51p Jimdose
// Added archiving functions
//
// 10 3/27/98 6:35p Jimdose
// made checking of classnames case independant
//
// 9 3/23/98 1:31p Jimdose
// Revamped event and command system
//
// 8 3/04/98 1:49p Jimdose
// Changed overloaded delete so that it used delete[] instead of delete, since
// we allocate with new[];
//
// 7 3/02/98 8:49p Jimdose
// Changed the classid parameter of CLASS_DECLARATION to a quoted string so
// that you could have a NULL classid.
//
// 6 2/03/98 10:53a Jimdose
// Updated to work with Quake 2 engine
// Made class registration automatic
//
// 5 1/22/98 6:50p Jimdose
// Made Q2 compatible
//
// 3 10/27/97 3:40p Jimdose
// Included stdarg.h
//
// 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.
//
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include "g_local.h"
#include "class.h"
#include "linklist.h"
int totalmemallocated = 0;
int numclassesallocated = 0;
static ClassDef *classlist = NULL;
ClassDef::ClassDef()
{
this->classname = NULL;
this->classID = NULL;
this->superclass = NULL;
this->responses = NULL,
this->numEvents = 0;
this->responseLookup = NULL;
this->newInstance = NULL;
this->classSize = 0;
this->super = NULL;
this->prev = this;
this->next = this;
}
ClassDef::ClassDef
(
const char *classname,
const char *classID,
const char *superclass,
ResponseDef *responses,
void *( *newInstance )( void ),
int classSize
)
{
ClassDef *node;
if ( classlist == NULL )
{
classlist = new ClassDef;
}
this->classname = classname;
this->classID = classID;
this->superclass = superclass;
this->responses = responses;
this->numEvents = 0;
this->responseLookup = NULL;
this->newInstance = newInstance;
this->classSize = classSize;
this->super = getClass( superclass );
// It's not uncommon for classes to not have a class id, so just set it
// to an empty string so that we're not checking for it all the time.
if ( !classID )
{
this->classID = "";
}
// Check if any subclasses were initialized before their superclass
for( node = classlist->next; node != classlist; node = node->next )
{
if ( ( node->super == NULL ) && ( !Q_stricmp( node->superclass, this->classname ) ) &&
( Q_stricmp( node->classname, "Class" ) ) )
{
node->super = this;
}
}
// Add to front of list
LL_Add( classlist, this, prev, next );
}
ClassDef::~ClassDef()
{
ClassDef *node;
if ( classlist != this )
{
LL_Remove( this, prev, next );
// Check if any subclasses were initialized before their superclass
for( node = classlist->next; node != classlist; node = node->next )
{
if ( node->super == this )
{
node->super = NULL;
}
}
}
else
{
// If the head of the list is deleted before the list is cleared, then we may have problems
assert( this->next == this->prev );
}
if ( responseLookup )
{
delete[] responseLookup;
responseLookup = NULL;
}
}
EXPORT_FROM_DLL void ClassDef::BuildResponseList
(
void
)
{
ClassDef *c;
ResponseDef *r;
int ev;
int i;
qboolean *set;
int num;
if ( responseLookup )
{
delete[] responseLookup;
responseLookup = NULL;
}
num = Event::NumEventCommands();
responseLookup = ( Response ** )new char[ sizeof( Response * ) * num ];
memset( responseLookup, 0, sizeof( Response * ) * num );
set = new qboolean[ num ];
memset( set, 0, sizeof( qboolean ) * num );
this->numEvents = num;
for( c = this; c != NULL; c = c->super )
{
r = c->responses;
if ( r )
{
for( i = 0; r[ i ].event != NULL; i++ )
{
ev = ( int )*r[ i ].event;
if ( !set[ ev ] )
{
set[ ev ] = true;
if ( r[ i ].response )
{
responseLookup[ ev ] = &r[ i ].response;
}
else
{
responseLookup[ ev ] = NULL;
}
}
}
}
}
delete[] set;
}
EXPORT_FROM_DLL void BuildEventResponses
(
void
)
{
ClassDef *c;
int amount;
int numclasses;
amount = 0;
numclasses = 0;
for( c = classlist->next; c != classlist; c = c->next )
{
c->BuildResponseList();
amount += c->numEvents * sizeof( Response * );
numclasses++;
}
gi.dprintf( "\n------------------\nEvent system initialized:\n"
"%d classes\n%d events\n%d total memory in response list\n\n",
numclasses, Event::NumEventCommands(), amount );
}
EXPORT_FROM_DLL ClassDef *getClassForID
(
const char *name
)
{
ClassDef *c;
for( c = classlist->next; c != classlist; c = c->next )
{
if ( c->classID && !Q_stricmp( c->classID, name ) )
{
return c;
}
}
return NULL;
}
EXPORT_FROM_DLL ClassDef *getClass
(
const char *name
)
{
ClassDef *c;
for( c = classlist->next; c != classlist; c = c->next )
{
if ( !Q_stricmp( c->classname, name ) )
{
return c;
}
}
return NULL;
}
EXPORT_FROM_DLL ClassDef *getClassList
(
void
)
{
return classlist;
}
EXPORT_FROM_DLL void listAllClasses
(
void
)
{
ClassDef *c;
for( c = classlist->next; c != classlist; c = c->next )
{
gi.dprintf( "%s\n", c->classname );
}
}
EXPORT_FROM_DLL void listInheritanceOrder
(
const char *classname
)
{
ClassDef *cls;
ClassDef *c;
cls = getClass( classname );
if ( !cls )
{
gi.dprintf( "Unknown class: %s\n", classname );
return;
}
for( c = cls; c != NULL; c = c->super )
{
gi.dprintf( "%s\n", c->classname );
}
}
EXPORT_FROM_DLL qboolean checkInheritance
(
ClassDef *superclass,
ClassDef *subclass
)
{
ClassDef *c;
for( c = subclass; c != NULL; c = c->super )
{
if ( c == superclass )
{
return true;
}
}
return false;
}
EXPORT_FROM_DLL qboolean checkInheritance
(
ClassDef *superclass,
const char *subclass
)
{
ClassDef *c;
c = getClass( subclass );
if ( c == NULL )
{
gi.dprintf( "Unknown class: %s\n", subclass );
return false;
}
return checkInheritance( superclass, c );
}
EXPORT_FROM_DLL qboolean checkInheritance
(
const char *superclass,
const char *subclass
)
{
ClassDef *c1;
ClassDef *c2;
c1 = getClass( superclass );
c2 = getClass( subclass );
if ( c1 == NULL )
{
gi.dprintf( "Unknown class: %s\n", superclass );
return false;
}
if ( c2 == NULL )
{
gi.dprintf( "Unknown class: %s\n", subclass );
return false;
}
return checkInheritance( c1, c2 );
}
CLASS_DECLARATION( NULL, Class, NULL );
ResponseDef Class::Responses[] =
{
{ NULL, NULL }
};
#ifdef NDEBUG
EXPORT_FROM_DLL void * Class::operator new( size_t s )
{
int *p;
s += sizeof( int );
p = ( int * )::new char[ s ];
*p = s;
totalmemallocated += s;
numclassesallocated++;
return p + 1;
}
EXPORT_FROM_DLL void Class::operator delete( void *ptr )
{
int *p;
p = ( ( int * )ptr ) - 1;
totalmemallocated -= *p;
numclassesallocated--;
::delete[]( p );
}
#else
EXPORT_FROM_DLL void * Class::operator new( size_t s )
{
int *p;
s += sizeof( int ) * 3;
p = ( int * )::new char[ s ];
p[ 0 ] = 0x12348765;
*( int * )( ((byte *)p) + s - sizeof( int ) ) = 0x56784321;
p[ 1 ] = s;
totalmemallocated += s;
numclassesallocated++;
return p + 2;
}
EXPORT_FROM_DLL void Class::operator delete( void *ptr )
{
int *p;
p = ( ( int * )ptr ) - 2;
assert( p[ 0 ] == 0x12348765 );
assert( *( int * )( ((byte *)p) + p[ 1 ] - sizeof( int ) ) == 0x56784321 );
totalmemallocated -= p[ 1 ];
numclassesallocated--;
::delete[]( p );
}
#endif
EXPORT_FROM_DLL void DisplayMemoryUsage
(
void
)
{
gi.printf( "Classes %-5d Class memory used: %d\n", numclassesallocated, totalmemallocated );
}
Class::Class()
{
SafePtrList = NULL;
}
Class::~Class()
{
while( SafePtrList != NULL )
{
SafePtrList->Clear();
}
}
EXPORT_FROM_DLL void Class::Archive
(
Archiver &arc
)
{
}
EXPORT_FROM_DLL void Class::Unarchive
(
Archiver &arc
)
{
}
EXPORT_FROM_DLL void Class::warning
(
const char *function,
const char *fmt,
...
)
{
va_list argptr;
char text[ 1024 ];
va_start( argptr, fmt );
vsprintf( text, fmt, argptr );
va_end( argptr );
if ( getClassID() )
{
gi.dprintf( "%s::%s : %s\n", getClassID(), function, text );
}
else
{
gi.dprintf( "%s::%s : %s\n", getClassname(), function, text );
}
}
EXPORT_FROM_DLL void Class::error
(
const char *function,
const char *fmt,
...
)
{
va_list argptr;
char text[ 1024 ];
va_start( argptr, fmt );
vsprintf( text, fmt, argptr );
va_end( argptr );
if ( getClassID() )
{
gi.error( "%s::%s : %s\n", getClassID(), function, text );
}
else
{
gi.error( "%s::%s : %s\n", getClassname(), function, text );
}
}
EXPORT_FROM_DLL qboolean Class::inheritsFrom
(
const char *name
)
{
ClassDef *c;
c = getClass( name );
if ( c == NULL )
{
gi.dprintf( "Unknown class: %s\n", name );
return false;
}
return checkInheritance( c, classinfo() );
}
EXPORT_FROM_DLL qboolean Class::isInheritedBy
(
const char *name
)
{
ClassDef *c;
c = getClass( name );
if ( c == NULL )
{
gi.dprintf( "Unknown class: %s\n", name );
return false;
}
return checkInheritance( classinfo(), c );
}
EXPORT_FROM_DLL const char *Class::getClassname
(
void
)
{
ClassDef *cls;
cls = classinfo();
return cls->classname;
}
EXPORT_FROM_DLL const char *Class::getClassID
(
void
)
{
ClassDef *cls;
cls = classinfo();
return cls->classID;
}
EXPORT_FROM_DLL const char *Class::getSuperclass
(
void
)
{
ClassDef *cls;
cls = classinfo();
return cls->superclass;
}
EXPORT_FROM_DLL void *Class::newInstance
(
void
)
{
ClassDef *cls;
cls = classinfo();
return cls->newInstance();
}