OpenTTD Source 15.0-beta2
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 "dbg_helpers.h"
13
14#include "../safeguards.h"
15
17static const char * const trackdir_names[] = {
18 "NE", "SE", "UE", "LE", "LS", "RS", "rne", "rse",
19 "SW", "NW", "UW", "LW", "LN", "RN", "rsw", "rnw",
20};
21
23std::string ValueStr(Trackdir td)
24{
25 return std::to_string(td) + " (" + ItemAtT(td, trackdir_names, "UNK", INVALID_TRACKDIR, "INV") + ")";
26}
27
29std::string ValueStr(TrackdirBits td_bits)
30{
31 return std::to_string(td_bits) + " (" + ComposeNameT(td_bits, trackdir_names, "UNK", INVALID_TRACKDIR_BIT, "INV") + ")";
32}
33
34
36static const char * const diagdir_names[] = {
37 "NE", "SE", "SW", "NW",
38};
39
41std::string ValueStr(DiagDirection dd)
42{
43 return std::to_string(dd) + " (" + ItemAtT(dd, diagdir_names, "UNK", INVALID_DIAGDIR, "INV") + ")";
44}
45
46
48static const char * const signal_type_names[] = {
49 "NORMAL", "ENTRY", "EXIT", "COMBO", "PBS", "NOENTRY",
50};
51
53std::string ValueStr(SignalType t)
54{
55 return std::to_string(t) + " (" + ItemAtT(t, signal_type_names, "UNK") + ")";
56}
57
58
60std::string TileStr(TileIndex tile)
61{
62 return fmt::format("0x{:04X} ({}, {})", tile.base(), TileX(tile), TileY(tile));
63}
64
size_t& DumpTarget::LastTypeId()
68{
69 static size_t last_type_id = 0;
70 return last_type_id;
71}
72
75{
76 std::string out;
77 if (!m_cur_struct.empty()) {
78 /* we are inside some named struct, return its name */
79 out = m_cur_struct.top();
80 }
81 return out;
82}
83
88bool DumpTarget::FindKnownName(size_t type_id, const void *ptr, std::string &name)
89{
90 KNOWN_NAMES::const_iterator it = m_known_names.find(KnownStructKey(type_id, ptr));
91 if (it != m_known_names.end()) {
92 /* we have found it */
93 name = (*it).second;
94 return true;
95 }
96 return false;
97}
98
101{
102 int num_spaces = 2 * m_indent;
103 if (num_spaces > 0) {
104 m_out += std::string(num_spaces, ' ');
105 }
106}
107
109void DumpTarget::WriteValue(const std::string &name, int value)
110{
111 WriteIndent();
112 m_out += name + " = " + std::to_string(value) + "\n";
113}
114
116void DumpTarget::WriteValue(const std::string &name, const std::string &value_str)
117{
118 WriteIndent();
119 m_out += name + " = " + value_str + "\n";
120}
121
123void DumpTarget::WriteTile(const std::string &name, TileIndex tile)
124{
125 WriteIndent();
126 m_out += name + " = " + TileStr(tile) + "\n";
127}
128
132void DumpTarget::BeginStruct(size_t type_id, const std::string &name, const void *ptr)
133{
134 /* make composite name */
135 std::string cur_name = GetCurrentStructName();
136 if (!cur_name.empty()) {
137 /* add name delimiter (we use structured names) */
138 cur_name += ".";
139 }
140 cur_name += name;
141
142 /* put the name onto stack (as current struct name) */
143 m_cur_struct.push(cur_name);
144
145 /* put it also to the map of known structures */
146 m_known_names.insert(KNOWN_NAMES::value_type(KnownStructKey(type_id, ptr), cur_name));
147
148 WriteIndent();
149 m_out += name + " = {\n";
150 m_indent++;
151}
152
157{
158 m_indent--;
159 WriteIndent();
160 m_out += "}\n";
161
162 /* remove current struct name from the stack */
163 m_cur_struct.pop();
164}
static const char *const diagdir_names[]
DiagDirection short names.
std::string ValueStr(Trackdir td)
Return name of given Trackdir.
std::string TileStr(TileIndex tile)
Translate TileIndex into string.
static const char *const trackdir_names[]
Trackdir & TrackdirBits short names.
static const char *const signal_type_names[]
SignalType short names.
Functions to be used for debug printings.
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:35
std::string ComposeNameT(E value, T &t, const char *t_unk, E val_inv, const char *name_inv)
Helper template function that returns compound bitfield name that is concatenation of names of each s...
Definition dbg_helpers.h:67
DiagDirection
Enumeration for diagonal directions.
@ INVALID_DIAGDIR
Flag for an invalid DiagDirection.
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 WriteValue(const std::string &name, int value)
Write 'name = value' with indent and new-line.
std::stack< std::string > m_cur_struct
here we will track the current structure name
void BeginStruct(size_t type_id, const std::string &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 WriteTile(const std::string &name, TileIndex t)
Write name & TileIndex to the output.
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