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. |
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:
OBJECT_iterator_has_more
- returns true if there is anything left to iterate overOBJECT_iterator_next
- returns next item in the collectionOBJECT_iterator_free
- destroys the iteratorYou can also use OSCAP_FOREACH convience macro.
#define OSCAP_FOR | ( | type, | |||
val, | |||||
init_val | ) | OSCAP_FOR_GENERIC(type, struct type *, val, init_val) |
Iterate over an array, given an iterator.
type | type of array elements (w/o the struct keyword) | |
val | name of an variable the member will be sequentially stored in | |
init_val | initial member value (i.e. an iterator pointing to the start element) |
#define OSCAP_FOR_GENERIC | ( | itype, | |||
vtype, | |||||
val, | |||||
init_val | ) |
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).
val | name of an variable the string will be sequentially stored in | |
init_val | initial member value (i.e. an iterator pointing to the start element) | |
code | code 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.
val | name of an variable the member will be sequentially stored in | |
init_val | initial member value (i.e. an iterator pointing to the start element) |
#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.
type | type of array elements (w/o the struct keyword) | |
val | name of an variable the member will be sequentially stored in | |
init_val | initial member value (i.e. an iterator pointing to the start element) | |
code | code to be executed for each element the iterator hits |
#define OSCAP_FOREACH_GENERIC | ( | itype, | |||
vtype, | |||||
val, | |||||
init_val, | |||||
code | ) |
{ \ 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.
val | name of an variable the string will be sequentially stored in | |
init_val | initial member value (i.e. an iterator pointing to the start element) | |
code | code to be executed for each string the iterator hits |