]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/lib/iter.qh
Test: fix TEST_Fail
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / iter.qh
index 57be59fcb895f076a772f426eba73cd94a1baf3f..d02dd6284f74617826adea456fc405ddc165a59a 100644 (file)
-#ifndef ITER_H
-#define ITER_H
+#pragma once
 
 #define FOREACH_ARRAY(arr, start, end, cond, body) \
-       do \
+       MACRO_BEGIN \
        { \
-               for (int i = start; i < end; ++i) \
+               for (int _i = start; _i < end; ++_i) \
                { \
+                       const noref int i = _i; \
                        const noref entity it = arr[i]; \
-                       if (cond) { body } \
+                       if (cond) { LAMBDA(body) } \
                } \
-       } \
-       while (0)
+       } MACRO_END
 
 #define FOREACH_LIST(list, next, cond, body) \
-       do \
+       MACRO_BEGIN \
        { \
-               int i = 0; \
-               for (entity it = list##_first; it; (it = it.next, ++i)) \
+               int _i = 0; \
+               for (entity _it = list##_first; _it; (_it = _it.next, ++_i)) \
                { \
-                       if (cond) { body } \
+                       const noref int i = _i; \
+                       const noref entity it = _it; \
+                       if (cond) { LAMBDA(body) } \
                } \
-       } \
-       while (0)
+       } MACRO_END
 
 #define FOREACH_WORD(words, cond, body) \
-       do \
+       MACRO_BEGIN \
        { \
                string _words = words; \
-               int i = 0; \
-               for (string _it; (_it = car(_words)); (_words = cdr(_words), ++i)) \
+               int _i = 0; \
+               for (string _it; (_it = car(_words)); (_words = cdr(_words), ++_i)) \
                { \
+                       const noref int i = _i; \
                        const noref string it = _it; \
-                       if (cond) { body } \
+                       if (cond) { LAMBDA(body) } \
                } \
-       } \
-       while (0)
+       } MACRO_END
+
+#define STRING_ITERATOR(this, s, i) \
+       string this##_s = s; \
+       int this##_i = i
+
+#define STRING_ITERATOR_SET(this, s, i) \
+       MACRO_BEGIN { \
+               this##_s = s; \
+               this##_i = i; \
+       } MACRO_END
+
+#define STRING_ITERATOR_GET(this) str2chr(this##_s, this##_i++)
+
+#define FOREACH_CHAR(s, cond, body) \
+       MACRO_BEGIN \
+       { \
+               STRING_ITERATOR(iter, s, 0); \
+               int _it; \
+               while ((_it = STRING_ITERATOR_GET(iter)) > 0) \
+               { \
+                       const noref int 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) _findchainfloat_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) _findchainfloat_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) _findchainfloat_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) \
-       do { \
-               int i = 0; \
-               for (entity it = findchainentity_tofield(_FOREACH_ENTITY_fld, NULL, _FOREACH_ENTITY_next); it; (it = it._FOREACH_ENTITY_next, ++i)) \
+       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) { body } \
+                       const noref int i = _i; \
+                       const noref entity it = _it; \
+                       if (cond) { LAMBDA(body) } \
                } \
-       } \
-       while (0)
+       } MACRO_END
 
 #define FOREACH_ENTITY_ORDERED(cond, body) \
+       MACRO_BEGIN { \
+               int _i = 0; \
+               for (entity _it = NULL; (_it = nextent(_it)); ++_i) \
+               { \
+                       const noref int i = _i; \
+                       const noref entity it = _it; \
+                       if (cond) { LAMBDA(body) } \
+               } \
+       } MACRO_END
+
+#define FOREACH_ENTITY_FLOAT(fld, match, body) \
+       MACRO_BEGIN { \
+               int _i = 0; \
+               for (entity _it = _findchainfloat_tofield(fld, match, _FOREACH_ENTITY_next); _it; (_it = _it._FOREACH_ENTITY_next, ++_i)) \
+               { \
+                       const noref int i = _i; \
+                       const noref entity it = _it; \
+                       LAMBDA(body) \
+               } \
+       } MACRO_END
+
+#define FOREACH_ENTITY_FLAGS(fld, match, body) \
+       MACRO_BEGIN { \
+               int _i = 0; \
+               for (entity _it = _findchainflags_tofield(fld, match, _FOREACH_ENTITY_next); _it; (_it = _it._FOREACH_ENTITY_next, ++_i)) \
+               { \
+                       const noref int i = _i; \
+                       const noref entity it = _it; \
+                       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)) \
+               { \
+                       const noref int i = _i; \
+                       const noref entity it = _it; \
+                       if (cond) { LAMBDA(body) } \
+               } \
+       } MACRO_END
+
+#define FOREACH_ENTITY_ENT(fld, match, body) \
        do { \
-               int i = 0; \
-               for (entity it = NULL; (it = nextent(it)); ++i) \
+               int _i = 0; \
+               for (entity _it = findchainentity_tofield(fld, match, _FOREACH_ENTITY_next); _it; (_it = _it._FOREACH_ENTITY_next, ++_i)) \
                { \
-                       if (cond) { body } \
+                       const noref int i = _i; \
+                       const noref entity it = _it; \
+                       LAMBDA(body) \
                } \
        } \
        while (0)
 #define FOREACH_ENTITY(cond, body) FOREACH_ENTITY_UNORDERED(cond, body)
 
 #define FOREACH(list, cond, body) FOREACH_LIST(list, enemy, cond, body)
-
-#endif