]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/iter.qh
Merge branch 'terencehill/connection_msg_fix' into 'master'
[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
40 #define STRING_ITERATOR_SET(this, s, i) \
41         MACRO_BEGIN { \
42                 this##_s = s; \
43                 this##_i = i; \
44         } MACRO_END
45
46 #define STRING_ITERATOR_GET(this) str2chr(this##_s, this##_i++)
47
48 #define FOREACH_CHAR(s, cond, body) \
49         MACRO_BEGIN \
50         { \
51                 STRING_ITERATOR(iter, s, 0); \
52                 int it; \
53                 while ((it = STRING_ITERATOR_GET(iter)) > 0) \
54                 { \
55                         if (cond) { LAMBDA(body) } \
56                 } \
57         } MACRO_END
58
59 #if defined(CSQC)
60         entity(.string fld, string match, .entity tofield) _findchainstring_tofield = #402;
61         entity(.entity fld, entity match, .entity tofield) findchainentity_tofield = #403;
62         entity(.float fld, float match, .entity tofield) _findchainfloat_tofield = #403;
63         entity(.float fld, float match, .entity tofield) _findchainflags_tofield = #450;
64 #elif defined(SVQC)
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(MENUQC)
70         entity(.string fld, string match, .entity tofield) _findchainstring_tofield = #26;
71         entity(.entity fld, entity match, .entity tofield) findchainentity_tofield = #27;
72         entity(.float fld, float match, .entity tofield) _findchainfloat_tofield = #27;
73         entity(.float fld, float match, .entity tofield) _findchainflags_tofield = #88;
74 #endif
75
76 .entity _FOREACH_ENTITY_fld;
77 .entity _FOREACH_ENTITY_next;
78
79 #define FOREACH_ENTITY_UNORDERED(cond, body) \
80         MACRO_BEGIN { \
81                 int i = 0; \
82                 for (entity it = findchainentity_tofield(_FOREACH_ENTITY_fld, NULL, _FOREACH_ENTITY_next); it; (it = it._FOREACH_ENTITY_next, ++i)) \
83                 { \
84                         if (cond) { LAMBDA(body) } \
85                 } \
86         } MACRO_END
87
88 #define FOREACH_ENTITY_ORDERED(cond, body) \
89         MACRO_BEGIN { \
90                 int i = 0; \
91                 for (entity it = NULL; (it = nextent(it)); ++i) \
92                 { \
93                         if (cond) { LAMBDA(body) } \
94                 } \
95         } MACRO_END
96
97 #define FOREACH_ENTITY_FLOAT(fld, match, body) \
98         MACRO_BEGIN { \
99                 int i = 0; \
100                 for (entity it = _findchainfloat_tofield(fld, match, _FOREACH_ENTITY_next); it; (it = it._FOREACH_ENTITY_next, ++i)) \
101                 { \
102                         LAMBDA(body) \
103                 } \
104         } MACRO_END
105
106 #define FOREACH_ENTITY_FLAGS(fld, match, body) \
107         MACRO_BEGIN { \
108                 int i = 0; \
109                 for (entity it = _findchainflags_tofield(fld, match, _FOREACH_ENTITY_next); it; (it = it._FOREACH_ENTITY_next, ++i)) \
110                 { \
111                         LAMBDA(body) \
112                 } \
113         } MACRO_END
114
115 #define FOREACH_ENTITY_CLASS(class, cond, body) \
116         MACRO_BEGIN { \
117                 int i = 0; \
118                 for (entity it = _findchainstring_tofield(classname, class, _FOREACH_ENTITY_next); it; (it = it._FOREACH_ENTITY_next, ++i)) \
119                 { \
120                         if (cond) { LAMBDA(body) } \
121                 } \
122         } MACRO_END
123
124 #define FOREACH_ENTITY(cond, body) FOREACH_ENTITY_UNORDERED(cond, body)
125
126 #define FOREACH(list, cond, body) FOREACH_LIST(list, enemy, cond, body)
127
128 #endif