OpenTTD Source: src/misc/dbg_helpers.cpp Source File

OpenTTD Source 15.3

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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
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
18 static 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), ItemAt(td, trackdir_names, "UNK", INVALID_TRACKDIR, "INV"));
27}
28
30std::string ValueStr(TrackdirBits td_bits)
31{
32 return fmt::format("{} ({})", to_underlying(td_bits), ComposeName(td_bits, trackdir_names, "UNK", INVALID_TRACKDIR_BIT, "INV"));
33}
34
35
37 static 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), ItemAt(dd, diagdir_names, "UNK", INVALID_DIAGDIR, "INV"));
45}
46
47
49 static 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), ItemAt(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
75std::string DumpTarget::GetCurrentStructName()
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
89 bool 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
101 void DumpTarget::WriteIndent()
102{
103 int num_spaces = 2 * m_indent;
104 if (num_spaces > 0) {
105 m_out += std::string(num_spaces, ' ');
106 }
107}
108
110 void DumpTarget::WriteTile(std::string_view name, TileIndex tile)
111{
112 WriteIndent();
113 format_append(m_out, "{} = {}\n", name, TileStr(tile));
114}
115
119 void 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
143 void DumpTarget::EndStruct()
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}
signal_type_names
static const std::string_view signal_type_names[]
SignalType short names.
Definition dbg_helpers.cpp:49
ValueStr
std::string ValueStr(Trackdir td)
Return name of given Trackdir.
Definition dbg_helpers.cpp:24
TileStr
std::string TileStr(TileIndex tile)
Translate TileIndex into string.
Definition dbg_helpers.cpp:61
diagdir_names
static const std::string_view diagdir_names[]
DiagDirection short names.
Definition dbg_helpers.cpp:37
trackdir_names
static const std::string_view trackdir_names[]
Trackdir & TrackdirBits short names.
Definition dbg_helpers.cpp:18
dbg_helpers.h
Functions to be used for debug printings.
ComposeName
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)
Helper template function that returns compound bitfield name that is concatenation of names of each s...
Definition dbg_helpers.h:55
ItemAt
std::string_view ItemAt(E idx, std::span< const std::string_view > t, std::string_view 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:26
DiagDirection
DiagDirection
Enumeration for diagonal directions.
Definition direction_type.h:78
INVALID_DIAGDIR
@ INVALID_DIAGDIR
Flag for an invalid DiagDirection.
Definition direction_type.h:85
to_underlying
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
TileY
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition map_func.h:438
TileX
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition map_func.h:428
SignalType
SignalType
Type of signal, i.e.
Definition signal_type.h:23
DumpTarget::KnownStructKey
Used as a key into map of known object instances.
Definition dbg_helpers.h:112
DumpTarget::m_indent
int m_indent
current indent/nesting level
Definition dbg_helpers.h:133
DumpTarget::m_out
std::string m_out
the output string
Definition dbg_helpers.h:132
DumpTarget::WriteTile
void WriteTile(std::string_view name, TileIndex t)
Write name & TileIndex to the output.
Definition dbg_helpers.cpp:110
DumpTarget::m_cur_struct
std::stack< std::string > m_cur_struct
here we will track the current structure name
Definition dbg_helpers.h:134
DumpTarget::BeginStruct
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>'.
Definition dbg_helpers.cpp:119
DumpTarget::GetCurrentStructName
std::string GetCurrentStructName()
Return structured name of the current class/structure.
Definition dbg_helpers.cpp:75
DumpTarget::m_known_names
KNOWN_NAMES m_known_names
map of known object instances and their structured names
Definition dbg_helpers.h:135
DumpTarget::FindKnownName
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:89
DumpTarget::WriteIndent
void WriteIndent()
Write some leading spaces into the output.
Definition dbg_helpers.cpp:101
DumpTarget::LastTypeId
static size_t & LastTypeId()
Keep track of the last assigned type_id.
Definition dbg_helpers.cpp:68
DumpTarget::EndStruct
void EndStruct()
Close structure '}<LF>'.
Definition dbg_helpers.cpp:143
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
Trackdir
Trackdir
Enumeration for tracks and directions.
Definition track_type.h:66
INVALID_TRACKDIR
@ INVALID_TRACKDIR
Flag for an invalid trackdir.
Definition track_type.h:85
TrackdirBits
TrackdirBits
Allow incrementing of Trackdir variables.
Definition track_type.h:97
INVALID_TRACKDIR_BIT
@ INVALID_TRACKDIR_BIT
Flag for an invalid trackdirbit value.
Definition track_type.h:113