]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - sail/array.h
cgram: provide usage on incorrect invocation
[bsdgames-darwin.git] / sail / array.h
1 /*-
2 * Copyright (c) 2009 The NetBSD Foundation, Inc.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to The NetBSD Foundation
6 * by David A. Holland.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #ifndef ARRAY_H
31 #define ARRAY_H
32
33 #if defined(__GNUC__) && defined(__GNUC_STDC_INLINE__)
34 #define __c99inline extern __attribute__((__gnu_inline__)) __inline
35 #elif defined(__GNUC__)
36 #define __c99inline extern __inline
37 #elif defined(__STDC_VERSION__) || defined(__lint__)
38 #define __c99inline __inline
39 #endif
40
41 #define ARRAYS_CHECKED
42
43 #ifdef ARRAYS_CHECKED
44 #include <assert.h>
45 #define arrayassert assert
46 #else
47 #define arrayassert(x) ((void)(x))
48 #endif
49
50 ////////////////////////////////////////////////////////////
51 // type and base operations
52
53 struct array {
54 void **v;
55 unsigned num, max;
56 };
57
58 struct array *array_create(void);
59 void array_destroy(struct array *);
60 void array_init(struct array *);
61 void array_cleanup(struct array *);
62 unsigned array_num(const struct array *);
63 void *array_get(const struct array *, unsigned index_);
64 void array_set(const struct array *, unsigned index_, void *val);
65 int array_setsize(struct array *, unsigned num);
66 int array_add(struct array *, void *val, unsigned *index_ret);
67 int array_insert(struct array *a, unsigned index_);
68 void array_remove(struct array *a, unsigned index_);
69
70 ////////////////////////////////////////////////////////////
71 // inlining for base operations
72
73 #ifndef ARRAYINLINE
74 #define ARRAYINLINE __c99inline
75 #endif
76
77 ARRAYINLINE unsigned
78 array_num(const struct array *a)
79 {
80 return a->num;
81 }
82
83 ARRAYINLINE void *
84 array_get(const struct array *a, unsigned index_)
85 {
86 arrayassert(index_ < a->num);
87 return a->v[index_];
88 }
89
90 ARRAYINLINE void
91 array_set(const struct array *a, unsigned index_, void *val)
92 {
93 arrayassert(index_ < a->num);
94 a->v[index_] = val;
95 }
96
97 ARRAYINLINE int
98 array_add(struct array *a, void *val, unsigned *index_ret)
99 {
100 unsigned index_ = a->num;
101 if (array_setsize(a, index_+1)) {
102 return -1;
103 }
104 a->v[index_] = val;
105 if (index_ret != NULL) {
106 *index_ret = index_;
107 }
108 return 0;
109 }
110
111 ////////////////////////////////////////////////////////////
112 // bits for declaring and defining typed arrays
113
114 /*
115 * Usage:
116 *
117 * DECLARRAY_BYTYPE(foo, bar) declares "struct foo", which is
118 * an array of pointers to "bar", plus the operations on it.
119 *
120 * DECLARRAY(foo) is equivalent to DECLARRAY_BYTYPE(fooarray, struct foo).
121 *
122 * DEFARRAY_BYTYPE and DEFARRAY are the same as DECLARRAY except that
123 * they define the operations, and both take an extra argument INLINE.
124 * For C99 this should be INLINE in header files and empty in the
125 * master source file, the same as the usage of ARRAYINLINE above and
126 * in array.c.
127 *
128 * Example usage in e.g. item.h of some game:
129 *
130 * DECLARRAY_BYTYPE(stringarray, char);
131 * DECLARRAY(potion);
132 * DECLARRAY(sword);
133 *
134 * #ifndef ITEMINLINE
135 * #define ITEMINLINE INLINE
136 * #endif
137 *
138 * DEFARRAY_BYTYPE(stringarray, char, ITEMINLINE);
139 * DEFARRAY(potion, ITEMINLINE);
140 * DEFARRAY(sword, ITEMINLINE);
141 *
142 * Then item.c would do "#define ITEMINLINE" before including item.h.
143 */
144
145 #define DECLARRAY_BYTYPE(ARRAY, T) \
146 struct ARRAY { \
147 struct array arr; \
148 }; \
149 \
150 struct ARRAY *ARRAY##_create(void); \
151 void ARRAY##_destroy(struct ARRAY *a); \
152 void ARRAY##_init(struct ARRAY *a); \
153 void ARRAY##_cleanup(struct ARRAY *a); \
154 unsigned ARRAY##_num(const struct ARRAY *a); \
155 T *ARRAY##_get(const struct ARRAY *a, unsigned index_); \
156 void ARRAY##_set(struct ARRAY *a, unsigned index_, T *val); \
157 int ARRAY##_setsize(struct ARRAY *a, unsigned num); \
158 int ARRAY##_add(struct ARRAY *a, T *val, unsigned *index_ret); \
159 int ARRAY##_insert(struct ARRAY *a, unsigned index_); \
160 void ARRAY##_remove(struct ARRAY *a, unsigned index_)
161
162
163 #define DEFARRAY_BYTYPE(ARRAY, T, INLINE) \
164 INLINE void \
165 ARRAY##_init(struct ARRAY *a) \
166 { \
167 array_init(&a->arr); \
168 } \
169 \
170 INLINE void \
171 ARRAY##_cleanup(struct ARRAY *a) \
172 { \
173 array_cleanup(&a->arr); \
174 } \
175 \
176 INLINE struct \
177 ARRAY *ARRAY##_create(void) \
178 { \
179 struct ARRAY *a; \
180 \
181 a = malloc(sizeof(*a)); \
182 if (a == NULL) { \
183 return NULL; \
184 } \
185 ARRAY##_init(a); \
186 return a; \
187 } \
188 \
189 INLINE void \
190 ARRAY##_destroy(struct ARRAY *a) \
191 { \
192 ARRAY##_cleanup(a); \
193 free(a); \
194 } \
195 \
196 INLINE unsigned \
197 ARRAY##_num(const struct ARRAY *a) \
198 { \
199 return array_num(&a->arr); \
200 } \
201 \
202 INLINE T * \
203 ARRAY##_get(const struct ARRAY *a, unsigned index_) \
204 { \
205 return (T *)array_get(&a->arr, index_); \
206 } \
207 \
208 INLINE void \
209 ARRAY##_set(struct ARRAY *a, unsigned index_, T *val) \
210 { \
211 array_set(&a->arr, index_, (void *)val); \
212 } \
213 \
214 INLINE int \
215 ARRAY##_setsize(struct ARRAY *a, unsigned num) \
216 { \
217 return array_setsize(&a->arr, num); \
218 } \
219 \
220 INLINE int \
221 ARRAY##_add(struct ARRAY *a, T *val, unsigned *ret) \
222 { \
223 return array_add(&a->arr, (void *)val, ret); \
224 } \
225 \
226 INLINE int \
227 ARRAY##_insert(struct ARRAY *a, unsigned index_) \
228 { \
229 return array_insert(&a->arr, index_); \
230 } \
231 \
232 INLINE void \
233 ARRAY##_remove(struct ARRAY *a, unsigned index_) \
234 { \
235 return array_remove(&a->arr, index_); \
236 }
237
238 #define DECLARRAY(T) DECLARRAY_BYTYPE(T##array, struct T)
239 #define DEFARRAY(T, INLINE) DEFARRAY_BYTYPE(T##array, struct T, INLINE)
240
241 ////////////////////////////////////////////////////////////
242 // basic array types
243
244 DECLARRAY_BYTYPE(stringarray, char);
245 DEFARRAY_BYTYPE(stringarray, char, ARRAYINLINE);
246
247 #endif /* ARRAY_H */