]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/iter.qh
Merge branch 'master' into terencehill/hud_updates
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / iter.qh
1 #pragma once
2
3 #if 1
4 #define ITER_CONST const
5 #else
6 #define ITER_CONST
7 #endif
8
9 #define FOREACH_ARRAY(arr, start, end, cond, body) \
10         MACRO_BEGIN \
11         { \
12                 for (int _i = start; _i < end; ++_i) \
13                 { \
14                         const noref int i = _i; \
15                         ITER_CONST noref entity it = arr[i]; \
16                         if (cond) { LAMBDA(body) } \
17                 } \
18         } MACRO_END
19
20 #define FOREACH(list, cond, body) FOREACH_LIST(list, enemy, cond, body)
21
22 #define FOREACH_LIST(list, next, cond, body) \
23         MACRO_BEGIN \
24         { \
25                 int _i = 0; \
26                 for (entity _it = list##_first, _next = NULL; _it; (_it = _next, ++_i)) \
27                 { \
28                         const noref int i = _i; \
29                         ITER_CONST noref entity it = _it; \
30                         _next = _it.next; \
31                         if (cond) { LAMBDA(body) } \
32                 } \
33         } MACRO_END
34
35 #define FOREACH_WORD(words, cond, body) \
36         MACRO_BEGIN \
37         { \
38                 string _words = words; \
39                 int _i = 0; \
40                 for (string _it; (_it = car(_words)); (_words = cdr(_words), ++_i)) \
41                 { \
42                         const noref int i = _i; \
43                         const noref string it = _it; \
44                         if (cond) { LAMBDA(body) } \
45                 } \
46         } MACRO_END
47
48 #define STRING_ITERATOR(this, s, i) \
49         string this##_s = s; \
50         int this##_i = i
51
52 #define STRING_ITERATOR_SET(this, s, i) \
53         MACRO_BEGIN { \
54                 this##_s = s; \
55                 this##_i = i; \
56         } MACRO_END
57
58 #define STRING_ITERATOR_GET(this) str2chr(this##_s, this##_i++)
59 #define STRING_ITERATOR_PEEK(this) str2chr(this##_s, this##_i)
60 #define STRING_ITERATOR_NEXT(this) MACRO_BEGIN ++this##_i; MACRO_END
61 #define STRING_ITERATOR_UNGET(this) MACRO_BEGIN --this##_i; MACRO_END
62 #define STRING_ITERATOR_SAVE(this) this##_i
63 #define STRING_ITERATOR_LOAD(this, n) MACRO_BEGIN this##_i = n; MACRO_END
64
65 #define FOREACH_CHAR(s, cond, body) \
66         MACRO_BEGIN \
67         { \
68                 STRING_ITERATOR(iter, s, 0); \
69                 int _it; \
70                 while ((_it = STRING_ITERATOR_GET(iter)) > 0) \
71                 { \
72                         const noref int it = _it; \
73                         if (cond) { LAMBDA(body) } \
74                 } \
75         } MACRO_END
76
77 #if defined(CSQC)
78     entity(entity start, .string fld, string match) _findstring = #18;
79         entity(.string fld, string match, .entity tofield) _findchainstring_tofield = #402;
80
81     entity(entity start, .entity fld, entity match) _findentity = #98;
82         entity(.entity fld, entity match, .entity tofield) _findchainentity_tofield = #403;
83
84     entity(entity start, .float fld, float match) _findfloat = #98;
85         entity(.float fld, float match, .entity tofield) _findchainfloat_tofield = #403;
86
87         entity(entity start, .float fld, float match) _findflags = #449;
88         entity(.float fld, float match, .entity tofield) _findchainflags_tofield = #450;
89 #elif defined(SVQC)
90     entity(entity start, .string fld, string match) _findstring = #18;
91         entity(.string fld, string match, .entity tofield) _findchainstring_tofield = #402;
92
93     entity(entity start, .entity fld, entity match) _findentity = #98;
94         entity(.entity fld, entity match, .entity tofield) _findchainentity_tofield = #403;
95
96     entity(entity start, .float fld, float match) _findfloat = #98;
97         entity(.float fld, float match, .entity tofield) _findchainfloat_tofield = #403;
98
99         entity(entity start, .float fld, float match) _findflags = #449;
100         entity(.float fld, float match, .entity tofield) _findchainflags_tofield = #450;
101 #elif defined(MENUQC)
102     entity(entity start, .string fld, string match) _findstring = #24;
103         entity(.string fld, string match, .entity tofield) _findchainstring_tofield = #26;
104
105     entity(entity start, .entity fld, entity match) _findentity = #25;
106         entity(.entity fld, entity match, .entity tofield) _findchainentity_tofield = #27;
107
108     entity(entity start, .float fld, float match) _findfloat = #25;
109         entity(.float fld, float match, .entity tofield) _findchainfloat_tofield = #27;
110
111         entity(entity start, .float fld, float match) _findflags = #87;
112         entity(.float fld, float match, .entity tofield) _findchainflags_tofield = #88;
113 #endif
114
115 #define ORDERED(F) F##_UNORDERED
116 #define _FOREACH_ENTITY_FIND_ORDERED(T, fld, match, cond, body) \
117     MACRO_BEGIN { \
118         int _i = 0; \
119         for (entity _it = NULL; (_it = _find##T(_it, fld, match)); ++_i) \
120         { \
121             const noref int i = _i; \
122             ITER_CONST noref entity it = _it; \
123             if (cond) LAMBDA(body) \
124         } \
125     } MACRO_END
126 #define MUTEX_LOCK(this) MACRO_BEGIN \
127         if (this) LOG_SEVEREF("Loop mutex held by %s", this); \
128         this = __FUNC__; \
129 MACRO_END
130 #define MUTEX_UNLOCK(this) MACRO_BEGIN \
131         this = string_null; \
132 MACRO_END
133 #define _FOREACH_ENTITY_FIND_UNORDERED(id, T, fld, match, cond, body) \
134         MACRO_BEGIN { \
135                 MUTEX_LOCK(_FOREACH_ENTITY_FIND_##T##_##id##mutex); \
136                 entity _foundchain_first = _findchain##T##_tofield(fld, match, _FOREACH_ENTITY_FIND_##T##_next##id); \
137                 FOREACH_LIST(_foundchain, _FOREACH_ENTITY_FIND_##T##_next##id, cond, body); \
138                 MUTEX_UNLOCK(_FOREACH_ENTITY_FIND_##T##_##id##mutex); \
139         } MACRO_END
140
141 #define FOREACH_ENTITY(cond, body) ORDERED(FOREACH_ENTITY)(cond, body)
142 #define FOREACH_ENTITY_ORDERED(cond, body) \
143         MACRO_BEGIN { \
144                 int _i = 0; \
145                 for (entity _it = NULL; (_it = nextent(_it)); ++_i) \
146                 { \
147                         const noref int i = _i; \
148                         ITER_CONST noref entity it = _it; \
149                         if (cond) LAMBDA(body) \
150                 } \
151         } MACRO_END
152 /** marker field, always NULL */
153 .entity _FOREACH_ENTITY_fld;
154 .entity _FOREACH_ENTITY_FIND_entity_nextall; noref string _FOREACH_ENTITY_FIND_entity_allmutex;
155 #define FOREACH_ENTITY_UNORDERED(cond, body) _FOREACH_ENTITY_FIND_UNORDERED(all, entity, _FOREACH_ENTITY_fld, NULL, cond, body)
156
157 #define FOREACH_ENTITY_FLAGS(fld, match, body) ORDERED(FOREACH_ENTITY_FLAGS)(fld, match, body)
158 #define FOREACH_ENTITY_FLAGS_ORDERED(fld, match, body) _FOREACH_ENTITY_FIND_ORDERED(flags, fld, match, true, body)
159 .entity _FOREACH_ENTITY_FIND_flags_next; noref string _FOREACH_ENTITY_FIND_flags_mutex;
160 #define FOREACH_ENTITY_FLAGS_UNORDERED(fld, match, body) _FOREACH_ENTITY_FIND_UNORDERED(, flags, fld, match, true, body)
161
162 #ifdef GAMEQC
163 entity(vector org, float rad, .entity tofield) _findchainradius_tofield = #22;
164 #define FOREACH_ENTITY_RADIUS(org, dist, cond, body) ORDERED(FOREACH_ENTITY_RADIUS)(org, dist, cond, body)
165 .entity _FOREACH_ENTITY_FIND_radius_next; noref string _FOREACH_ENTITY_FIND_radius_mutex;
166 #define FOREACH_ENTITY_RADIUS_UNORDERED(org, dist, cond, body) _FOREACH_ENTITY_FIND_UNORDERED(, radius, org, dist, cond, body)
167 .entity _FOREACH_ENTITY_FIND_radius_nexttmp; noref string _FOREACH_ENTITY_FIND_radius_tmpmutex;
168 #define FOREACH_ENTITY_RADIUS_ORDERED(org, dist, cond, body) \
169 MACRO_BEGIN \
170         entity _rev_first = NULL; \
171         _FOREACH_ENTITY_FIND_UNORDERED(tmp, radius, org, dist, cond, (it._FOREACH_ENTITY_FIND_radius_nexttmp = _rev_first, _rev_first = it)); \
172         MUTEX_LOCK(_FOREACH_ENTITY_FIND_radius_tmpmutex); \
173         FOREACH_LIST(_rev, _FOREACH_ENTITY_FIND_radius_nexttmp, true, body); \
174         MUTEX_UNLOCK(_FOREACH_ENTITY_FIND_radius_tmpmutex); \
175 MACRO_END
176 #endif
177
178 #define FOREACH_ENTITY_FLOAT(fld, match, body) ORDERED(FOREACH_ENTITY_FLOAT)(fld, match, body)
179 #define FOREACH_ENTITY_FLOAT_ORDERED(fld, match, body) _FOREACH_ENTITY_FIND_ORDERED(float, fld, match, true, body)
180 .entity _FOREACH_ENTITY_FIND_float_next; noref string _FOREACH_ENTITY_FIND_float_mutex;
181 #define FOREACH_ENTITY_FLOAT_UNORDERED(fld, match, body) _FOREACH_ENTITY_FIND_UNORDERED(, float, fld, match, true, body)
182
183 #define FOREACH_ENTITY_ENT(fld, match, body) ORDERED(FOREACH_ENTITY_ENT)(fld, match, body)
184 #define FOREACH_ENTITY_ENT_ORDERED(fld, match, body) _FOREACH_ENTITY_FIND_ORDERED(entity, fld, match, true, body)
185 .entity _FOREACH_ENTITY_FIND_entity_next; noref string _FOREACH_ENTITY_FIND_entity_mutex;
186 #define FOREACH_ENTITY_ENT_UNORDERED(fld, match, body) _FOREACH_ENTITY_FIND_UNORDERED(, entity, fld, match, true, body)
187
188 #define FOREACH_ENTITY_STRING(fld, match, body) ORDERED(FOREACH_ENTITY_STRING)(fld, match, body)
189 #define FOREACH_ENTITY_STRING_ORDERED(fld, match, body) _FOREACH_ENTITY_FIND_ORDERED(string, fld, match, true, body)
190 .entity _FOREACH_ENTITY_FIND_string_next; noref string _FOREACH_ENTITY_FIND_string_mutex;
191 #define FOREACH_ENTITY_STRING_UNORDERED(fld, match, body) _FOREACH_ENTITY_FIND_UNORDERED(, string, fld, match, true, body)
192
193 #define FOREACH_ENTITY_CLASS(class, cond, body) ORDERED(FOREACH_ENTITY_CLASS)(class, cond, body)
194 #define FOREACH_ENTITY_CLASS_ORDERED(class, cond, body) _FOREACH_ENTITY_FIND_ORDERED(string, classname, class, cond, body)
195 .entity _FOREACH_ENTITY_FIND_string_nextclazz; noref string _FOREACH_ENTITY_FIND_string_clazzmutex;
196 #define FOREACH_ENTITY_CLASS_UNORDERED(class, cond, body) _FOREACH_ENTITY_FIND_UNORDERED(clazz, string, classname, class, cond, body)