xref: /freebsd/contrib/atf/atf-c/detail/map.h (revision fed1ca4b719c56c930f2259d80663cd34be812bb)
1 /* Copyright (c) 2008 The NetBSD Foundation, Inc.
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
14  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
15  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
18  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  */
25 
26 #if !defined(ATF_C_DETAIL_MAP_H)
27 #define ATF_C_DETAIL_MAP_H
28 
29 #include <stdarg.h>
30 #include <stdbool.h>
31 
32 #include <atf-c/detail/list.h>
33 #include <atf-c/error_fwd.h>
34 
35 /* ---------------------------------------------------------------------
36  * The "atf_map_citer" type.
37  * --------------------------------------------------------------------- */
38 
39 struct atf_map_citer {
40     const struct atf_map *m_map;
41     const void *m_entry;
42     atf_list_citer_t m_listiter;
43 };
44 typedef struct atf_map_citer atf_map_citer_t;
45 
46 /* Getters. */
47 const char *atf_map_citer_key(const atf_map_citer_t);
48 const void *atf_map_citer_data(const atf_map_citer_t);
49 atf_map_citer_t atf_map_citer_next(const atf_map_citer_t);
50 
51 /* Operators. */
52 bool atf_equal_map_citer_map_citer(const atf_map_citer_t,
53                                    const atf_map_citer_t);
54 
55 /* ---------------------------------------------------------------------
56  * The "atf_map_iter" type.
57  * --------------------------------------------------------------------- */
58 
59 struct atf_map_iter {
60     struct atf_map *m_map;
61     void *m_entry;
62     atf_list_iter_t m_listiter;
63 };
64 typedef struct atf_map_iter atf_map_iter_t;
65 
66 /* Getters. */
67 const char *atf_map_iter_key(const atf_map_iter_t);
68 void *atf_map_iter_data(const atf_map_iter_t);
69 atf_map_iter_t atf_map_iter_next(const atf_map_iter_t);
70 
71 /* Operators. */
72 bool atf_equal_map_iter_map_iter(const atf_map_iter_t,
73                                  const atf_map_iter_t);
74 
75 /* ---------------------------------------------------------------------
76  * The "atf_map" type.
77  * --------------------------------------------------------------------- */
78 
79 /* A list-based map.  Typically very inefficient, but our maps are small
80  * enough. */
81 struct atf_map {
82     atf_list_t m_list;
83 };
84 typedef struct atf_map atf_map_t;
85 
86 /* Constructors and destructors */
87 atf_error_t atf_map_init(atf_map_t *);
88 atf_error_t atf_map_init_charpp(atf_map_t *, const char *const *);
89 void atf_map_fini(atf_map_t *);
90 
91 /* Getters. */
92 atf_map_iter_t atf_map_begin(atf_map_t *);
93 atf_map_citer_t atf_map_begin_c(const atf_map_t *);
94 atf_map_iter_t atf_map_end(atf_map_t *);
95 atf_map_citer_t atf_map_end_c(const atf_map_t *);
96 atf_map_iter_t atf_map_find(atf_map_t *, const char *);
97 atf_map_citer_t atf_map_find_c(const atf_map_t *, const char *);
98 size_t atf_map_size(const atf_map_t *);
99 char **atf_map_to_charpp(const atf_map_t *);
100 
101 /* Modifiers. */
102 atf_error_t atf_map_insert(atf_map_t *, const char *, void *, bool);
103 
104 /* Macros. */
105 #define atf_map_for_each(iter, map) \
106     for (iter = atf_map_begin(map); \
107          !atf_equal_map_iter_map_iter((iter), atf_map_end(map)); \
108          iter = atf_map_iter_next(iter))
109 #define atf_map_for_each_c(iter, map) \
110     for (iter = atf_map_begin_c(map); \
111          !atf_equal_map_citer_map_citer((iter), atf_map_end_c(map)); \
112          iter = atf_map_citer_next(iter))
113 
114 #endif /* !defined(ATF_C_DETAIL_MAP_H) */
115