Tor
0.4.7.0-alpha-dev
|
Macros for iterating over the elements of a smartlist_t. More...
Go to the source code of this file.
Macros | |
#define | SMARTLIST_FOREACH_BEGIN(sl, type, var) |
#define | SMARTLIST_FOREACH_REVERSE_BEGIN(sl, type, var) |
#define | SMARTLIST_FOREACH_END(var) |
#define | SMARTLIST_FOREACH(sl, type, var, cmd) |
#define | SMARTLIST_DEL_CURRENT(sl, var) |
#define | SMARTLIST_DEL_CURRENT_KEEPORDER(sl, var) |
#define | SMARTLIST_REPLACE_CURRENT(sl, var, val) |
Macros for iterating over the elements of a smartlist_t.
Definition in file smartlist_foreach.h.
#define SMARTLIST_DEL_CURRENT | ( | sl, | |
var | |||
) |
Helper: While in a SMARTLIST_FOREACH loop over the list sl indexed with the variable var, remove the current element in a way that won't confuse the loop.
Definition at line 120 of file smartlist_foreach.h.
#define SMARTLIST_DEL_CURRENT_KEEPORDER | ( | sl, | |
var | |||
) |
Helper: While in a SMARTLIST_FOREACH loop over the list sl indexed with the variable var, remove the current element in a way that won't confuse the loop.
Definition at line 130 of file smartlist_foreach.h.
#define SMARTLIST_FOREACH | ( | sl, | |
type, | |||
var, | |||
cmd | |||
) |
An alias for SMARTLIST_FOREACH_BEGIN and SMARTLIST_FOREACH_END, using cmd as the loop body. This wrapper is here for convenience with very short loops.
By convention, we do not use this for loops which nest, or for loops over 10 lines or so. Use SMARTLIST_FOREACH_{BEGIN,END} for those.
Definition at line 112 of file smartlist_foreach.h.
#define SMARTLIST_FOREACH_BEGIN | ( | sl, | |
type, | |||
var | |||
) |
Iterate over the items in a smartlist sl, in order. For each item, assign it to a new local variable of type type named var, and execute the statements inside the loop body. Inside the loop, the loop index can be accessed as var_sl_idx and the length of the list can be accessed as var_sl_len.
NOTE: Do not change the length of the list while the loop is in progress, unless you adjust the _sl_len variable correspondingly. See second example below.
Example use:
smartlist_t *list = smartlist_split("A:B:C", ":", 0, 0); SMARTLIST_FOREACH_BEGIN(list, char *, cp) { printf("%d: %s\n", cp_sl_idx, cp); tor_free(cp); } SMARTLIST_FOREACH_END(cp); smartlist_free(list);
Example use (advanced):
SMARTLIST_FOREACH_BEGIN(list, char *, cp) { if (!strcmp(cp, "junk")) { tor_free(cp); SMARTLIST_DEL_CURRENT(list, cp); } } SMARTLIST_FOREACH_END(cp);
Definition at line 78 of file smartlist_foreach.h.
#define SMARTLIST_FOREACH_END | ( | var | ) |
Definition at line 99 of file smartlist_foreach.h.
#define SMARTLIST_FOREACH_REVERSE_BEGIN | ( | sl, | |
type, | |||
var | |||
) |
Iterates over the items in smartlist sl in reverse order, similar to SMARTLIST_FOREACH_BEGIN
NOTE: This macro is incompatible with SMARTLIST_DEL_CURRENT.
Definition at line 91 of file smartlist_foreach.h.
#define SMARTLIST_REPLACE_CURRENT | ( | sl, | |
var, | |||
val | |||
) |
Helper: While in a SMARTLIST_FOREACH loop over the list sl indexed with the variable var, replace the current element with val. Does not deallocate the current value of var.
Definition at line 141 of file smartlist_foreach.h.