1 /*
2 * glcontainers.h: interface to common Gnulib container helpers
3 *
4 * Copyright (C) 2019 Colin Watson.
5 *
6 * This file is part of man-db.
7 *
8 * man-db is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * man-db is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with man-db; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #ifndef MAN_GLCONTAINERS_H
24 #define MAN_GLCONTAINERS_H
25
26 #include <stdbool.h>
27 #include <stdlib.h>
28
29 #include "gl_list.h"
30 #include "gl_map.h"
31 #include "gl_set.h"
32
33 #include "mp.h"
34
35 /* These types are compatible with those required by Gnulib container
36 * initialisation.
37 */
38
39 bool string_equals (const void *s1, const void *s2);
40 size_t string_hash (const void *s);
41 void plain_free (const void *s);
42
43 /* Convenience functions. */
44
45 gl_list_t new_string_list (gl_list_implementation_t implementation,
46 bool allow_duplicates);
47 gl_map_t new_string_map (gl_map_implementation_t implementation,
48 gl_mapvalue_dispose_fn vdispose_fn);
49 gl_set_t new_string_set (gl_set_implementation_t implementation);
50
51 /* Iterator macros. */
52
53 #define GL_LIST_FOREACH(list, item) \
54 MPP_DECLARE (1, gl_list_iterator_t list##_iter = gl_list_iterator (list)) \
55 MPP_DECLARE (2, gl_list_node_t list##_node) \
56 MPP_WHILE (3, gl_list_iterator_next (&list##_iter, \
57 (const void **) &item, \
58 &list##_node)) \
59 MPP_FINALLY (4, gl_list_iterator_free (&list##_iter))
60
61 #define GL_MAP_FOREACH(map, key, value) \
62 MPP_DECLARE (1, gl_map_iterator_t map##_iter = gl_map_iterator (map)) \
63 MPP_WHILE (2, gl_map_iterator_next (&map##_iter, \
64 (const void **) &key, \
65 (const void **) &value)) \
66 MPP_FINALLY (3, gl_map_iterator_free (&map##_iter))
67
68 #endif /* MAN_GLCONTAINERS_H */