File: container.h

package info (click to toggle)
openmohaa 0.82.1%2Bdfsg-1
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 34,192 kB
  • sloc: cpp: 315,720; ansic: 275,789; sh: 312; xml: 246; asm: 141; makefile: 7
file content (543 lines) | stat: -rw-r--r-- 12,024 bytes parent folder | download
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
/*
===========================================================================
Copyright (C) 2025 the OpenMoHAA team

This file is part of OpenMoHAA source code.

OpenMoHAA source code is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.

OpenMoHAA source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with OpenMoHAA source code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
===========================================================================
*/

// container.h: C++ Container

#pragma once

#if defined(GAME_DLL)
//
// game dll specific defines
//
#    include "../fgame/g_local.h"

#    define CONTAINER_Error          gi.Error
#    define CONTAINER_DPrintf        gi.DPrintf
#    define CONTAINER_WDPrintf(text) gi.DPrintf(text)
#    define CONTAINER_Alloc          gi.Malloc
#    define CONTAINER_Free           gi.Free

#elif defined(CGAME_DLL)
//
// cgame dll specific defines
//
#    include "../cgame/cg_local.h"

#    define CONTAINER_Error          cgi.Error
#    define CONTAINER_DPrintf        cgi.DPrintf
#    define CONTAINER_WDPrintf(text) cgi.DPrintf(text)
#    define CONTAINER_Alloc          cgi.Malloc
#    define CONTAINER_Free           cgi.Free

#elif defined(REF_DLL)

#    include "../renderercommon/tr_common.h"

//
// client specific defines
//
#    define CONTAINER_Error          Com_Error
#    define CONTAINER_DPrintf        Com_DPrintf
#    define CONTAINER_WDPrintf(text) Com_DPrintf(text)
#    define CONTAINER_Alloc          ri.Malloc
#    define CONTAINER_Free           ri.Free

#else

#    include "qcommon.h"

//
// client specific defines
//
#    define CONTAINER_Error          Com_Error
#    define CONTAINER_DPrintf        Com_DPrintf
#    define CONTAINER_WDPrintf(text) Com_DPrintf(text)
#    define CONTAINER_Alloc          Z_Malloc
#    define CONTAINER_Free           Z_Free
#endif

#include <utility>
#include <new>

class Archiver;

template<class Type>
class Container
{
private:
    Type *objlist;
    int   numobjects;
    int   maxobjects;

private:
    void Copy(const Container<Type>& container);

public:
    Container();

    Container(const Container<Type>& container);
    Container<Type>& operator=(const Container<Type>& container);

    Container(Container<Type>&& container);
    Container<Type>& operator=(Container<Type>&& container);

    ~Container();

    void Archive(Archiver& arc);
    void Archive(Archiver& arc, void (*ArchiveFunc)(Archiver& arc, Type *obj));

    int   AddObject(const Type& obj);
    int   AddUniqueObject(const Type& obj);
    void  AddObjectAt(int index, const Type& obj);
    Type *AddressOfObjectAt(int index);
    void  ClearObjectList(void);
    void  FreeObjectList(void);
    int   IndexOfObject(const Type& obj);
    void  InsertObjectAt(int index, const Type& obj);
    int   MaxObjects(void) const;
    int   NumObjects(void) const;
    Type& ObjectAt(const size_t index) const;
    bool  ObjectInList(const Type& obj);
    void  RemoveObjectAt(int index);
    void  RemoveObject(const Type& obj);
    void  Reset(void);
    void  Resize(int maxelements);
    void  SetObjectAt(int index, const Type& obj);
    void  Sort(int (*compare)(const void *elem1, const void *elem2));
    Type& operator[](const uintptr_t index) const;
};

template<class Type>
Container<Type>::Container()
{
    objlist    = NULL;
    numobjects = 0;
    maxobjects = 0;
}

template<class Type>
Container<Type>::Container(const Container<Type>& container)
{
    objlist = NULL;

    Copy(container);
}

template<class Type>
Container<Type>& Container<Type>::operator=(const Container<Type>& container)
{
    Copy(container);

    return *this;
}

