-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitplus_foreach.h
40 lines (35 loc) · 1.42 KB
/
itplus_foreach.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* @file
* Definition of the `foreach` macro.
*/
#ifndef LIB_ITPLUS_FOREACH_UTILS_H
#define LIB_ITPLUS_FOREACH_UTILS_H
#include "itplus_macro_utils.h"
#define UNIQVAR(x) ITPL_CONCAT(ITPL_CONCAT(x, _4x2_), __LINE__) /* "Unique" variable name */
/**
* @def foreach(T, x, it)
* @brief Iterate through given iterable and store each element in `x`
*
* # Example
*
* @code
* Iterable(int) it = ...;
*
* foreach (int, element, it) {
* printf("%d\n", element);
* }
* @endcode
*
* @param T Type of the elements the iterable yields.
* @param x The variable name to store each element in. Available only inside the loop.
* @param it The iterable to iterate over. This will be consumed.
*
* @note `it` must not be an unevaluated expression. Otherwise, it will be evaluated multiple times in this macro.
* @note This macro cannot be used multiple times *in the same line*. Due to naming conflicts from an implicitly defined
* variable.
*/
#define foreach(T, x, it) \
Maybe(T) UNIQVAR(res) = (it).tc->next((it).self); \
for (T x = from_just_(UNIQVAR(res)); is_just(UNIQVAR(res)); \
UNIQVAR(res) = (it).tc->next((it).self), x = from_just_(UNIQVAR(res)))
#endif /* !LIB_ITPLUS_FOREACH_UTILS_H */