Codechange: Simplify YAPF debug helpers a little. (#14741)
Remove template magic to get C-array size.
This commit is contained in:
@@ -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"));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -18,24 +18,14 @@
|
||||
#include "../track_type.h"
|
||||
#include "../core/format.hpp"
|
||||
|
||||
/** Helper template class that provides C array length and item type */
|
||||
template <typename T> struct ArrayT;
|
||||
|
||||
/** Helper template class that provides C array length and item type */
|
||||
template <typename T, size_t N> struct ArrayT<T[N]> {
|
||||
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 <typename E, typename T>
|
||||
inline typename ArrayT<T>::Item ItemAtT(E idx, const T &t, typename ArrayT<T>::Item t_unk)
|
||||
template <typename E>
|
||||
inline std::string_view ItemAt(E idx, std::span<const std::string_view> t, std::string_view t_unk)
|
||||
{
|
||||
if (static_cast<size_t>(idx) >= ArrayT<T>::length) {
|
||||
if (static_cast<size_t>(idx) >= std::size(t)) {
|
||||
return t_unk;
|
||||
}
|
||||
return t[idx];
|
||||
@@ -46,16 +36,13 @@ inline typename ArrayT<T>::Item ItemAtT(E idx, const T &t, typename ArrayT<T>::I
|
||||
* or t_inv when index == idx_inv
|
||||
* or t_unk when index is out of bounds.
|
||||
*/
|
||||
template <typename E, typename T>
|
||||
inline typename ArrayT<T>::Item ItemAtT(E idx, const T &t, typename ArrayT<T>::Item t_unk, E idx_inv, typename ArrayT<T>::Item t_inv)
|
||||
template <typename E>
|
||||
inline std::string_view ItemAt(E idx, std::span<const std::string_view> t, std::string_view t_unk, E idx_inv, std::string_view t_inv)
|
||||
{
|
||||
if (static_cast<size_t>(idx) < ArrayT<T>::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<T>::Item ItemAtT(E idx, const T &t, typename ArrayT<T>::I
|
||||
* or t_inv when index == idx_inv
|
||||
* or t_unk when index is out of bounds.
|
||||
*/
|
||||
template <typename E, typename T>
|
||||
inline std::string ComposeNameT(E value, T &t, std::string_view t_unk, E val_inv, std::string_view name_inv)
|
||||
template <typename E>
|
||||
inline std::string ComposeName(E value, std::span<const std::string_view> 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 = "<none>";
|
||||
} else {
|
||||
for (size_t i = 0; i < ArrayT<T>::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 <typename E>
|
||||
inline std::string ComposeNameT(E value, std::span<const std::string_view> names, std::string_view unknown_name)
|
||||
inline std::string ComposeName(E value, std::span<const std::string_view> names, std::string_view unknown_name)
|
||||
{
|
||||
std::string out;
|
||||
if (value.base() == 0) {
|
||||
|
||||
@@ -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 */
|
||||
|
||||
Reference in New Issue
Block a user