]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/iter.qh
Merge branch 'master' into terencehill/menu_optimization
[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 #if defined(CSQC)
37         entity(.string fld, string match, .entity tofield) _findchainstring_tofield = #402;
38         entity(.entity fld, entity match, .entity tofield) findchainentity_tofield = #403;
39         entity(.float fld, float match, .entity tofield) _findchainflags_tofield = #450;
40 #elif defined(SVQC)
41         entity(.string fld, string match, .entity tofield) _findchainstring_tofield = #402;
42         entity(.entity fld, entity match, .entity tofield) findchainentity_tofield = #403;
43         entity(.float fld, float match, .entity tofield) _findchainflags_tofield = #450;
44 #elif defined(MENUQC)
45         entity(.string fld, string match, .entity tofield) _findchainstring_tofield = #26;
46         entity(.entity fld, entity match, .entity tofield) findchainentity_tofield = #27;
47         entity(.float fld, float match, .entity tofield) _findchainflags_tofield = #88;
48 #endif
49
50 .entity _FOREACH_ENTITY_fld;
51 .entity _FOREACH_ENTITY_next;
52
53 #define FOREACH_ENTITY_UNORDERED(cond, body) \
54         MACRO_BEGIN { \
55                 int i = 0; \
56                 for (entity it = findchainentity_tofield(_FOREACH_ENTITY_fld, NULL, _FOREACH_ENTITY_next); it; (it = it._FOREACH_ENTITY_next, ++i)) \
57                 { \
58                         if (cond) { LAMBDA(body) } \
59                 } \
60         } MACRO_END
61
62 #define FOREACH_ENTITY_ORDERED(cond, body) \
63         MACRO_BEGIN { \
64                 int i = 0; \
65                 for (entity it = NULL; (it = nextent(it)); ++i) \
66                 { \
67                         if (cond) { LAMBDA(body) } \
68                 } \
69         } MACRO_END
70
71 #define FOREACH_ENTITY_FLAGS(fld, flags, body) \
72         MACRO_BEGIN { \
73                 int i = 0; \
74                 for (entity it = _findchainflags_tofield(fld, flags, _FOREACH_ENTITY_next); it; (it = it._FOREACH_ENTITY_next, ++i)) \
75                 { \
76                         LAMBDA(body) \
77                 } \
78         } MACRO_END
79
80 #define FOREACH_ENTITY_CLASS(class, cond, body) \
81         MACRO_BEGIN { \
82                 int i = 0; \
83                 for (entity it = _findchainstring_tofield(classname, class, _FOREACH_ENTITY_next); it; (it = it._FOREACH_ENTITY_next, ++i)) \
84                 { \
85                         if (cond) { LAMBDA(body) } \
86                 } \
87         } MACRO_END
88
89 #define FOREACH_ENTITY(cond, body) FOREACH_ENTITY_UNORDERED(cond, body)
90
91 #define FOREACH(list, cond, body) FOREACH_LIST(list, enemy, cond, body)
92
93 #endif