X-Git-Url: https://de.git.xonotic.org/?a=blobdiff_plain;f=qcsrc%2Flib%2Fiter.qh;h=c21d02121347d34b79042a31809ccd421693ab3f;hb=4e78b3833a581612022ebe7c034c1d61d4c0b96f;hp=53b3d662992f23668fc77be6e45a4cb91a0422d2;hpb=18e2cd311a581f77ba8eb9c5421dd219ff5d760d;p=xonotic%2Fxonotic-data.pk3dir.git diff --git a/qcsrc/lib/iter.qh b/qcsrc/lib/iter.qh index 53b3d6629..c21d02121 100644 --- a/qcsrc/lib/iter.qh +++ b/qcsrc/lib/iter.qh @@ -1,20 +1,180 @@ -#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) +#pragma once + +#if 1 +#define ITER_CONST const +#else +#define ITER_CONST +#endif + +#define FOREACH_ARRAY(arr, start, end, cond, body) \ + MACRO_BEGIN \ + { \ + for (int _i = start; _i < end; ++_i) \ + { \ + const noref int i = _i; \ + ITER_CONST noref entity it = arr[i]; \ + if (cond) { LAMBDA(body) } \ + } \ + } MACRO_END #define FOREACH(list, cond, body) FOREACH_LIST(list, enemy, cond, body) +#define FOREACH_LIST(list, next, cond, body) \ + MACRO_BEGIN \ + { \ + int _i = 0; \ + for (entity _it = list##_first; _it; (_it = _it.next, ++_i)) \ + { \ + const noref int i = _i; \ + ITER_CONST noref entity it = _it; \ + 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 int i = _i; \ + const noref string it = _it; \ + if (cond) { LAMBDA(body) } \ + } \ + } 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 STRING_ITERATOR_PEEK(this) str2chr(this##_s, this##_i) +#define STRING_ITERATOR_NEXT(this) MACRO_BEGIN ++this##_i; MACRO_END +#define STRING_ITERATOR_UNGET(this) MACRO_BEGIN --this##_i; MACRO_END +#define STRING_ITERATOR_SAVE(this) this##_i +#define STRING_ITERATOR_LOAD(this, n) MACRO_BEGIN this##_i = n; MACRO_END + +#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(entity start, .string fld, string match) _findstring = #18; + entity(.string fld, string match, .entity tofield) _findchainstring_tofield = #402; + + entity(entity start, .entity fld, entity match) _findentity = #98; + entity(.entity fld, entity match, .entity tofield) _findchainentity_tofield = #403; + + entity(entity start, .float fld, float match) _findfloat = #98; + entity(.float fld, float match, .entity tofield) _findchainfloat_tofield = #403; + + entity(entity start, .float fld, float match) _findflags = #449; + entity(.float fld, float match, .entity tofield) _findchainflags_tofield = #450; +#elif defined(SVQC) + entity(entity start, .string fld, string match) _findstring = #18; + entity(.string fld, string match, .entity tofield) _findchainstring_tofield = #402; + + entity(entity start, .entity fld, entity match) _findentity = #98; + entity(.entity fld, entity match, .entity tofield) _findchainentity_tofield = #403; + + entity(entity start, .float fld, float match) _findfloat = #98; + entity(.float fld, float match, .entity tofield) _findchainfloat_tofield = #403; + + entity(entity start, .float fld, float match) _findflags = #449; + entity(.float fld, float match, .entity tofield) _findchainflags_tofield = #450; +#elif defined(MENUQC) + entity(entity start, .string fld, string match) _findstring = #24; + entity(.string fld, string match, .entity tofield) _findchainstring_tofield = #26; + + entity(entity start, .entity fld, entity match) _findentity = #25; + entity(.entity fld, entity match, .entity tofield) _findchainentity_tofield = #27; + + entity(entity start, .float fld, float match) _findfloat = #25; + entity(.float fld, float match, .entity tofield) _findchainfloat_tofield = #27; + + entity(entity start, .float fld, float match) _findflags = #87; + entity(.float fld, float match, .entity tofield) _findchainflags_tofield = #88; #endif + +#define ORDERED(F) F##_UNORDERED +#define _FOREACH_ENTITY_FIND_ORDERED(T, fld, match, cond, body) \ + MACRO_BEGIN { \ + int _i = 0; \ + for (entity _it = NULL; (_it = _find##T(_it, fld, match)); ++_i) \ + { \ + const noref int i = _i; \ + ITER_CONST noref entity it = _it; \ + if (cond) LAMBDA(body) \ + } \ + } MACRO_END +#define _FOREACH_ENTITY_FIND_UNORDERED(id, T, fld, match, cond, body) \ + MACRO_BEGIN { \ + if (_FOREACH_ENTITY_FIND_##T##_##id##mutex) LOG_SEVEREF("Loop mutex held by %s", _FOREACH_ENTITY_FIND_##T##_##id##mutex); \ + _FOREACH_ENTITY_FIND_##T##_##id##mutex = __FUNC__; \ + entity _foundchain_first = _findchain##T##_tofield(fld, match, _FOREACH_ENTITY_FIND_##T##_next##id); \ + FOREACH_LIST(_foundchain, _FOREACH_ENTITY_FIND_##T##_next##id, cond, body); \ + _FOREACH_ENTITY_FIND_##T##_##id##mutex = string_null; \ + } MACRO_END + +#define FOREACH_ENTITY(cond, body) ORDERED(FOREACH_ENTITY)(cond, body) +#define FOREACH_ENTITY_ORDERED(cond, body) \ + MACRO_BEGIN { \ + int _i = 0; \ + for (entity _it = NULL; (_it = nextent(_it)); ++_i) \ + { \ + const noref int i = _i; \ + ITER_CONST noref entity it = _it; \ + if (cond) LAMBDA(body) \ + } \ + } MACRO_END +/** marker field, always NULL */ +.entity _FOREACH_ENTITY_fld; +.entity _FOREACH_ENTITY_FIND_entity_nextall; noref string _FOREACH_ENTITY_FIND_entity_allmutex; +#define FOREACH_ENTITY_UNORDERED(cond, body) _FOREACH_ENTITY_FIND_UNORDERED(all, entity, _FOREACH_ENTITY_fld, NULL, cond, body) + +#define FOREACH_ENTITY_FLAGS(fld, match, body) ORDERED(FOREACH_ENTITY_FLAGS)(fld, match, body) +#define FOREACH_ENTITY_FLAGS_ORDERED(fld, match, body) _FOREACH_ENTITY_FIND_ORDERED(flags, fld, match, true, body) +.entity _FOREACH_ENTITY_FIND_flags_next; noref string _FOREACH_ENTITY_FIND_flags_mutex; +#define FOREACH_ENTITY_FLAGS_UNORDERED(fld, match, body) _FOREACH_ENTITY_FIND_UNORDERED(, flags, fld, match, true, body) + +#ifndef MENUQC +entity(vector org, float rad, .entity tofield) _findchainradius_tofield = #22; +#define FOREACH_ENTITY_RADIUS(org, dist, cond, body) FOREACH_ENTITY_RADIUS_UNORDERED(org, dist, cond, body) +.entity _FOREACH_ENTITY_FIND_radius_next; noref string _FOREACH_ENTITY_FIND_radius_mutex; +#define FOREACH_ENTITY_RADIUS_UNORDERED(org, dist, cond, body) _FOREACH_ENTITY_FIND_UNORDERED(, radius, org, dist, cond, body) +#endif + +#define FOREACH_ENTITY_FLOAT(fld, match, body) ORDERED(FOREACH_ENTITY_FLOAT)(fld, match, body) +#define FOREACH_ENTITY_FLOAT_ORDERED(fld, match, body) _FOREACH_ENTITY_FIND_ORDERED(float, fld, match, true, body) +.entity _FOREACH_ENTITY_FIND_float_next; noref string _FOREACH_ENTITY_FIND_float_mutex; +#define FOREACH_ENTITY_FLOAT_UNORDERED(fld, match, body) _FOREACH_ENTITY_FIND_UNORDERED(, float, fld, match, true, body) + +#define FOREACH_ENTITY_ENT(fld, match, body) ORDERED(FOREACH_ENTITY_ENT)(fld, match, body) +#define FOREACH_ENTITY_ENT_ORDERED(fld, match, body) _FOREACH_ENTITY_FIND_ORDERED(entity, fld, match, true, body) +.entity _FOREACH_ENTITY_FIND_entity_next; noref string _FOREACH_ENTITY_FIND_entity_mutex; +#define FOREACH_ENTITY_ENT_UNORDERED(fld, match, body) _FOREACH_ENTITY_FIND_UNORDERED(, entity, fld, match, true, body) + +#define FOREACH_ENTITY_STRING(fld, match, body) ORDERED(FOREACH_ENTITY_STRING)(fld, match, body) +#define FOREACH_ENTITY_STRING_ORDERED(fld, match, body) _FOREACH_ENTITY_FIND_ORDERED(string, fld, match, true, body) +.entity _FOREACH_ENTITY_FIND_string_next; noref string _FOREACH_ENTITY_FIND_string_mutex; +#define FOREACH_ENTITY_STRING_UNORDERED(fld, match, body) _FOREACH_ENTITY_FIND_UNORDERED(, string, fld, match, true, body) + +#define FOREACH_ENTITY_CLASS(class, cond, body) ORDERED(FOREACH_ENTITY_CLASS)(class, cond, body) +#define FOREACH_ENTITY_CLASS_ORDERED(class, cond, body) _FOREACH_ENTITY_FIND_ORDERED(string, classname, class, cond, body) +.entity _FOREACH_ENTITY_FIND_string_nextclazz; noref string _FOREACH_ENTITY_FIND_string_clazzmutex; +#define FOREACH_ENTITY_CLASS_UNORDERED(class, cond, body) _FOREACH_ENTITY_FIND_UNORDERED(clazz, string, classname, class, cond, body)