dd23a35186cba6518c8d6d2d937ce1dcf42ffa93
[apps/madmutt.git] / array.h
1 /*
2  *  This program is free software; you can redistribute it and/or modify
3  *  it under the terms of the GNU General Public License as published by
4  *  the Free Software Foundation; either version 2 of the License, or (at
5  *  your option) any later version.
6  *
7  *  This program is distributed in the hope that it will be useful, but
8  *  WITHOUT ANY WARRANTY; without even the implied warranty of
9  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10  *  General Public License for more details.
11  *
12  *  You should have received a copy of the GNU General Public License
13  *  along with this program; if not, write to the Free Software
14  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
15  *  MA 02110-1301, USA.
16  *
17  *  Copyright © 2006 Pierre Habouzit
18  */
19
20 #ifndef MUTT_LIB_LIB_ARRAY_H
21 #define MUTT_LIB_LIB_ARRAY_H
22
23 #define DO_ARRAY_TYPE(type, prefix)            \
24     typedef struct prefix##_array {            \
25         type **arr;                            \
26         ssize_t len;                           \
27         ssize_t size;                          \
28     } prefix##_array
29
30 #define DO_ARRAY_FUNCS(type, prefix, dtor)                                    \
31                                                                               \
32     static inline void prefix##_array_init(prefix##_array *array) {           \
33         p_clear(array, 1);                                                    \
34     }                                                                         \
35     static inline void                                                        \
36     prefix##_array_wipe(prefix##_array *array) {                              \
37         while (array->len) {                                                  \
38             dtor(&array->arr[--array->len]);                                  \
39         }                                                                     \
40         p_delete(&array->arr);                                                \
41     }                                                                         \
42                                                                               \
43     static inline void                                                        \
44     prefix##_array_insert(prefix##_array *array, ssize_t pos, type *item) {   \
45         __array_insert((__array *)array, pos, (void*)item);                   \
46     }                                                                         \
47     static inline void                                                        \
48     prefix##_array_append(prefix##_array *array, type *item) {                \
49         __array_insert((__array *)array, array->len, (void*)item);            \
50     }                                                                         \
51     static inline type *                                                      \
52     prefix##_array_take(prefix##_array *array, ssize_t pos) {                 \
53         return (type *)__array_take((__array *)array, pos);                   \
54     }
55
56 DO_ARRAY_TYPE(void, _);
57 void __array_insert(__array *array, ssize_t pos, void *item);
58 void *__array_take(__array *array, ssize_t pos);
59
60 DO_ARRAY_TYPE(char *, string);
61 DO_ARRAY_FUNCS(char *, string, p_delete);
62
63 #endif /* MUTT_LIB_LIB_ARRAY_H */