Open SCAP Library
Defines
Iterators & collections
Common

Iterators concept. More...

Defines

#define OSCAP_FOREACH_GENERIC(itype, vtype, val, init_val, code)
 Iterate over an array, given an iterator.
#define OSCAP_FOREACH(type, val, init_val, code)   OSCAP_FOREACH_GENERIC(type, struct type *, val, init_val, code)
 Iterate over an array, given an iterator.
#define OSCAP_FOREACH_STR(val, init_val, code)   OSCAP_FOREACH_GENERIC(oscap_string, const char *, val, init_val, code)
 Iterate over an array of strings, given an iterator.
#define OSCAP_FOR_GENERIC(itype, vtype, val, init_val)
 Iterate over an array, given an iterator.
#define OSCAP_FOR(type, val, init_val)   OSCAP_FOR_GENERIC(type, struct type *, val, init_val)
 Iterate over an array, given an iterator.
#define OSCAP_FOR_STR(val, init_val)   OSCAP_FOR_GENERIC(oscap_string, const char *, val, init_val)
 Iterate over an array of strings, given an iterator.

Detailed Description

Iterators concept.

Any iterator name takes a form of struct OBJECT_iterator, where OBJECT is a name of particular datatype the iterator iterates over.

Each iterator type defines several manipulation functions, namely:

You can also use OSCAP_FOREACH convience macro.


Define Documentation

#define OSCAP_FOR (   type,
  val,
  init_val 
)    OSCAP_FOR_GENERIC(type, struct type *, val, init_val)

Iterate over an array, given an iterator.

Parameters:
typetype of array elements (w/o the struct keyword)
valname of an variable the member will be sequentially stored in
init_valinitial member value (i.e. an iterator pointing to the start element)
See also:
OSCAP_FOR_GENERIC
#define OSCAP_FOR_GENERIC (   itype,
  vtype,
  val,
  init_val 
)
Value:
vtype val = NULL; struct itype##_iterator *val##_iter = (init_val); \
    while (itype##_iterator_has_more(val##_iter)                        \
            ? (val = itype##_iterator_next(val##_iter), true)           \
            : (itype##_iterator_free(val##_iter), val##_iter = NULL, false))

Iterate over an array, given an iterator.

It is generally not safe to use break, return or goto inside the loop (iterator wouldn't be properly freed otherwise). Two variables, named VAL and VAL_iter (substitute VAL for actual macro argument) will be added to current variable scope. You can free the iterator explicitly after previous unusual escape from the loop (e.g. using break).

Parameters:
valname of an variable the string will be sequentially stored in
init_valinitial member value (i.e. an iterator pointing to the start element)
codecode to be executed for each string the iterator hits
#define OSCAP_FOR_STR (   val,
  init_val 
)    OSCAP_FOR_GENERIC(oscap_string, const char *, val, init_val)

Iterate over an array of strings, given an iterator.

Parameters:
valname of an variable the member will be sequentially stored in
init_valinitial member value (i.e. an iterator pointing to the start element)
See also:
OSCAP_FOR_GENERIC
#define OSCAP_FOREACH (   type,
  val,
  init_val,
  code 
)    OSCAP_FOREACH_GENERIC(type, struct type *, val, init_val, code)

Iterate over an array, given an iterator.

Parameters:
typetype of array elements (w/o the struct keyword)
valname of an variable the member will be sequentially stored in
init_valinitial member value (i.e. an iterator pointing to the start element)
codecode to be executed for each element the iterator hits
See also:
OSCAP_FOREACH_GENERIC
#define OSCAP_FOREACH_GENERIC (   itype,
  vtype,
  val,
  init_val,
  code 
)
Value:
{                                                            \
        struct itype##_iterator *val##_iter = (init_val);        \
        vtype val;                                               \
        while (itype##_iterator_has_more(val##_iter)) {          \
            val = itype##_iterator_next(val##_iter);             \
            code                                                 \
        }                                                        \
        itype##_iterator_free(val##_iter);                       \
    }

Iterate over an array, given an iterator.

Execute code for each array member stored in val. It is NOT safe to use return or goto inside of the code, the iterator would not be freed properly.

#define OSCAP_FOREACH_STR (   val,
  init_val,
  code 
)    OSCAP_FOREACH_GENERIC(oscap_string, const char *, val, init_val, code)

Iterate over an array of strings, given an iterator.

Parameters:
valname of an variable the string will be sequentially stored in
init_valinitial member value (i.e. an iterator pointing to the start element)
codecode to be executed for each string the iterator hits
See also:
OSCAP_FOREACH_GENERIC