]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/iter.qh
Iter: prevent invalidation
[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 int i = _i; \
10                         const noref entity it = arr[i]; \
11                         if (cond) { LAMBDA(body) } \
12                 } \
13         } MACRO_END
14
15 #define FOREACH_LIST(list, next, cond, body) \
16         MACRO_BEGIN \
17         { \
18                 int _i = 0; \
19                 for (entity _it = list##_first; _it; (_it = _it.next, ++_i)) \
20                 { \
21                         const noref int i = _i; \
22                         const noref entity it = _it; \
23                         if (cond) { LAMBDA(body) } \
24                 } \
25         } MACRO_END
26
27 #define FOREACH_WORD(words, cond, body) \
28         MACRO_BEGIN \
29         { \
30                 string _words = words; \
31                 int _i = 0; \
32                 for (string _it; (_it = car(_words)); (_words = cdr(_words), ++_i)) \
33                 { \
34                         const noref int i = _i; \
35                         const noref string it = _it; \
36                         if (cond) { LAMBDA(body) } \
37                 } \
38         } MACRO_END
39
40 #define STRING_ITERATOR(this, s, i) \
41         string this##_s = s; \
42         int this##_i = i
43
44 #define STRING_ITERATOR_SET(this, s, i) \
45         MACRO_BEGIN { \
46                 this##_s = s; \
47                 this##_i = i; \
48         } MACRO_END
49
50 #define STRING_ITERATOR_GET(this) str2chr(this##_s, this##_i++)
51
52 #define FOREACH_CHAR(s, cond, body) \
53         MACRO_BEGIN \
54         { \
55                 STRING_ITERATOR(iter, s, 0); \
56                 int _it; \
57                 while ((_it = STRING_ITERATOR_GET(iter)) > 0) \
58                 { \
59                         const noref int it = _it; \
60                         if (cond) { LAMBDA(body) } \
61                 } \
62         } MACRO_END
63
64 #if defined(CSQC)
65         entity(.string fld, string match, .entity tofield) _findchainstring_tofield = #402;
66         entity(.entity fld, entity match, .entity tofield) findchainentity_tofield = #403;
67         entity(.float fld, float match, .entity tofield) _findchainfloat_tofield = #403;
68         entity(.float fld, float match, .entity tofield) _findchainflags_tofield = #450;
69 #elif defined(SVQC)
70         entity(.string fld, string match, .entity tofield) _findchainstring_tofield = #402;
71         entity(.entity fld, entity match, .entity tofield) findchainentity_tofield = #403;
72         entity(.float fld, float match, .entity tofield) _findchainfloat_tofield = #403;
73         entity(.float fld, float match, .entity tofield) _findchainflags_tofield = #450;
74 #elif defined(MENUQC)
75         entity(.string fld, string match, .entity tofield) _findchainstring_tofield = #26;
76         entity(.entity fld, entity match, .entity tofield) findchainentity_tofield = #27;
77         entity(.float fld, float match, .entity tofield) _findchainfloat_tofield = #27;
78         entity(.float fld, float match, .entity tofield) _findchainflags_tofield = #88;
79 #endif
80
81 .entity _FOREACH_ENTITY_fld;
82 .entity _FOREACH_ENTITY_next;
83
84 #define FOREACH_ENTITY_UNORDERED(cond, body) \
85         MACRO_BEGIN { \
86                 int _i = 0; \
87                 for (entity _it = findchainentity_tofield(_FOREACH_ENTITY_fld, NULL, _FOREACH_ENTITY_next); _it; (_it = _it._FOREACH_ENTITY_next, ++_i)) \
88                 { \
89                         const noref int i = _i; \
90                         const noref entity it = _it; \
91                         if (cond) { LAMBDA(body) } \
92                 } \
93         } MACRO_END
94
95 #define FOREACH_ENTITY_ORDERED(cond, body) \
96         MACRO_BEGIN { \
97                 int _i = 0; \
98                 for (entity _it = NULL; (_it = nextent(_it)); ++_i) \
99                 { \
100                         const noref int i = _i; \
101                         const noref entity it = _it; \
102                         if (cond) { LAMBDA(body) } \
103                 } \
104         } MACRO_END
105
106 #define FOREACH_ENTITY_FLOAT(fld, match, body) \
107         MACRO_BEGIN { \
108                 int _i = 0; \
109                 for (entity _it = _findchainfloat_tofield(fld, match, _FOREACH_ENTITY_next); _it; (_it = _it._FOREACH_ENTITY_next, ++_i)) \
110                 { \
111                         const noref int i = _i; \
112                         const noref entity it = _it; \
113                         LAMBDA(body) \
114                 } \
115         } MACRO_END
116
117 #define FOREACH_ENTITY_FLAGS(fld, match, body) \
118         MACRO_BEGIN { \
119                 int _i = 0; \
120                 for (entity _it = _findchainflags_tofield(fld, match, _FOREACH_ENTITY_next); _it; (_it = _it._FOREACH_ENTITY_next, ++_i)) \
121                 { \
122                         const noref int i = _i; \
123                         const noref entity it = _it; \
124                         LAMBDA(body) \
125                 } \
126         } MACRO_END
127
128 #define FOREACH_ENTITY_CLASS(class, cond, body) \
129         MACRO_BEGIN { \
130                 int _i = 0; \
131                 for (entity _it = _findchainstring_tofield(classname, class, _FOREACH_ENTITY_next); _it; (_it = _it._FOREACH_ENTITY_next, ++_i)) \
132                 { \
133                         const noref int i = _i; \
134                         const noref entity it = _it; \
135                         if (cond) { LAMBDA(body) } \
136                 } \
137         } MACRO_END
138
139 #define FOREACH_ENTITY_ENT(fld, match, body) \
140         do { \
141                 int _i = 0; \
142                 for (entity _it = findchainentity_tofield(fld, match, _FOREACH_ENTITY_next); _it; (_it = _it._FOREACH_ENTITY_next, ++_i)) \
143                 { \
144                         const noref int i = _i; \
145                         const noref entity it = _it; \
146                         LAMBDA(body) \
147                 } \
148         } \
149         while (0)
150
151 #define FOREACH_ENTITY(cond, body) FOREACH_ENTITY_UNORDERED(cond, body)
152
153 #define FOREACH(list, cond, body) FOREACH_LIST(list, enemy, cond, body)
154
155 #endif