CLibs
Loading...
Searching...
No Matches
foreach.h File Reference
#include <stdlib.h>
#include <string.h>

Go to the source code of this file.

Macros

#define foreach_uni(TYPE, ITEM_NAME, INITIALIZER, ACCESSOR, SIZE)
#define foreach_arr(ITEM_TYPE, ITEM_NAME, ARRAY, COUNT)
#define foreach_str(ITEM_NAME, STRING)
#define foreach_ls(TYPE, ITEM_NAME, LIST)
#define foreach_set(ENTRY_NAME, SET)
#define foreach_que(ENTRY_NAME, QUEUE)

Macro Definition Documentation

◆ foreach_arr

#define foreach_arr ( ITEM_TYPE,
ITEM_NAME,
ARRAY,
COUNT )
Value:
foreach_helper_init( COUNT, ITEM_NAME ) \
foreach_helper_assign( ITEM_TYPE, ITEM_NAME, ( ARRAY )[ 0 ], \
( ARRAY )[ foreach_index_##ITEM_NAME ] )

Iterates over an array.

Stores each array member in a new variable (only visible in the scope of the foreach loop).

Parameters
ITEM_TYPEC-keyword (or typedef/…) of the type of the array items and the new variable
ITEM_NAMEname of the new variable
ARRAYarray of type TYPE[ COUNT ]
COUNTnumber of elements in array

◆ foreach_ls

#define foreach_ls ( TYPE,
ITEM_NAME,
LIST )
Value:
foreach_helper_init( list_size( LIST ), ITEM_NAME ) \
foreach_helper_assign( TYPE, ITEM_NAME, list_fetch( LIST, 0, TYPE ), \
list_fetch( LIST, foreach_index_##ITEM_NAME, TYPE ) )
size_t list_size(const struct dynamic_array *)
Returns number of elements in the list.
#define list_fetch(LIST, IDX, TYPE)
Definition dynarr.h:99

Iterates over a list.

Stores each list member in a new variable.

Parameters
TYPEtype of the new variable and items in the list
ITEM_NAMEname of the new variable
LISTa valid List *
Attention
Requires previous definition of CLIBS_FOREACH_LIST or CLIBS_DYNAMIC_ARRAY_H. The latter is defined when including src/structs/dynarr.h.

◆ foreach_que

#define foreach_que ( ENTRY_NAME,
QUEUE )
Value:
for ( const struct queue_node *ENTRY_NAME = queue__iterator_get_head( QUEUE ); \
ENTRY_NAME != NULL; ENTRY_NAME = queue__iterator_get_next( ENTRY_NAME ) )

Iterates over a queue.

"Retrieves" a const struct queue_node struct (see src/structs/queue.h); use queue_node_get_data() to retrieve data from the item

Example

foreach_que( queue )
{
const int data = deref_as( int, queue_node_get_data( entry ) );
// ...
}
#define foreach_que(ENTRY_NAME, QUEUE)
Definition foreach.h:128
#define deref_as(type, var)
Dereferences a pointer as if it was a pointer to type
Definition pointer_utils.h:53
const void * queue_node_get_data(const struct queue_node *)
Parameters
QUEUEvalid struct fifo_queue *
Attention
  1. Do not reassign ENTRY_NAME variable.
  2. Requires previous definition of CLIBS_FOREACH_QUEUE or CLIBS_QUEUE_H. The latter is defined when including src/structs/queue.h.

◆ foreach_set

#define foreach_set ( ENTRY_NAME,
SET )
Value:
for ( SetEnumeratedEntry ENTRY_NAME = set_get_next( ( SET ), -1 ); \
ENTRY_NAME.index >= 0; \
ENTRY_NAME = set_get_next( ( SET ), ENTRY_NAME.index ) )
SetEnumeratedEntry set_get_next(const Set *, int64_t index_last)
Definition set.h:169
int64_t index
Definition set.h:171

Iterates over a set.

Retrieves a const SetEnumeratedEntry struct (see src/structs/set.h); in short, SetEnumeratedEntry contains a field struct set_item *item with the desired data

Parameters
SETa valid Set *
Attention
  1. Do not modify the contents of the SetEnumeratedEntry.
  2. Requires previous definition of CLIBS_FOREACH_SET or CLIBS_SETS_H. The latter is defined when including src/structs/set.h.

◆ foreach_str

#define foreach_str ( ITEM_NAME,
STRING )
Value:
foreach_helper_init( strlen( STRING ), ITEM_NAME ) \
foreach_helper_assign( const char, ITEM_NAME, ( STRING )[ 0 ], \
( STRING )[ foreach_index_##ITEM_NAME ] )

Iterates over a string.

Stores each character of the string in a new variable of type const char.

Parameters
ITEM_NAMEname of the new variable
STRINGstring

◆ foreach_uni

#define foreach_uni ( TYPE,
ITEM_NAME,
INITIALIZER,
ACCESSOR,
SIZE )
Value:
foreach_helper_init( SIZE, ITEM_NAME ) \
foreach_helper_assign( TYPE, ITEM_NAME, INITIALIZER, ACCESSOR )

Universal foreach macro

Parameters
TYPEtype of the item
ITEM_NAMEname of the variable created and assigned in the loop
INITIALIZERruns on the first pass through the loop
ACCESSORruns every other time (index variable is foreach_index_ITEM_NAME)
SIZEnumber of elements in the structure