Tor 0.4.9.0-alpha-dev
Macros
smartlist_foreach.h File Reference

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)
 

Detailed Description

Macros for iterating over the elements of a smartlist_t.

Definition in file smartlist_foreach.h.

Macro Definition Documentation

◆ SMARTLIST_DEL_CURRENT

#define SMARTLIST_DEL_CURRENT (   sl,
  var 
)
Value:
smartlist_del(sl, var ## _sl_idx); \
--var ## _sl_idx; \
--var ## _sl_len; \
STMT_END
void smartlist_del(smartlist_t *sl, int idx)

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.

◆ SMARTLIST_DEL_CURRENT_KEEPORDER

#define SMARTLIST_DEL_CURRENT_KEEPORDER (   sl,
  var 
)
Value:
smartlist_del_keeporder(sl, var ## _sl_idx); \
--var ## _sl_idx; \
--var ## _sl_len; \
STMT_END
void smartlist_del_keeporder(smartlist_t *sl, int idx)

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.

◆ SMARTLIST_FOREACH

#define SMARTLIST_FOREACH (   sl,
  type,
  var,
  cmd 
)
Value:
SMARTLIST_FOREACH_BEGIN(sl,type,var) { \
cmd; \
} SMARTLIST_FOREACH_END(var)
#define SMARTLIST_FOREACH_BEGIN(sl, type, var)

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.

◆ SMARTLIST_FOREACH_BEGIN

#define SMARTLIST_FOREACH_BEGIN (   sl,
  type,
  var 
)
Value:
STMT_BEGIN \
int var ## _sl_idx, var ## _sl_len=(sl)->num_used; \
type var; \
for (var ## _sl_idx = 0; var ## _sl_idx < var ## _sl_len; \
++var ## _sl_idx) { \
var = (sl)->list[var ## _sl_idx];

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.

◆ SMARTLIST_FOREACH_END

#define SMARTLIST_FOREACH_END (   var)
Value:
var = NULL; \
(void) var ## _sl_idx; \
} STMT_END

Definition at line 99 of file smartlist_foreach.h.

◆ SMARTLIST_FOREACH_REVERSE_BEGIN

#define SMARTLIST_FOREACH_REVERSE_BEGIN (   sl,
  type,
  var 
)
Value:
STMT_BEGIN \
int var ## _sl_idx, var ## _sl_len=(sl)->num_used; \
type var; \
for (var ## _sl_idx = var ## _sl_len-1; var ## _sl_idx >= 0; \
--var ## _sl_idx) { \
var = (sl)->list[var ## _sl_idx];

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.

◆ SMARTLIST_REPLACE_CURRENT

#define SMARTLIST_REPLACE_CURRENT (   sl,
  var,
  val 
)
Value:
STMT_BEGIN \
smartlist_set(sl, var ## _sl_idx, val); \
STMT_END

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.