template<class Type>
Container<Type>::Container(Container<Type>&& container)
{
    objlist              = container.objlist;
    numobjects           = container.numobjects;
    maxobjects           = container.maxobjects;
    container.objlist    = NULL;
    container.numobjects = 0;
    container.maxobjects = 0;
}

template<class Type>
Container<Type>& Container<Type>::operator=(Container<Type>&& container)
{
    FreeObjectList();

    objlist              = container.objlist;
    numobjects           = container.numobjects;
    maxobjects           = container.maxobjects;
    container.objlist    = NULL;
    container.numobjects = 0;
    container.maxobjects = 0;

    return *this;
}

template<class Type>
Container<Type>::~Container()
{
    FreeObjectList();
}

template<class Type>
int Container<Type>::AddObject(const Type& obj)
{
    if (!objlist) {
        Resize(10);
    }

    if (numobjects >= maxobjects) {
        Resize(numobjects * 2);
    }

    new (objlist + numobjects) Type(obj);
    numobjects++;

    return numobjects;
}

template<class Type>
int Container<Type>::AddUniqueObject(const Type& obj)
{
    int index;

    index = IndexOfObject(obj);

    if (!index) {
        index = AddObject(obj);
    }

    return index;
}

template<class Type>
void Container<Type>::AddObjectAt(int index, const Type& obj)
{
    int i;

    if (index > maxobjects) {
        Resize(index);
    }

    if (index > numobjects) {
        for (i = numobjects; i < index; i++) {
            new (objlist + i) Type();
        }

        numobjects = index;
    }

    SetObjectAt(index, obj);
}

template<class Type>
Type *Container<Type>::AddressOfObjectAt(int index)
{
    if (index > maxobjects) {
        CONTAINER_Error(ERR_DROP, "Container::AddressOfObjectAt : index is greater than maxobjects");
    }

    if (index > numobjects) {
        numobjects = index;
    }

    return &objlist[index - 1];
}

template<class Type>
void Container<Type>::ClearObjectList(void)
{
    size_t i;

    if (objlist && numobjects) {
        for (i = 0; i < numobjects; ++i) {
            objlist[i].~Type();
        }

        // don't free the object list, it can be reused later
        numobjects = 0;
    }
}

template<class Type>
void Container<Type>::FreeObjectList(void)
{
    size_t i;

    if (objlist) {
        for (i = 0; i < numobjects; ++i) {
            objlist[i].~Type();
        }

        CONTAINER_Free(objlist);
    }

    objlist    = NULL;
    numobjects = 0;
    maxobjects = 0;
}

template<class Type>
int Container<Type>::IndexOfObject(const Type& obj)
{
    int i;

    if (!objlist) {
        return 0;
    }

    for (i = 0; i < numobjects; i++) {
        if (objlist[i] == obj) {
            return i + 1;
        }
    }

    return 0;
}

template<class Type>
void Container<Type>::InsertObjectAt(int index, const Type& obj)
{
    size_t i;

    if ((index <= 0) || (index > numobjects + 1)) {
        CONTAINER_Error(ERR_DROP, "Container::InsertObjectAt : index out of range");
        return;
    }

    numobjects++;
    intptr_t arrayIndex = index - 1;

    if (numobjects > maxobjects) {
        maxobjects = numobjects;
        if (!objlist) {
            objlist = (Type *)CONTAINER_Alloc(sizeof(Type) * maxobjects);

            for (i = 0; i < arrayIndex; ++i) {
                new (objlist + i) Type();
            }

            new (objlist + arrayIndex) Type(obj);
        } else {
            Type *temp = objlist;
            if (maxobjects < numobjects) {
                maxobjects = numobjects;
            }

            objlist = (Type *)CONTAINER_Alloc(sizeof(Type) * maxobjects);

            for (i = 0; i < arrayIndex; ++i) {
                new (objlist + i) Type(std::move(temp[i]));
            }

            new (objlist + arrayIndex) Type(obj);
            for (i = arrayIndex; i < numobjects - 1; ++i) {
                new (objlist + i + 1) Type(std::move(temp[i]));
            }

            CONTAINER_Free(temp);
        }
    } else {
        for (i = numobjects - 1; i > arrayIndex; i--) {
            objlist[i] = std::move(objlist[i - 1]);
        }
        objlist[arrayIndex] = obj;
    }
}

