1 /*	$NetBSD: list.h,v 1.3 2014/12/10 04:38:03 christos Exp $	*/
2 
3 /*
4  * Automated Testing Framework (atf)
5  *
6  * Copyright (c) 2008 The NetBSD Foundation, Inc.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
19  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
20  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
23  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
27  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
29  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #if !defined(ATF_C_LIST_H)
33 #define ATF_C_LIST_H
34 
35 #include <stdarg.h>
36 #include <stdbool.h>
37 #include <stddef.h>
38 
39 #include <atf-c/error_fwd.h>
40 
41 /* ---------------------------------------------------------------------
42  * The "atf_list_citer" type.
43  * --------------------------------------------------------------------- */
44 
45 struct atf_list_citer {
46     const struct atf_list *m_list;
47     const void *m_entry;
48 };
49 typedef struct atf_list_citer atf_list_citer_t;
50 
51 /* Getters. */
52 const void *atf_list_citer_data(const atf_list_citer_t);
53 atf_list_citer_t atf_list_citer_next(const atf_list_citer_t);
54 
55 /* Operators. */
56 bool atf_equal_list_citer_list_citer(const atf_list_citer_t,
57                                      const atf_list_citer_t);
58 
59 /* ---------------------------------------------------------------------
60  * The "atf_list_iter" type.
61  * --------------------------------------------------------------------- */
62 
63 struct atf_list_iter {
64     struct atf_list *m_list;
65     void *m_entry;
66 };
67 typedef struct atf_list_iter atf_list_iter_t;
68 
69 /* Getters. */
70 void *atf_list_iter_data(const atf_list_iter_t);
71 atf_list_iter_t atf_list_iter_next(const atf_list_iter_t);
72 
73 /* Operators. */
74 bool atf_equal_list_iter_list_iter(const atf_list_iter_t,
75                                    const atf_list_iter_t);
76 
77 /* ---------------------------------------------------------------------
78  * The "atf_list" type.
79  * --------------------------------------------------------------------- */
80 
81 struct atf_list {
82     void *m_begin;
83     void *m_end;
84 
85     size_t m_size;
86 };
87 typedef struct atf_list atf_list_t;
88 
89 /* Constructors and destructors */
90 atf_error_t atf_list_init(atf_list_t *);
91 void atf_list_fini(atf_list_t *);
92 
93 /* Getters. */
94 atf_list_iter_t atf_list_begin(atf_list_t *);
95 atf_list_citer_t atf_list_begin_c(const atf_list_t *);
96 atf_list_iter_t atf_list_end(atf_list_t *);
97 atf_list_citer_t atf_list_end_c(const atf_list_t *);
98 void *atf_list_index(atf_list_t *, const size_t);
99 const void *atf_list_index_c(const atf_list_t *, const size_t);
100 size_t atf_list_size(const atf_list_t *);
101 char **atf_list_to_charpp(const atf_list_t *);
102 
103 /* Modifiers. */
104 atf_error_t atf_list_append(atf_list_t *, void *, bool);
105 void atf_list_append_list(atf_list_t *, atf_list_t *);
106 
107 /* Macros. */
108 #define atf_list_for_each(iter, list) \
109     for (iter = atf_list_begin(list); \
110          !atf_equal_list_iter_list_iter((iter), atf_list_end(list)); \
111          iter = atf_list_iter_next(iter))
112 #define atf_list_for_each_c(iter, list) \
113     for (iter = atf_list_begin_c(list); \
114          !atf_equal_list_citer_list_citer((iter), atf_list_end_c(list)); \
115          iter = atf_list_citer_next(iter))
116 
117 #endif /* ATF_C_LIST_H */
118