diff --git a/src/misc/dbg_helpers.cpp b/src/misc/dbg_helpers.cpp index bc51801fa9..65076410f6 100644 --- a/src/misc/dbg_helpers.cpp +++ b/src/misc/dbg_helpers.cpp @@ -23,13 +23,13 @@ static const std::string_view trackdir_names[] = { /** Return name of given Trackdir. */ std::string ValueStr(Trackdir td) { - return fmt::format("{} ({})", to_underlying(td), ItemAtT(td, trackdir_names, "UNK", INVALID_TRACKDIR, "INV")); + return fmt::format("{} ({})", to_underlying(td), ItemAt(td, trackdir_names, "UNK", INVALID_TRACKDIR, "INV")); } /** Return composed name of given TrackdirBits. */ std::string ValueStr(TrackdirBits td_bits) { - return fmt::format("{} ({})", to_underlying(td_bits), ComposeNameT(td_bits, trackdir_names, "UNK", INVALID_TRACKDIR_BIT, "INV")); + return fmt::format("{} ({})", to_underlying(td_bits), ComposeName(td_bits, trackdir_names, "UNK", INVALID_TRACKDIR_BIT, "INV")); } @@ -41,7 +41,7 @@ static const std::string_view diagdir_names[] = { /** Return name of given DiagDirection. */ std::string ValueStr(DiagDirection dd) { - return fmt::format("{} ({})", to_underlying(dd), ItemAtT(dd, diagdir_names, "UNK", INVALID_DIAGDIR, "INV")); + return fmt::format("{} ({})", to_underlying(dd), ItemAt(dd, diagdir_names, "UNK", INVALID_DIAGDIR, "INV")); } @@ -53,7 +53,7 @@ static const std::string_view signal_type_names[] = { /** Return name of given SignalType. */ std::string ValueStr(SignalType t) { - return fmt::format("{} ({})", to_underlying(t), ItemAtT(t, signal_type_names, "UNK")); + return fmt::format("{} ({})", to_underlying(t), ItemAt(t, signal_type_names, "UNK")); } diff --git a/src/misc/dbg_helpers.h b/src/misc/dbg_helpers.h index 69e598fb74..6cf520e861 100644 --- a/src/misc/dbg_helpers.h +++ b/src/misc/dbg_helpers.h @@ -18,24 +18,14 @@ #include "../track_type.h" #include "../core/format.hpp" -/** Helper template class that provides C array length and item type */ -template struct ArrayT; - -/** Helper template class that provides C array length and item type */ -template struct ArrayT { - static const size_t length = N; - using Item = T; -}; - - /** * Helper template function that returns item of array at given index * or t_unk when index is out of bounds. */ -template -inline typename ArrayT::Item ItemAtT(E idx, const T &t, typename ArrayT::Item t_unk) +template +inline std::string_view ItemAt(E idx, std::span t, std::string_view t_unk) { - if (static_cast(idx) >= ArrayT::length) { + if (static_cast(idx) >= std::size(t)) { return t_unk; } return t[idx]; @@ -46,16 +36,13 @@ inline typename ArrayT::Item ItemAtT(E idx, const T &t, typename ArrayT::I * or t_inv when index == idx_inv * or t_unk when index is out of bounds. */ -template -inline typename ArrayT::Item ItemAtT(E idx, const T &t, typename ArrayT::Item t_unk, E idx_inv, typename ArrayT::Item t_inv) +template +inline std::string_view ItemAt(E idx, std::span t, std::string_view t_unk, E idx_inv, std::string_view t_inv) { - if (static_cast(idx) < ArrayT::length) { - return t[idx]; - } if (idx == idx_inv) { return t_inv; } - return t_unk; + return ItemAt(idx, t, t_unk); } /** @@ -64,8 +51,8 @@ inline typename ArrayT::Item ItemAtT(E idx, const T &t, typename ArrayT::I * or t_inv when index == idx_inv * or t_unk when index is out of bounds. */ -template -inline std::string ComposeNameT(E value, T &t, std::string_view t_unk, E val_inv, std::string_view name_inv) +template +inline std::string ComposeName(E value, std::span t, std::string_view t_unk, E val_inv, std::string_view name_inv) { std::string out; if (value == val_inv) { @@ -73,7 +60,7 @@ inline std::string ComposeNameT(E value, T &t, std::string_view t_unk, E val_inv } else if (value == 0) { out = ""; } else { - for (size_t i = 0; i < ArrayT::length; i++) { + for (size_t i = 0; i < std::size(t); i++) { if ((value & (1 << i)) == 0) continue; out += (!out.empty() ? "+" : ""); out += t[i]; @@ -93,7 +80,7 @@ inline std::string ComposeNameT(E value, T &t, std::string_view t_unk, E val_inv * or unknown_name when index is out of bounds. */ template -inline std::string ComposeNameT(E value, std::span names, std::string_view unknown_name) +inline std::string ComposeName(E value, std::span names, std::string_view unknown_name) { std::string out; if (value.base() == 0) { diff --git a/src/pathfinder/yapf/yapf_type.hpp b/src/pathfinder/yapf/yapf_type.hpp index 2a045e0785..7aeb0f80af 100644 --- a/src/pathfinder/yapf/yapf_type.hpp +++ b/src/pathfinder/yapf/yapf_type.hpp @@ -72,7 +72,7 @@ inline std::string ValueStr(EndSegmentReasons flags) "PATH_TOO_LONG", "FIRST_TWO_WAY_RED", "LOOK_AHEAD_END", "TARGET_REACHED" }; - return fmt::format("0x{:04X} ({})", flags.base(), ComposeNameT(flags, end_segment_reason_names, "UNK")); + return fmt::format("0x{:04X} ({})", flags.base(), ComposeName(flags, end_segment_reason_names, "UNK")); } #endif /* YAPF_TYPE_HPP */