tor  0.4.1.0-alpha-dev
smartlist_foreach.h
Go to the documentation of this file.
1 /* Copyright (c) 2003-2004, Roger Dingledine
2  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3  * Copyright (c) 2007-2019, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
5 
11 #ifndef TOR_SMARTLIST_FOREACH_H
12 #define TOR_SMARTLIST_FOREACH_H
13 
44 /* Note: these macros use token pasting, and reach into smartlist internals.
45  * This can make them a little daunting. Here's the approximate unpacking of
46  * the above examples, for entertainment value:
47  *
48  * <pre>
49  * smartlist_t *list = smartlist_split("A:B:C", ":", 0, 0);
50  * {
51  * int cp_sl_idx, cp_sl_len = smartlist_len(list);
52  * char *cp;
53  * for (cp_sl_idx = 0; cp_sl_idx < cp_sl_len; ++cp_sl_idx) {
54  * cp = smartlist_get(list, cp_sl_idx);
55  * printf("%d: %s\n", cp_sl_idx, cp);
56  * tor_free(cp);
57  * }
58  * }
59  * smartlist_free(list);
60  * </pre>
61  *
62  * <pre>
63  * {
64  * int cp_sl_idx, cp_sl_len = smartlist_len(list);
65  * char *cp;
66  * for (cp_sl_idx = 0; cp_sl_idx < cp_sl_len; ++cp_sl_idx) {
67  * cp = smartlist_get(list, cp_sl_idx);
68  * if (!strcmp(cp, "junk")) {
69  * tor_free(cp);
70  * smartlist_del(list, cp_sl_idx);
71  * --cp_sl_idx;
72  * --cp_sl_len;
73  * }
74  * }
75  * }
76  * </pre>
77  */
78 #define SMARTLIST_FOREACH_BEGIN(sl, type, var) \
79  STMT_BEGIN \
80  int var ## _sl_idx, var ## _sl_len=(sl)->num_used; \
81  type var; \
82  for (var ## _sl_idx = 0; var ## _sl_idx < var ## _sl_len; \
83  ++var ## _sl_idx) { \
84  var = (sl)->list[var ## _sl_idx];
85 
91 #define SMARTLIST_FOREACH_REVERSE_BEGIN(sl, type, var) \
92  STMT_BEGIN \
93  int var ## _sl_idx, var ## _sl_len=(sl)->num_used; \
94  type var; \
95  for (var ## _sl_idx = var ## _sl_len-1; var ## _sl_idx >= 0; \
96  --var ## _sl_idx) { \
97  var = (sl)->list[var ## _sl_idx];
98 
99 #define SMARTLIST_FOREACH_END(var) \
100  var = NULL; \
101  (void) var ## _sl_idx; \
102  } STMT_END
103 
112 #define SMARTLIST_FOREACH(sl, type, var, cmd) \
113  SMARTLIST_FOREACH_BEGIN(sl,type,var) { \
114  cmd; \
115  } SMARTLIST_FOREACH_END(var)
116 
120 #define SMARTLIST_DEL_CURRENT(sl, var) \
121  STMT_BEGIN \
122  smartlist_del(sl, var ## _sl_idx); \
123  --var ## _sl_idx; \
124  --var ## _sl_len; \
125  STMT_END
126 
130 #define SMARTLIST_DEL_CURRENT_KEEPORDER(sl, var) \
131  STMT_BEGIN \
132  smartlist_del_keeporder(sl, var ## _sl_idx); \
133  --var ## _sl_idx; \
134  --var ## _sl_len; \
135  STMT_END
136 
141 #define SMARTLIST_REPLACE_CURRENT(sl, var, val) \
142  STMT_BEGIN \
143  smartlist_set(sl, var ## _sl_idx, val); \
144  STMT_END
145 
146 #endif /* !defined(TOR_SMARTLIST_FOREACH_H) */