OpenTTD Source 20250522-master-g467f832c2f
dbg_helpers.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
7
10#include "../stdafx.h"
11#include "../rail_map.h"
12#include "../core/enum_type.hpp"
13#include "dbg_helpers.h"
14
15#include "../safeguards.h"
16
18static const std::string_view trackdir_names[] = {
19 "NE", "SE", "UE", "LE", "LS", "RS", "rne", "rse",
20 "SW", "NW", "UW", "LW", "LN", "RN", "rsw", "rnw",
21};
22
24std::string ValueStr(Trackdir td)
25{
26 return fmt::format("{} ({})", to_underlying(td), ItemAtT(td, trackdir_names, "UNK", INVALID_TRACKDIR, "INV"));
27}
28
30std::string ValueStr(TrackdirBits td_bits)
31{
32 return fmt::format("{} ({})", to_underlying(td_bits), ComposeNameT(td_bits, trackdir_names, "UNK", INVALID_TRACKDIR_BIT, "INV"));
33}
34
35
37static const std::string_view diagdir_names[] = {
38 "NE", "SE", "SW", "NW",
39};
40
42std::string ValueStr(DiagDirection dd)
43{
44 return fmt::format("{} ({})", to_underlying(dd), ItemAtT(dd, diagdir_names, "UNK", INVALID_DIAGDIR, "INV"));
45}
46
47
49static const std::string_view signal_type_names[] = {
50 "NORMAL", "ENTRY", "EXIT", "COMBO", "PBS", "NOENTRY",
51};
52
54std::string ValueStr(SignalType t)
55{
56 return fmt::format("{} ({})", to_underlying(t), ItemAtT(t, signal_type_names, "UNK"));
57}
58
59
61std::string TileStr(TileIndex tile)
62{
63 return fmt::format("0x{:04X} ({}, {})", tile.base(), TileX(tile), TileY(tile));
64}
65
size_t& DumpTarget::LastTypeId()
69{
70 static size_t last_type_id = 0;
71 return last_type_id;
72}
73
76{
77 std::string out;
78 if (!m_cur_struct.empty()) {
79 /* we are inside some named struct, return its name */
80 out = m_cur_struct.top();
81 }
82 return out;
83}
84
89bool DumpTarget::FindKnownName(size_t type_id, const void *ptr, std::string &name)
90{
91 KNOWN_NAMES::const_iterator it = m_known_names.find(KnownStructKey(type_id, ptr));
92 if (it != m_known_names.end()) {
93 /* we have found it */
94 name = (*it).second;
95 return true;
96 }
97 return false;
98}
99
102{
103 int num_spaces = 2 * m_indent;
104 if (num_spaces > 0) {
105 m_out += std::string(num_spaces, ' ');
106 }
107}
108
110void DumpTarget::WriteTile(std::string_view name, TileIndex tile)
111{
112 WriteIndent();
113 format_append(m_out, "{} = {}\n", name, TileStr(tile));
114}
115
119void DumpTarget::BeginStruct(size_t type_id, std::string_view name, const void *ptr)
120{
121 /* make composite name */
122 std::string cur_name = GetCurrentStructName();
123 if (!cur_name.empty()) {
124 /* add name delimiter (we use structured names) */
125 cur_name += ".";
126 }
127 cur_name += name;
128
129 /* put the name onto stack (as current struct name) */
130 m_cur_struct.push(cur_name);
131
132 /* put it also to the map of known structures */
133 m_known_names.insert(KNOWN_NAMES::value_type(KnownStructKey(type_id, ptr), cur_name));
134
135 WriteIndent();
136 format_append(m_out, "{} = {{\n", name);
137 m_indent++;
138}
139
144{
145 m_indent--;
146 WriteIndent();
147 m_out += "}\n";
148
149 /* remove current struct name from the stack */
150 m_cur_struct.pop();
151}
static const std::string_view signal_type_names[]
SignalType short names.
std::string ValueStr(Trackdir td)
Return name of given Trackdir.
std::string TileStr(TileIndex tile)
Translate TileIndex into string.
static const std::string_view diagdir_names[]
DiagDirection short names.
static const std::string_view trackdir_names[]
Trackdir & TrackdirBits short names.
Functions to be used for debug printings.
std::string ComposeNameT(E value, T &t, std::string_view t_unk, E val_inv, std::string_view name_inv)
Helper template function that returns compound bitfield name that is concatenation of names of each s...
Definition dbg_helpers.h:68
ArrayT< T >::Item ItemAtT(E idx, const T &t, typename ArrayT< T >::Item t_unk)
Helper template function that returns item of array at given index or t_unk when index is out of boun...
Definition dbg_helpers.h:36
DiagDirection
Enumeration for diagonal directions.
@ INVALID_DIAGDIR
Flag for an invalid DiagDirection.
constexpr std::underlying_type_t< enum_type > to_underlying(enum_type e)
Implementation of std::to_underlying (from C++23)
Definition enum_type.hpp:17
static debug_inline uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition map_func.h:424
static debug_inline uint TileX(TileIndex tile)
Get the X component of a tile.
Definition map_func.h:414
SignalType
Type of signal, i.e.
Definition signal_type.h:23
Used as a key into map of known object instances.
int m_indent
current indent/nesting level
std::string m_out
the output string
void WriteTile(std::string_view name, TileIndex t)
Write name & TileIndex to the output.
std::stack< std::string > m_cur_struct
here we will track the current structure name
void BeginStruct(size_t type_id, std::string_view name, const void *ptr)
Open new structure (one level deeper than the current one) 'name = {<LF>'.
std::string GetCurrentStructName()
Return structured name of the current class/structure.
KNOWN_NAMES m_known_names
map of known object instances and their structured names
bool FindKnownName(size_t type_id, const void *ptr, std::string &name)
Find the given instance in our anti-recursion repository.
void WriteIndent()
Write some leading spaces into the output.
static size_t & LastTypeId()
Keep track of the last assigned type_id.
void EndStruct()
Close structure '}<LF>'.
Trackdir
Enumeration for tracks and directions.
Definition track_type.h:66
@ INVALID_TRACKDIR
Flag for an invalid trackdir.
Definition track_type.h:85
TrackdirBits
Allow incrementing of Trackdir variables.
Definition track_type.h:97
@ INVALID_TRACKDIR_BIT
Flag for an invalid trackdirbit value.
Definition track_type.h:113