diff --git a/graph.cpp b/graph.cpp index d0d601a2..2cb48666 100644 --- a/graph.cpp +++ b/graph.cpp @@ -45,7 +45,7 @@ EX bool hide_player() { ; } -#define ADC(V,c) IF_KEY_EXISTS(it, current_display->all_drawn_copies, c) for(const shiftmatrix& V: it->second) +#define ADC(V,c) if (auto *it = hr::find_or_null(current_display->all_drawn_copies, c)) for(const shiftmatrix& V: it->second) EX hookset hooks_handleKey; EX hookset hooks_drawcell; diff --git a/hyper.h b/hyper.h index 3c43b595..ddac8031 100644 --- a/hyper.h +++ b/hyper.h @@ -575,11 +575,6 @@ typedef function cellfunction; #define forCellCM(ct, cf) forCellIdCM(ct,forCellCM ## __LINE__,cf) #define forCellAll(ct, cf) forCellIdCM(ct,forCellAll ## __LINE__,cf) -/* conditions */ - -/** `IF_KEY_EXISTS(it, map, key) statement` checks whether the map 'map' contain key 'key', and if so, executes statement with it set to the relevant iterator */ -#define IF_KEY_EXISTS(it, map, key) for(auto it: {map.find(key)}) if(it != map.end()) - // canAttack/moveval flags #define AF_NORMAL 0 // nothing special about this attack @@ -851,6 +846,13 @@ template array make_array(T a, T b, T c, T d) { array x; x[0 template array make_array(T a, T b, T c) { array x; x[0] = a; x[1] = b; x[2] = c; return x; } template array make_array(T a, T b) { array x; x[0] = a; x[1] = b; return x; } +// Find in a std::map or std::unordered_map, or return null. +template +const typename Map::value_type *find_or_null(const Map& map, const Key& key) { + auto it = map.find(key); + return (it == map.end()) ? nullptr : &*it; + } + namespace daily { extern bool on; extern int daily_id; diff --git a/util.cpp b/util.cpp index f98e7d60..9072203d 100644 --- a/util.cpp +++ b/util.cpp @@ -207,7 +207,7 @@ cld exp_parser::parse(int prio) { cld c = rparse(0); force_eat(")"); - IF_KEY_EXISTS(it, extra_params, "angleunit") { + if (auto *it = hr::find_or_null(extra_params, "angleunit")) { a *= it->second; b *= it->second; c *= it->second; @@ -237,13 +237,13 @@ cld exp_parser::parse(int prio) { test.compute_sum(); test.compute_geometry(); res = test.edgelength; - IF_KEY_EXISTS(it, extra_params, "distunit") + if (auto *it = hr::find_or_null(extra_params, "distunit")) res /= it->second; } #endif else if(eat("regangle(")) { cld edgelen = parse(0); - IF_KEY_EXISTS(it, extra_params, "distunit") { + if (auto *it = hr::find_or_null(extra_params, "distunit")) { edgelen = edgelen * it->second; } @@ -260,13 +260,13 @@ cld exp_parser::parse(int prio) { if(arb::legacy) { res = M_PI - result; - IF_KEY_EXISTS(it, extra_params, "angleofs") + if (auto *it = hr::find_or_null(extra_params, "angleofs")) res -= it->second; } else res = result; - IF_KEY_EXISTS(it, extra_params, "angleunit") + if (auto *it = hr::find_or_null(extra_params, "angleunit")) res /= it->second; } else if(eat("test(")) { @@ -318,8 +318,8 @@ cld exp_parser::parse(int prio) { else if(next() == '(') at++, res = parsepar(); else { string number = next_token(); - IF_KEY_EXISTS(it, extra_params, number) res = it->second; - else IF_KEY_EXISTS(it, params, number) res = it->second->get_cld(); + if (auto *it = hr::find_or_null(extra_params, number)) res = it->second; + else if (auto *it = hr::find_or_null(params, number)) res = it->second->get_cld(); else if(number == "e") res = exp(1); else if(number == "i") res = cld(0, 1); else if(number == "p" || number == "pi") res = M_PI;