]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/iter.qh
yEnc binary-to-text encoding with tests
[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         MACRO_BEGIN \
6         { \
7                 for (int i = start; i < end; ++i) \
8                 { \
9                         const noref entity it = arr[i]; \
10                         if (cond) { LAMBDA(body) } \
11                 } \
12         } MACRO_END
13
14 #define FOREACH_LIST(list, next, cond, body) \
15         MACRO_BEGIN \
16         { \
17                 int i = 0; \
18                 for (entity it = list##_first; it; (it = it.next, ++i)) \
19                 { \
20                         if (cond) { LAMBDA(body) } \
21                 } \
22         } MACRO_END
23
24 #define FOREACH_WORD(words, cond, body) \
25         MACRO_BEGIN \
26         { \
27                 string _words = words; \
28                 int i = 0; \
29                 for (string _it; (_it = car(_words)); (_words = cdr(_words), ++i)) \
30                 { \
31                         const noref string it = _it; \
32                         if (cond) { LAMBDA(body) } \
33                 } \
34         } MACRO_END
35
36 #define STRING_ITERATOR(this, s, i) \
37         string this##_s = s; \
38         int this##_i = i; \
39         MACRO_BEGIN MACRO_END
40
41 #define STRING_ITERATOR_GET(this) str2chr(this##_s, this##_i++)
42
43 #define FOREACH_CHAR(s, cond, body) \
44         MACRO_BEGIN \
45         { \
46                 STRING_ITERATOR(iter, s, 0); \
47                 int it; \
48                 while ((it = STRING_ITERATOR_GET(iter)) > 0) \
49                 { \
50                         if (cond) { LAMBDA(body) } \
51                 } \
52         } MACRO_END
53
54 #if defined(CSQC)
55         entity(.string fld, string match, .entity tofield) _findchainstring_tofield = #402;
56         entity(.entity fld, entity match, .entity tofield) findchainentity_tofield = #403;
57         entity(.float fld, float match, .entity tofield) _findchainflags_tofield = #450;
58 #elif defined(SVQC)
59         entity(.string fld, string match, .entity tofield) _findchainstring_tofield = #402;
60         entity(.entity fld, entity match, .entity tofield) findchainentity_tofield = #403;
61         entity(.float fld, float match, .entity tofield) _findchainflags_tofield = #450;
62 #elif defined(MENUQC)
63         entity(.string fld, string match, .entity tofield) _findchainstring_tofield = #26;
64         entity(.entity fld, entity match, .entity tofield) findchainentity_tofield = #27;
65         entity(.float fld, float match, .entity tofield) _findchainflags_tofield = #88;
66 #endif
67
68 .entity _FOREACH_ENTITY_fld;
69 .entity _FOREACH_ENTITY_next;
70
71 #define FOREACH_ENTITY_UNORDERED(cond, body) \
72         MACRO_BEGIN { \
73                 int i = 0; \
74                 for (entity it = findchainentity_tofield(_FOREACH_ENTITY_fld, NULL, _FOREACH_ENTITY_next); it; (it = it._FOREACH_ENTITY_next, ++i)) \
75                 { \
76                         if (cond) { LAMBDA(body) } \
77                 } \
78         } MACRO_END
79
80 #define FOREACH_ENTITY_ORDERED(cond, body) \
81         MACRO_BEGIN { \
82                 int i = 0; \
83                 for (entity it = NULL; (it = nextent(it)); ++i) \
84                 { \
85                         if (cond) { LAMBDA(body) } \
86                 } \
87         } MACRO_END
88
89 #define FOREACH_ENTITY_FLAGS(fld, flags, body) \
90         MACRO_BEGIN { \
91                 int i = 0; \
92                 for (entity it = _findchainflags_tofield(fld, flags, _FOREACH_ENTITY_next); it; (it = it._FOREACH_ENTITY_next, ++i)) \
93                 { \
94                         LAMBDA(body) \
95                 } \
96         } MACRO_END
97
98 #define FOREACH_ENTITY_CLASS(class, cond, body) \
99         MACRO_BEGIN { \
100                 int i = 0; \
101                 for (entity it = _findchainstring_tofield(classname, class, _FOREACH_ENTITY_next); it; (it = it._FOREACH_ENTITY_next, ++i)) \
102                 { \
103                         if (cond) { LAMBDA(body) } \
104                 } \
105         } MACRO_END
106
107 #define FOREACH_ENTITY(cond, body) FOREACH_ENTITY_UNORDERED(cond, body)
108
109 #define FOREACH(list, cond, body) FOREACH_LIST(list, enemy, cond, body)
110
111 #endif