foreach: allow a single argument to foreach_int() and foreach_ptr().
[ccan] / ccan / foreach / foreach.h
1 #ifndef CCAN_FOREACH_H
2 #define CCAN_FOREACH_H
3 #include "config.h"
4 #include <stddef.h>
5 #include <assert.h>
6 #include <stdbool.h>
7
8 #if HAVE_COMPOUND_LITERALS
9 #if HAVE_FOR_LOOP_DECLARATION
10 /**
11  * foreach_int - iterate over a fixed series of integers
12  * @i: the int-compatible iteration variable
13  * ...: one or more integer-compatible values
14  *
15  * This is a convenient wrapper function for setting a variable to one or
16  * more explicit values in turn.  continue and break work as expected.
17  *
18  * Example:
19  *      int i;
20  *      foreach_int(i, 0, -1, 100, 0, -99) {
21  *              printf("i is %i\n", i);
22  *      }
23  */
24 #define foreach_int(i, ...)                                             \
25         for (unsigned _foreach_i = (((i) = ((int[]) { __VA_ARGS__ })[0]), 0); \
26              _foreach_i < sizeof((int[]) { __VA_ARGS__ })/sizeof(int);  \
27              (i) = ((int[]) { __VA_ARGS__, 0 })[++_foreach_i])
28
29 /**
30  * foreach_ptr - iterate over a non-NULL series of pointers
31  * @i: the pointer iteration variable
32  * ...: one or more compatible pointer values
33  *
34  * This is a convenient wrapper function for setting a variable to one
35  * or more explicit values in turn.  None of the values can be NULL;
36  * that is the termination condition (ie. @i will be NULL on
37  * completion).  continue and break work as expected.
38  *
39  * Example:
40  *      const char *p;
41  *      foreach_ptr(p, "Hello", "world") {
42  *              printf("p is %s\n", p);
43  *      }
44  */
45 #define foreach_ptr(i, ...)                                     \
46         for (unsigned _foreach_i                                \
47                      = (((i) = ((const void *[]){ __VA_ARGS__ })[0]), 0); \
48              (i);                                                       \
49              (i) = (void *)((FOREACH_TYPEOF(i)[])                       \
50                      { __VA_ARGS__, NULL})[++_foreach_i],               \
51                 _foreach_no_nullval(_foreach_i, i,                      \
52                                     ((const void *[]){ __VA_ARGS__})))
53 #else /* !HAVE_FOR_LOOP_DECLARATION */
54 /* GCC in C89 mode still has compound literals, but no for-declarations */
55 #define foreach_int(i, ...)                                             \
56         for ((i) = ((int[]){ __VA_ARGS__ })[0], _foreach_iter_init(&(i)); \
57              _foreach_iter(&(i)) < sizeof((int[]) { __VA_ARGS__ })      \
58                            / sizeof(int);                               \
59              (i) = (int[]) { __VA_ARGS__, 0 }[_foreach_iter_inc(&(i))])
60
61 #define foreach_ptr(i, ...)                                             \
62         for ((i) = ((FOREACH_TYPEOF(i)[]){ __VA_ARGS__ })[0],           \
63                      _foreach_iter_init(&(i));                          \
64              (i);                                                       \
65              (i) = (void *)((FOREACH_TYPEOF(i)[]){ __VA_ARGS__, NULL }) \
66                      [_foreach_iter_inc(&(i))],                         \
67                  _foreach_no_nullval(_foreach_iter(&(i)), i,            \
68                                      ((void *[]){ __VA_ARGS__})))
69
70 void _foreach_iter_init(const void *i);
71 unsigned int _foreach_iter(const void *i);
72 unsigned int _foreach_iter_inc(const void *i);
73
74 #endif /* !HAVE_FOR_LOOP_DECLARATION */
75
76 /* Make sure they don't put NULL values into array! */
77 #define _foreach_no_nullval(i, p, arr)                          \
78         assert((i) >= sizeof(arr)/sizeof(arr[0]) || (p))
79
80 #if HAVE_TYPEOF
81 #define FOREACH_TYPEOF(i) __typeof__(i)
82 #else
83 #define FOREACH_TYPEOF(i) const void *
84 #endif
85
86 #else /* !HAVE_COMPOUND_LITERALS */
87
88 /* No compound literals, but it's still (just) possible. */
89 #define foreach_int(i, ...)                                             \
90         for (i = _foreach_intval_init(&(i), __VA_ARGS__, _foreach_term); \
91              !_foreach_intval_done(&i);                                 \
92              i = _foreach_intval_next(&(i), __VA_ARGS__, _foreach_term))
93
94 #define foreach_ptr(i, ...)                                             \
95         for (i = _foreach_ptrval_init(&(i), __VA_ARGS__, NULL);         \
96              (i);                                                       \
97              i = _foreach_ptrval_next(&(i), __VA_ARGS__, NULL))
98
99 extern int _foreach_term;
100 int _foreach_intval_init(const void *i, int val, ...);
101 bool _foreach_intval_done(const void *i);
102 int _foreach_intval_next(const void *i, int val, ...);
103 void *_foreach_ptrval_init(const void *i, const void *val, ...);
104 void *_foreach_ptrval_next(const void *i, const void *val, ...);
105 #endif /* !HAVE_COMPOUND_LITERALS */
106
107 #endif /* CCAN_FOREACH_H */