Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
opcodegarray.h
Go to the documentation of this file.
1 /*
2  Crystal Space utility library: vector class interface
3  Copyright (C) 1998,1999,2000 by Andrew Zabolotny <bit@eltech.ru>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public
16  License along with this library; if not, write to the Free
17  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 
20 #ifndef __CS_GARRAY_H__
21 #define __CS_GARRAY_H__
22 
23 // Common macro for declarations below
24 #define CS_TYPEDEF_GROWING_ARRAY_EXT(Name, Type, ExtraConstructor, Extra) \
25  class Name \
26  { \
27  typedef Type ga_type; \
28  ga_type *root; \
29  int limit; \
30  int length; \
31  public: \
32  int Limit () const \
33  { return limit; } \
34  void SetLimit (int iLimit) \
35  { \
36  if (limit == iLimit) return; \
37  if ((limit = iLimit)!=0) \
38  root = (ga_type *)realloc (root, limit * sizeof (ga_type)); \
39  else \
40  { if (root) { free (root); root = NULL; } } \
41  } \
42  Name () \
43  { limit = length = 0; root = NULL; ExtraConstructor; } \
44  ~Name () \
45  { SetLimit (0); } \
46  int Length () const \
47  { return length; } \
48  void SetLength (int iLength, int iGrowStep = 8) \
49  { \
50  length = iLength; \
51  int newlimit = ((length + (iGrowStep - 1)) / iGrowStep) * iGrowStep;\
52  if (newlimit != limit) SetLimit (newlimit); \
53  } \
54  ga_type &operator [] (int n) \
55  { CS_ASSERT (n >= 0 && n < limit); return root [n]; } \
56  const ga_type &operator [] (int n) const \
57  { CS_ASSERT (n >= 0 && n < limit); return root [n]; } \
58  ga_type &Get (int n) \
59  { CS_ASSERT (n >= 0 && n < limit); return root [n]; } \
60  void Delete (int n) \
61  { CS_ASSERT (n >= 0 && n < limit); \
62  memmove (root + n, root + n + 1, (limit - n - 1) * sizeof (ga_type)); \
63  SetLength (length-1); } \
64  ga_type *GetArray () \
65  { return root; } \
66  int Push (const ga_type &val, int iGrowStep = 8) \
67  { \
68  SetLength (length + 1, iGrowStep); \
69  memcpy (root + length - 1, &val, sizeof (ga_type)); \
70  return length-1; \
71  } \
72  void Insert (int pos, const ga_type &val, int iGrowStep = 8) \
73  { \
74  CS_ASSERT (pos>=0 && pos<=length); \
75  SetLength (length + 1, iGrowStep); \
76  memmove (root+pos+1, root+pos, sizeof(ga_type) * (length-pos-1)); \
77  memcpy (root + pos, &val, sizeof (ga_type)); \
78  } \
79  Extra \
80  }
81 
101 #define CS_TYPEDEF_GROWING_ARRAY(Name, Type) \
102  CS_TYPEDEF_GROWING_ARRAY_EXT (Name, Type, ;, ;)
103 
115 #define CS_TYPEDEF_GROWING_ARRAY_REF(Name, Type) \
116  CS_TYPEDEF_GROWING_ARRAY_EXT (Name, Type, RefCount = 0, \
117  int RefCount; \
118  void IncRef () \
119  { RefCount++; } \
120  void DecRef () \
121  { \
122  if (RefCount == 1) SetLimit (0); \
123  RefCount--; \
124  })
125 
136 #define CS_DECLARE_GROWING_ARRAY(Name, Type) \
137  CS_TYPEDEF_GROWING_ARRAY(__##Name##_##Type,Type) Name
138 
142 #define CS_DECLARE_GROWING_ARRAY_REF(Name, Type) \
143  CS_TYPEDEF_GROWING_ARRAY_REF(__##Name,Type) Name
144 
145 #endif // __CS_GARRAY_H__