source: golgotha/src/i4/memory/growarry.hh @ 80

Last change on this file since 80 was 80, checked in by Sam Hocevar, 12 years ago
  • Adding the Golgotha source code. Not sure what's going to be interesting in there, but since it's all public domain, there's certainly stuff to pick up.
File size: 1.5 KB
Line 
1/********************************************************************** <BR>
2  This file is part of Crack dot Com's free source code release of
3  Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
4  information about compiling & licensing issues visit this URL</a>
5  <PRE> If that doesn't help, contact Jonathan Clark at
6  golgotha_source@usa.net (Subject should have "GOLG" in it)
7***********************************************************************/
8
9#ifndef I4_GROW_ARRAY_HH
10#define I4_GROW_ARRAY_HH
11
12
13#include "arch.hh"
14#include "memory/malloc.hh"
15#include "error/error.hh"
16
17template <class T>
18class i4_grow_array
19//{{{
20{
21protected:
22  T *entry;
23  w32 used,entries,grow;
24  char *name;
25public:
26
27  int size() const { return used; }
28
29  T& operator[](int i) { return entry[i];  }
30
31  i4_grow_array(w32 entries, char *name, w32 grow = 0)
32    : entries(entries), name(name), grow(grow), entry(0), used(0)
33  {
34    if (entries>0)
35      entry = (T*)i4_malloc(sizeof(T)*entries, name);
36    else
37      entry = (T*)i4_malloc(sizeof(T)*grow, name);
38  }
39
40  w32 add(T item)
41  {
42    if (used>=entries)
43    {
44      if (grow)
45      {
46        entries += grow;
47        T* new_entry = (T*)i4_realloc(entry, sizeof(T *)*entries, name);
48        entry = new_entry;
49      }
50      else
51        i4_error("i4_grow_array '%s' out of entries",name);
52    }
53    entry[used] = item;
54    used++;
55    return used-1;
56  }
57
58  void clear() { used = 0; }
59  ~i4_grow_array() { i4_free(entry); }
60};
61
62
63
64#endif
Note: See TracBrowser for help on using the repository browser.