]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/iter.qh
lib/iter: FOREACH_ENTITY
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / iter.qh
1 #ifndef ITER_H
2 #define ITER_H
3
4 #define FOREACH_ARRAY(arr, start, end, cond, body) \
5         do \
6         { \
7                 for (int i = start; i < end; ++i) \
8                 { \
9                         const noref entity it = arr[i]; \
10                         if (cond) { body } \
11                 } \
12         } \
13         while (0)
14
15 #define FOREACH_LIST(list, next, cond, body) \
16         do \
17         { \
18                 int i = 0; \
19                 for (entity it = list##_first; it; (it = it.next, ++i)) \
20                 { \
21                         if (cond) { body } \
22                 } \
23         } \
24         while (0)
25
26 #define FOREACH_WORD(words, cond, body) \
27         do \
28         { \
29                 string _words = words; \
30                 int i = 0; \
31                 for (string _it; (_it = car(_words)); (_words = cdr(_words), ++i)) \
32                 { \
33                         const noref string it = _it; \
34                         if (cond) { body } \
35                 } \
36         } \
37         while (0)
38
39 #if defined(CSQC)
40         entity(.entity fld, entity match, .entity tofield) findchainentity_tofield = #403;
41 #elif defined(SVQC)
42         entity(.entity fld, entity match, .entity tofield) findchainentity_tofield = #403;
43 #elif defined(MENUQC)
44         entity(.entity fld, entity match, .entity tofield) findchainentity_tofield = #27;
45 #endif
46
47 .entity _FOREACH_ENTITY_fld;
48 .entity _FOREACH_ENTITY_next;
49
50 #define FOREACH_ENTITY_UNORDERED(cond, body) \
51         do { \
52                 int i = 0; \
53                 for (entity it = findchainentity_tofield(_FOREACH_ENTITY_fld, NULL, _FOREACH_ENTITY_next); it; (it = it._FOREACH_ENTITY_next, ++i)) \
54                 { \
55                         if (cond) { body } \
56                 } \
57         } \
58         while (0)
59
60 #define FOREACH_ENTITY_ORDERED(cond, body) \
61         do { \
62                 int i = 0; \
63                 for (entity it = NULL; (it = nextent(it)); ++i) \
64                 { \
65                         if (cond) { body } \
66                 } \
67         } \
68         while (0)
69
70 #define FOREACH_ENTITY(cond, body) FOREACH_ENTITY_UNORDERED(cond, body)
71
72 #define FOREACH(list, cond, body) FOREACH_LIST(list, enemy, cond, body)
73
74 #endif