OpenTTD Source  20241108-master-g80f628063a
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 <sstream>
15 #include <iomanip>
16 
17 #include "../safeguards.h"
18 
20 static const char * const trackdir_names[] = {
21  "NE", "SE", "UE", "LE", "LS", "RS", "rne", "rse",
22  "SW", "NW", "UW", "LW", "LN", "RN", "rsw", "rnw",
23 };
24 
26 std::string ValueStr(Trackdir td)
27 {
28  return std::to_string(td) + " (" + ItemAtT(td, trackdir_names, "UNK", INVALID_TRACKDIR, "INV") + ")";
29 }
30 
32 std::string ValueStr(TrackdirBits td_bits)
33 {
34  return std::to_string(td_bits) + " (" + ComposeNameT(td_bits, trackdir_names, "UNK", INVALID_TRACKDIR_BIT, "INV") + ")";
35 }
36 
37 
39 static const char * const diagdir_names[] = {
40  "NE", "SE", "SW", "NW",
41 };
42 
44 std::string ValueStr(DiagDirection dd)
45 {
46  return std::to_string(dd) + " (" + ItemAtT(dd, diagdir_names, "UNK", INVALID_DIAGDIR, "INV") + ")";
47 }
48 
49 
51 static const char * const signal_type_names[] = {
52  "NORMAL", "ENTRY", "EXIT", "COMBO", "PBS", "NOENTRY",
53 };
54 
56 std::string ValueStr(SignalType t)
57 {
58  return std::to_string(t) + " (" + ItemAtT(t, signal_type_names, "UNK") + ")";
59 }
60 
61 
63 std::string TileStr(TileIndex tile)
64 {
65  std::stringstream ss;
66  ss << "0x" << std::setfill('0') << std::setw(4) << std::hex << tile.base(); // 0x%04X
67  ss << " (" << TileX(tile) << ", " << TileY(tile) << ")";
68  return ss.str();
69 }
70  size_t& DumpTarget::LastTypeId()
74 {
75  static size_t last_type_id = 0;
76  return last_type_id;
77 }
78 
81 {
82  std::string out;
83  if (!m_cur_struct.empty()) {
84  /* we are inside some named struct, return its name */
85  out = m_cur_struct.top();
86  }
87  return out;
88 }
89 
94 bool DumpTarget::FindKnownName(size_t type_id, const void *ptr, std::string &name)
95 {
96  KNOWN_NAMES::const_iterator it = m_known_names.find(KnownStructKey(type_id, ptr));
97  if (it != m_known_names.end()) {
98  /* we have found it */
99  name = (*it).second;
100  return true;
101  }
102  return false;
103 }
104 
107 {
108  int num_spaces = 2 * m_indent;
109  if (num_spaces > 0) {
110  m_out += std::string(num_spaces, ' ');
111  }
112 }
113 
115 void DumpTarget::WriteValue(const std::string &name, int value)
116 {
117  WriteIndent();
118  m_out += name + " = " + std::to_string(value) + "\n";
119 }
120 
122 void DumpTarget::WriteValue(const std::string &name, const std::string &value_str)
123 {
124  WriteIndent();
125  m_out += name + " = " + value_str + "\n";
126 }
127 
129 void DumpTarget::WriteTile(const std::string &name, TileIndex tile)
130 {
131  WriteIndent();
132  m_out += name + " = " + TileStr(tile) + "\n";
133 }
134 
138 void DumpTarget::BeginStruct(size_t type_id, const std::string &name, const void *ptr)
139 {
140  /* make composite name */
141  std::string cur_name = GetCurrentStructName();
142  if (!cur_name.empty()) {
143  /* add name delimiter (we use structured names) */
144  cur_name += ".";
145  }
146  cur_name += name;
147 
148  /* put the name onto stack (as current struct name) */
149  m_cur_struct.push(cur_name);
150 
151  /* put it also to the map of known structures */
152  m_known_names.insert(KNOWN_NAMES::value_type(KnownStructKey(type_id, ptr), cur_name));
153 
154  WriteIndent();
155  m_out += name + " = {\n";
156  m_indent++;
157 }
158 
163 {
164  m_indent--;
165  WriteIndent();
166  m_out += "}\n";
167 
168  /* remove current struct name from the stack */
169  m_cur_struct.pop();
170 }
static const char *const diagdir_names[]
DiagDirection short names.
Definition: dbg_helpers.cpp:39
std::string ValueStr(Trackdir td)
Return name of given Trackdir.
Definition: dbg_helpers.cpp:26
std::string TileStr(TileIndex tile)
Translate TileIndex into string.
Definition: dbg_helpers.cpp:63
static const char *const trackdir_names[]
Trackdir & TrackdirBits short names.
Definition: dbg_helpers.cpp:20
static const char *const signal_type_names[]
SignalType short names.
Definition: dbg_helpers.cpp:51
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:425
static debug_inline uint TileX(TileIndex tile)
Get the X component of a tile.
Definition: map_func.h:415
SignalType
Type of signal, i.e.
Definition: signal_type.h:23
Used as a key into map of known object instances.
Definition: dbg_helpers.h:98
int m_indent
current indent/nesting level
Definition: dbg_helpers.h:119
std::string m_out
the output string
Definition: dbg_helpers.h:118
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
Definition: dbg_helpers.h:120
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.
Definition: dbg_helpers.cpp:80
KNOWN_NAMES m_known_names
map of known object instances and their structured names
Definition: dbg_helpers.h:121
bool FindKnownName(size_t type_id, const void *ptr, std::string &name)
Find the given instance in our anti-recursion repository.
Definition: dbg_helpers.cpp:94
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.
Definition: dbg_helpers.cpp:73
void EndStruct()
Close structure '}<LF>'.
Trackdir
Enumeration for tracks and directions.
Definition: track_type.h:67
@ INVALID_TRACKDIR
Flag for an invalid trackdir.
Definition: track_type.h:86
TrackdirBits
Allow incrementing of Trackdir variables.
Definition: track_type.h:98
@ INVALID_TRACKDIR_BIT
Flag for an invalid trackdirbit value.
Definition: track_type.h:114