]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/lib/iter.qh
Merge branch 'master' into terencehill/menu_optimization
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / iter.qh
index 53b3d662992f23668fc77be6e45a4cb91a0422d2..f293aa1493ae11226504c928abd98511dba56573 100644 (file)
@@ -1,19 +1,92 @@
 #ifndef ITER_H
 #define ITER_H
 
-#define FOREACH_ARRAY(arr, start, end, cond, body) do { \
-    for (int i = start; i < end; ++i) {                 \
-        const noref entity it = arr[i];                 \
-        if (cond) { body }                              \
-    }                                                   \
-} while(0)
-
-#define FOREACH_LIST(list, next, cond, body) do {               \
-    noref int i = 0;                                            \
-    for (entity it = list##_first; it; (it = it.next, ++i)) {   \
-        if (cond) { body }                                      \
-    }                                                           \
-} while(0)
+#define FOREACH_ARRAY(arr, start, end, cond, body) \
+       MACRO_BEGIN \
+       { \
+               for (int i = start; i < end; ++i) \
+               { \
+                       const noref entity it = arr[i]; \
+                       if (cond) { LAMBDA(body) } \
+               } \
+       } MACRO_END
+
+#define FOREACH_LIST(list, next, cond, body) \
+       MACRO_BEGIN \
+       { \
+               int i = 0; \
+               for (entity it = list##_first; it; (it = it.next, ++i)) \
+               { \
+                       if (cond) { LAMBDA(body) } \
+               } \
+       } MACRO_END
+
+#define FOREACH_WORD(words, cond, body) \
+       MACRO_BEGIN \
+       { \
+               string _words = words; \
+               int i = 0; \
+               for (string _it; (_it = car(_words)); (_words = cdr(_words), ++i)) \
+               { \
+                       const noref string it = _it; \
+                       if (cond) { LAMBDA(body) } \
+               } \
+       } MACRO_END
+
+#if defined(CSQC)
+       entity(.string fld, string match, .entity tofield) _findchainstring_tofield = #402;
+       entity(.entity fld, entity match, .entity tofield) findchainentity_tofield = #403;
+       entity(.float fld, float match, .entity tofield) _findchainflags_tofield = #450;
+#elif defined(SVQC)
+       entity(.string fld, string match, .entity tofield) _findchainstring_tofield = #402;
+       entity(.entity fld, entity match, .entity tofield) findchainentity_tofield = #403;
+       entity(.float fld, float match, .entity tofield) _findchainflags_tofield = #450;
+#elif defined(MENUQC)
+       entity(.string fld, string match, .entity tofield) _findchainstring_tofield = #26;
+       entity(.entity fld, entity match, .entity tofield) findchainentity_tofield = #27;
+       entity(.float fld, float match, .entity tofield) _findchainflags_tofield = #88;
+#endif
+
+.entity _FOREACH_ENTITY_fld;
+.entity _FOREACH_ENTITY_next;
+
+#define FOREACH_ENTITY_UNORDERED(cond, body) \
+       MACRO_BEGIN { \
+               int i = 0; \
+               for (entity it = findchainentity_tofield(_FOREACH_ENTITY_fld, NULL, _FOREACH_ENTITY_next); it; (it = it._FOREACH_ENTITY_next, ++i)) \
+               { \
+                       if (cond) { LAMBDA(body) } \
+               } \
+       } MACRO_END
+
+#define FOREACH_ENTITY_ORDERED(cond, body) \
+       MACRO_BEGIN { \
+               int i = 0; \
+               for (entity it = NULL; (it = nextent(it)); ++i) \
+               { \
+                       if (cond) { LAMBDA(body) } \
+               } \
+       } MACRO_END
+
+#define FOREACH_ENTITY_FLAGS(fld, flags, body) \
+       MACRO_BEGIN { \
+               int i = 0; \
+               for (entity it = _findchainflags_tofield(fld, flags, _FOREACH_ENTITY_next); it; (it = it._FOREACH_ENTITY_next, ++i)) \
+               { \
+                       LAMBDA(body) \
+               } \
+       } MACRO_END
+
+#define FOREACH_ENTITY_CLASS(class, cond, body) \
+       MACRO_BEGIN { \
+               int i = 0; \
+               for (entity it = _findchainstring_tofield(classname, class, _FOREACH_ENTITY_next); it; (it = it._FOREACH_ENTITY_next, ++i)) \
+               { \
+                       if (cond) { LAMBDA(body) } \
+               } \
+       } MACRO_END
+
+#define FOREACH_ENTITY(cond, body) FOREACH_ENTITY_UNORDERED(cond, body)
 
 #define FOREACH(list, cond, body) FOREACH_LIST(list, enemy, cond, body)