]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/iter.qh
Merge branch 'master' into Mirio/balance
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / iter.qh
1 #pragma once
2
3 #define FOREACH_ARRAY(arr, start, end, cond, body) \
4         MACRO_BEGIN \
5         { \
6                 for (int _i = start; _i < end; ++_i) \
7                 { \
8                         const noref int i = _i; \
9                         const noref entity it = arr[i]; \
10                         if (cond) { LAMBDA(body) } \
11                 } \
12         } MACRO_END
13
14 #define FOREACH(list, cond, body) FOREACH_LIST(list, enemy, cond, body)
15
16 #define FOREACH_LIST(list, next, cond, body) \
17         MACRO_BEGIN \
18         { \
19                 int _i = 0; \
20                 for (entity _it = list##_first; _it; (_it = _it.next, ++_i)) \
21                 { \
22                         const noref int i = _i; \
23                         const noref entity it = _it; \
24                         if (cond) { LAMBDA(body) } \
25                 } \
26         } MACRO_END
27
28 #define FOREACH_WORD(words, cond, body) \
29         MACRO_BEGIN \
30         { \
31                 string _words = words; \
32                 int _i = 0; \
33                 for (string _it; (_it = car(_words)); (_words = cdr(_words), ++_i)) \
34                 { \
35                         const noref int i = _i; \
36                         const noref string it = _it; \
37                         if (cond) { LAMBDA(body) } \
38                 } \
39         } MACRO_END
40
41 #define STRING_ITERATOR(this, s, i) \
42         string this##_s = s; \
43         int this##_i = i
44
45 #define STRING_ITERATOR_SET(this, s, i) \
46         MACRO_BEGIN { \
47                 this##_s = s; \
48                 this##_i = i; \
49         } MACRO_END
50
51 #define STRING_ITERATOR_GET(this) str2chr(this##_s, this##_i++)
52
53 #define FOREACH_CHAR(s, cond, body) \
54         MACRO_BEGIN \
55         { \
56                 STRING_ITERATOR(iter, s, 0); \
57                 int _it; \
58                 while ((_it = STRING_ITERATOR_GET(iter)) > 0) \
59                 { \
60                         const noref int it = _it; \
61                         if (cond) { LAMBDA(body) } \
62                 } \
63         } MACRO_END
64
65 #if defined(CSQC)
66         entity(.string fld, string match, .entity tofield) _findchainstring_tofield = #402;
67         entity(.entity fld, entity match, .entity tofield) _findchainentity_tofield = #403;
68         entity(.float fld, float match, .entity tofield) _findchainfloat_tofield = #403;
69         entity(.float fld, float match, .entity tofield) _findchainflags_tofield = #450;
70 #elif defined(SVQC)
71         entity(.string fld, string match, .entity tofield) _findchainstring_tofield = #402;
72         entity(.entity fld, entity match, .entity tofield) _findchainentity_tofield = #403;
73         entity(.float fld, float match, .entity tofield) _findchainfloat_tofield = #403;
74         entity(.float fld, float match, .entity tofield) _findchainflags_tofield = #450;
75 #elif defined(MENUQC)
76         entity(.string fld, string match, .entity tofield) _findchainstring_tofield = #26;
77         entity(.entity fld, entity match, .entity tofield) _findchainentity_tofield = #27;
78         entity(.float fld, float match, .entity tofield) _findchainfloat_tofield = #27;
79         entity(.float fld, float match, .entity tofield) _findchainflags_tofield = #88;
80 #endif
81
82 #define ORDERED(F) F##_UNORDERED
83
84 #define FOREACH_ENTITY(cond, body) ORDERED(FOREACH_ENTITY)(cond, body)
85 #define FOREACH_ENTITY_ORDERED(cond, body) \
86         MACRO_BEGIN { \
87                 int _i = 0; \
88                 for (entity _it = NULL; (_it = nextent(_it)); ++_i) \
89                 { \
90                         const noref int i = _i; \
91                         const noref entity it = _it; \
92                         if (cond) { LAMBDA(body) } \
93                 } \
94         } MACRO_END
95 /** marker field, always NULL */
96 .entity _FOREACH_ENTITY_fld;
97
98 .entity _FOREACH_ENTITY_next;
99 noref string _FOREACH_ENTITY_mutex;
100 #define FOREACH_ENTITY_UNORDERED(cond, body) \
101         MACRO_BEGIN { \
102                 if (_FOREACH_ENTITY_mutex) LOG_SEVEREF("Loop mutex held by %s", _FOREACH_ENTITY_mutex); \
103                 _FOREACH_ENTITY_mutex = __FUNC__; \
104                 entity _foundchain_first = _findchainentity_tofield(_FOREACH_ENTITY_fld, NULL, _FOREACH_ENTITY_next); \
105                 FOREACH_LIST(_foundchain, _FOREACH_ENTITY_next, cond, body); \
106                 _FOREACH_ENTITY_mutex = string_null; \
107         } MACRO_END
108
109
110
111 #define FOREACH_ENTITY_CLASS(class, cond, body) ORDERED(FOREACH_ENTITY_CLASS)(class, cond, body)
112 #define FOREACH_ENTITY_CLASS_ORDERED(class, cond, body) FOREACH_ENTITY_ORDERED(it.classname == class && (cond), body)
113 .entity _FOREACH_ENTITY_CLASS_next;
114 noref string _FOREACH_ENTITY_CLASS_mutex;
115 #define FOREACH_ENTITY_CLASS_UNORDERED(class, cond, body) \
116         MACRO_BEGIN { \
117                 if (_FOREACH_ENTITY_CLASS_mutex) LOG_SEVEREF("Loop mutex held by %s", _FOREACH_ENTITY_CLASS_mutex); \
118                 _FOREACH_ENTITY_CLASS_mutex = __FUNC__; \
119                 entity _foundchain_first = _findchainstring_tofield(classname, class, _FOREACH_ENTITY_CLASS_next); \
120                 FOREACH_LIST(_foundchain, _FOREACH_ENTITY_CLASS_next, cond, body); \
121                 _FOREACH_ENTITY_CLASS_mutex = string_null; \
122         } MACRO_END
123
124
125
126 #define FOREACH_ENTITY_FLOAT(fld, match, body) ORDERED(FOREACH_ENTITY_FLOAT)(fld, match, body)
127 #define FOREACH_ENTITY_FLOAT_ORDERED(fld, match, body) FOREACH_ENTITY_ORDERED(it.fld == match, body)
128 .entity _FOREACH_ENTITY_FLOAT_next;
129 noref string _FOREACH_ENTITY_FLOAT_mutex;
130 #define FOREACH_ENTITY_FLOAT_UNORDERED(fld, match, body) \
131         MACRO_BEGIN { \
132                 if (_FOREACH_ENTITY_FLOAT_mutex) LOG_SEVEREF("Loop mutex held by %s", _FOREACH_ENTITY_FLOAT_mutex); \
133                 _FOREACH_ENTITY_FLOAT_mutex = __FUNC__; \
134                 entity _foundchain_first = _findchainfloat_tofield(fld, match, _FOREACH_ENTITY_FLOAT_next); \
135                 FOREACH_LIST(_foundchain, _FOREACH_ENTITY_FLOAT_next, true, body); \
136                 _FOREACH_ENTITY_FLOAT_mutex = string_null; \
137         } MACRO_END
138
139
140
141 #define FOREACH_ENTITY_FLAGS(fld, match, body) ORDERED(FOREACH_ENTITY_FLAGS)(fld, match, body)
142 #define FOREACH_ENTITY_FLAGS_ORDERED(fld, match, body) FOREACH_ENTITY_ORDERED(it.fld & match, body)
143 .entity _FOREACH_ENTITY_FLAGS_next;
144 noref string _FOREACH_ENTITY_FLAGS_mutex;
145 #define FOREACH_ENTITY_FLAGS_UNORDERED(fld, match, body) \
146         MACRO_BEGIN { \
147                 if (_FOREACH_ENTITY_FLAGS_mutex) LOG_SEVEREF("Loop mutex held by %s", _FOREACH_ENTITY_FLAGS_mutex); \
148                 _FOREACH_ENTITY_FLAGS_mutex = __FUNC__; \
149                 entity _foundchain_first = _findchainflags_tofield(fld, match, _FOREACH_ENTITY_FLAGS_next); \
150                 FOREACH_LIST(_foundchain, _FOREACH_ENTITY_FLAGS_next, true, body); \
151                 _FOREACH_ENTITY_FLAGS_mutex = string_null; \
152         } MACRO_END
153
154
155
156 #define FOREACH_ENTITY_ENT(fld, match, body) ORDERED(FOREACH_ENTITY_ENT)(fld, match, body)
157 #define FOREACH_ENTITY_ENT_ORDERED(fld, match, body) FOREACH_ENTITY_ORDERED(it.fld == match, body)
158 .entity _FOREACH_ENTITY_ENT_next;
159 noref string _FOREACH_ENTITY_ENT_mutex;
160 #define FOREACH_ENTITY_ENT_UNORDERED(fld, match, body) \
161         MACRO_BEGIN { \
162                 if (_FOREACH_ENTITY_ENT_mutex) LOG_SEVEREF("Loop mutex held by %s", _FOREACH_ENTITY_ENT_mutex); \
163                 _FOREACH_ENTITY_ENT_mutex = __FUNC__; \
164                 entity _foundchain_first = _findchainentity_tofield(fld, match, _FOREACH_ENTITY_ENT_next); \
165                 FOREACH_LIST(_foundchain, _FOREACH_ENTITY_ENT_next, true, body); \
166                 _FOREACH_ENTITY_ENT_mutex = string_null; \
167         } MACRO_END
168
169
170
171 #define FOREACH_ENTITY_STRING(fld, match, body) ORDERED(FOREACH_ENTITY_STRING)(fld, match, body)
172 #define FOREACH_ENTITY_STRING_ORDERED(fld, match, body) FOREACH_ENTITY_ORDERED(it.fld == match, body)
173 .entity _FOREACH_ENTITY_STRING_next;
174 noref string _FOREACH_ENTITY_STRING_mutex;
175 #define FOREACH_ENTITY_STRING_UNORDERED(fld, match, body) \
176         MACRO_BEGIN { \
177                 if (_FOREACH_ENTITY_STRING_mutex) LOG_SEVEREF("Loop mutex held by %s", _FOREACH_ENTITY_STRING_mutex); \
178                 _FOREACH_ENTITY_STRING_mutex = __FUNC__; \
179                 entity _foundchain_first = _findchainstring_tofield(fld, match, _FOREACH_ENTITY_STRING_next); \
180                 FOREACH_LIST(_foundchain, _FOREACH_ENTITY_STRING_next, true, body); \
181                 _FOREACH_ENTITY_STRING_mutex = string_null; \
182         } MACRO_END