Mercurial > ~dholland > hg > tradcpp > index.cgi
comparison array.h @ 47:2e25e55dba6b
Fix inline usage as per the version in dholland-make2.
author | David A. Holland |
---|---|
date | Sat, 30 Mar 2013 23:05:59 -0400 |
parents | 337110e7240a |
children | 0921c47b4f22 |
comparison
equal
deleted
inserted
replaced
46:eaa154ded584 | 47:2e25e55dba6b |
---|---|
40 #define arrayassert assert | 40 #define arrayassert assert |
41 #else | 41 #else |
42 #define arrayassert(x) ((void)(x)) | 42 #define arrayassert(x) ((void)(x)) |
43 #endif | 43 #endif |
44 | 44 |
45 #ifndef ARRAYINLINE | |
46 #define ARRAYINLINE __c99inline | |
47 #endif | |
48 | |
45 //////////////////////////////////////////////////////////// | 49 //////////////////////////////////////////////////////////// |
46 // type and base operations | 50 // type and base operations |
47 | 51 |
48 struct array { | 52 struct array { |
49 void **v; | 53 void **v; |
52 | 56 |
53 struct array *array_create(void); | 57 struct array *array_create(void); |
54 void array_destroy(struct array *); | 58 void array_destroy(struct array *); |
55 void array_init(struct array *); | 59 void array_init(struct array *); |
56 void array_cleanup(struct array *); | 60 void array_cleanup(struct array *); |
57 unsigned array_num(const struct array *); | 61 ARRAYINLINE unsigned array_num(const struct array *); |
58 void *array_get(const struct array *, unsigned index_); | 62 ARRAYINLINE void *array_get(const struct array *, unsigned index_); |
59 void array_set(const struct array *, unsigned index_, void *val); | 63 ARRAYINLINE void array_set(const struct array *, unsigned index_, void *val); |
60 void array_setsize(struct array *, unsigned num); | 64 void array_setsize(struct array *, unsigned num); |
61 void array_add(struct array *, void *val, unsigned *index_ret); | 65 ARRAYINLINE void array_add(struct array *, void *val, unsigned *index_ret); |
62 void array_insert(struct array *a, unsigned index_); | 66 void array_insert(struct array *a, unsigned index_); |
63 void array_remove(struct array *a, unsigned index_); | 67 void array_remove(struct array *a, unsigned index_); |
64 | 68 |
65 //////////////////////////////////////////////////////////// | 69 //////////////////////////////////////////////////////////// |
66 // inlining for base operations | 70 // inlining for base operations |
67 | |
68 #ifndef ARRAYINLINE | |
69 #define ARRAYINLINE __c99inline | |
70 #endif | |
71 | 71 |
72 ARRAYINLINE unsigned | 72 ARRAYINLINE unsigned |
73 array_num(const struct array *a) | 73 array_num(const struct array *a) |
74 { | 74 { |
75 return a->num; | 75 return a->num; |
104 // bits for declaring and defining typed arrays | 104 // bits for declaring and defining typed arrays |
105 | 105 |
106 /* | 106 /* |
107 * Usage: | 107 * Usage: |
108 * | 108 * |
109 * DECLARRAY_BYTYPE(foo, bar) declares "struct foo", which is | 109 * DECLARRAY_BYTYPE(foo, bar, INLINE) declares "struct foo", which is |
110 * an array of pointers to "bar", plus the operations on it. | 110 * an array of pointers to "bar", plus the operations on it. |
111 * | 111 * |
112 * DECLARRAY(foo) is equivalent to DECLARRAY_BYTYPE(fooarray, struct foo). | 112 * DECLARRAY(foo, INLINE) is equivalent to |
113 * DECLARRAY_BYTYPE(fooarray, struct foo, INLINE). | |
113 * | 114 * |
114 * DEFARRAY_BYTYPE and DEFARRAY are the same as DECLARRAY except that | 115 * DEFARRAY_BYTYPE and DEFARRAY are the same as DECLARRAY except that |
115 * they define the operations, and both take an extra argument INLINE. | 116 * they define the operations. |
116 * For C99 this should be INLINE in header files and empty in the | 117 * |
117 * master source file, the same as the usage of ARRAYINLINE above and | 118 * The argument INLINE can be used as follows: |
118 * in array.c. | 119 * |
119 * | 120 * 1. For no inlining: |
120 * Example usage in e.g. item.h of some game: | 121 * In foo.h: |
121 * | 122 * DECLARRAY(foo, ); |
122 * DECLARRAY_BYTYPE(stringarray, char); | 123 * In foo.c: |
123 * DECLARRAY(potion); | 124 * DEFARRAY(foo, ); |
124 * DECLARRAY(sword); | 125 * |
125 * | 126 * 2. To be file-static: |
126 * #ifndef ITEMINLINE | 127 * In foo.c: |
127 * #define ITEMINLINE INLINE | 128 * DECLARRAY(foo, static); |
128 * #endif | 129 * DEFARRAY(foo, static); |
129 * | 130 * |
130 * DEFARRAY_BYTYPE(stringarray, char, ITEMINLINE); | 131 * 3. To inline using C99: |
131 * DEFARRAY(potion, ITEMINLINE); | 132 * In foo.h: |
132 * DEFARRAY(sword, ITEMINLINE); | 133 * DECLARRAY(foo, inline); |
133 * | 134 * DEFARRAY(foo, inline); |
134 * Then item.c would do "#define ITEMINLINE" before including item.h. | 135 * |
136 * 4. To inline with old gcc: | |
137 * In foo.h: | |
138 * #ifndef FOO_INLINE | |
139 * #define FOO_INLINE extern inline | |
140 * #endif | |
141 * DECLARRAY(foo, ); | |
142 * DEFARRAY(foo, FOO_INLINE); | |
143 * In foo.c: | |
144 * #define FOO_INLINE | |
145 * #include "foo.h" | |
146 * | |
147 * 5. To inline such that it works both with old gcc and C99: | |
148 * In foo.h: | |
149 * #ifndef FOO_INLINE | |
150 * #define FOO_INLINE extern inline | |
151 * #endif | |
152 * DECLARRAY(foo, FOO_INLINE); | |
153 * DEFARRAY(foo, FOO_INLINE); | |
154 * In foo.c: | |
155 * #define FOO_INLINE | |
156 * #include "foo.h" | |
157 * | |
158 * The mechanism in case (4) ensures that an externally linkable | |
159 * definition exists. | |
135 */ | 160 */ |
136 | 161 |
137 #define DECLARRAY_BYTYPE(ARRAY, T) \ | 162 #define DECLARRAY_BYTYPE(ARRAY, T, INLINE) \ |
138 struct ARRAY { \ | 163 struct ARRAY { \ |
139 struct array arr; \ | 164 struct array arr; \ |
140 }; \ | 165 }; \ |
141 \ | 166 \ |
142 struct ARRAY *ARRAY##_create(void); \ | 167 INLINE struct ARRAY *ARRAY##_create(void); \ |
143 void ARRAY##_destroy(struct ARRAY *a); \ | 168 INLINE void ARRAY##_destroy(struct ARRAY *a); \ |
144 void ARRAY##_init(struct ARRAY *a); \ | 169 INLINE void ARRAY##_init(struct ARRAY *a); \ |
145 void ARRAY##_cleanup(struct ARRAY *a); \ | 170 INLINE void ARRAY##_cleanup(struct ARRAY *a); \ |
146 unsigned ARRAY##_num(const struct ARRAY *a); \ | 171 INLINE unsigned ARRAY##_num(const struct ARRAY *a); \ |
147 T *ARRAY##_get(const struct ARRAY *a, unsigned index_); \ | 172 INLINE T *ARRAY##_get(const struct ARRAY *a, unsigned index_); \ |
148 void ARRAY##_set(struct ARRAY *a, unsigned index_, T *val); \ | 173 INLINE void ARRAY##_set(struct ARRAY *a, unsigned index_, T *val); \ |
149 void ARRAY##_setsize(struct ARRAY *a, unsigned num); \ | 174 INLINE void ARRAY##_setsize(struct ARRAY *a, unsigned num); \ |
150 void ARRAY##_add(struct ARRAY *a, T *val, unsigned *index_ret); \ | 175 INLINE void ARRAY##_add(struct ARRAY *a, T *val, unsigned *index_ret);\ |
151 void ARRAY##_insert(struct ARRAY *a, unsigned index_); \ | 176 INLINE void ARRAY##_insert(struct ARRAY *a, unsigned index_); \ |
152 void ARRAY##_remove(struct ARRAY *a, unsigned index_) | 177 INLINE void ARRAY##_remove(struct ARRAY *a, unsigned index_) |
153 | 178 |
154 | 179 |
155 #define DEFARRAY_BYTYPE(ARRAY, T, INLINE) \ | 180 #define DEFARRAY_BYTYPE(ARRAY, T, INLINE) \ |
156 INLINE void \ | 181 INLINE void \ |
157 ARRAY##_init(struct ARRAY *a) \ | 182 ARRAY##_init(struct ARRAY *a) \ |
222 ARRAY##_remove(struct ARRAY *a, unsigned index_) \ | 247 ARRAY##_remove(struct ARRAY *a, unsigned index_) \ |
223 { \ | 248 { \ |
224 array_remove(&a->arr, index_); \ | 249 array_remove(&a->arr, index_); \ |
225 } | 250 } |
226 | 251 |
227 #define DECLARRAY(T) DECLARRAY_BYTYPE(T##array, struct T) | 252 #define DECLARRAY(T, INLINE) DECLARRAY_BYTYPE(T##array, struct T, INLINE) |
228 #define DEFARRAY(T, INLINE) DEFARRAY_BYTYPE(T##array, struct T, INLINE) | 253 #define DEFARRAY(T, INLINE) DEFARRAY_BYTYPE(T##array, struct T, INLINE) |
229 | 254 |
230 #define DESTROYALL_ARRAY(T, INLINE) \ | 255 #define DESTROYALL_ARRAY(T, INLINE) \ |
231 void T##array_destroyall(struct T##array *arr); \ | 256 void T##array_destroyall(struct T##array *arr); \ |
232 \ | 257 \ |
246 | 271 |
247 | 272 |
248 //////////////////////////////////////////////////////////// | 273 //////////////////////////////////////////////////////////// |
249 // basic array types | 274 // basic array types |
250 | 275 |
251 DECLARRAY_BYTYPE(stringarray, char); | 276 DECLARRAY_BYTYPE(stringarray, char, ARRAYINLINE); |
252 DEFARRAY_BYTYPE(stringarray, char, ARRAYINLINE); | 277 DEFARRAY_BYTYPE(stringarray, char, ARRAYINLINE); |
253 | 278 |
254 #endif /* ARRAY_H */ | 279 #endif /* ARRAY_H */ |