template<class Type>
int Container<Type>::MaxObjects(void) const
{
    return maxobjects;
}

template<class Type>
int Container<Type>::NumObjects(void) const
{
    return numobjects;
}

template<class Type>
Type& Container<Type>::ObjectAt(const size_t index) const
{
    if ((index <= 0) || (index > numobjects)) {
        CONTAINER_Error(ERR_DROP, "Container::ObjectAt : index out of range");
    }

    return objlist[index - 1];
}

template<class Type>
bool Container<Type>::ObjectInList(const Type& obj)
{
    if (!IndexOfObject(obj)) {
        return false;
    }

    return true;
}

template<class Type>
void Container<Type>::RemoveObjectAt(int index)
{
    int i;

    if (!objlist) {
        return;
    }

    if ((index <= 0) || (index > numobjects)) {
        return;
    }

    i = index - 1;
    numobjects--;

    for (i = index - 1; i < numobjects; i++) {
        objlist[i] = std::move(objlist[i + 1]);
    }

    // Destroy the last object as it's now useless
    objlist[numobjects].~Type();
}

template<class Type>
void Container<Type>::RemoveObject(const Type& obj)
{
    int index;

    index = IndexOfObject(obj);

    assert(index);
    if (!index) {
        CONTAINER_WDPrintf("Container::RemoveObject : Object not in list\n");
        return;
    }

    RemoveObjectAt(index);
}

template<class Type>
void Container<Type>::Reset()
{
    objlist    = NULL;
    numobjects = 0;
    maxobjects = 0;
}

template<class Type>
void Container<Type>::Resize(int maxelements)
{
    Type  *temp;
    size_t i;

    if (maxelements <= 0) {
        FreeObjectList();
        return;
    }

    if (!objlist) {
        maxobjects = maxelements;
        objlist    = (Type *)CONTAINER_Alloc(sizeof(Type) * maxobjects);
    } else {
        temp = objlist;

        maxobjects = maxelements;

        if (maxobjects < numobjects) {
            maxobjects = numobjects;
        }

        objlist = (Type *)CONTAINER_Alloc(sizeof(Type) * maxobjects);

        for (i = 0; i < numobjects; i++) {
            // move the older type
            new (objlist + i) Type(std::move(temp[i]));

            // destruct the older type
            temp[i].~Type();
        }

        CONTAINER_Free(temp);
    }
}

template<class Type>
void Container<Type>::SetObjectAt(int index, const Type& obj)
{
    if (!objlist) {
        return;
    }

    if ((index <= 0) || (index > numobjects)) {
        CONTAINER_Error(ERR_DROP, "Container::SetObjectAt : index out of range");
    }

    objlist[index - 1] = obj;
}

template<class Type>
void Container<Type>::Sort(int (*compare)(const void *elem1, const void *elem2))
{
    if (!objlist) {
        return;
    }

    qsort((void *)objlist, (size_t)numobjects, sizeof(Type), compare);
}

template<class Type>
Type& Container<Type>::operator[](const uintptr_t index) const
{
    return ObjectAt(index + 1);
}

template<class Type>
void Container<Type>::Copy(const Container<Type>& container)
{
    int i;

    if (&container == this) {
        return;
    }

    FreeObjectList();

    numobjects = container.numobjects;
    maxobjects = container.maxobjects;
    objlist    = NULL;

    if (container.objlist == NULL || !container.maxobjects) {
        return;
    }

    Resize(maxobjects);

    if (!container.numobjects) {
        return;
    }

    for (i = 0; i < container.numobjects; i++) {
        new (objlist + i) Type(container.objlist[i]);
    }

    return;
}

template<typename T>
void *operator new(size_t count, Container<T>& container)
{
    (void)count;

    assert(count == sizeof(T));
    return &container.ObjectAt(container.AddObject());
}

template<typename T>
void operator delete(void *ptr, Container<T>& container)
{
    container.RemoveObject((T *)ptr);
}