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