OpenTTD Source 20260218-master-g2123fca5ea
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
9
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
28std::string ValueStr(Trackdir td)
29{
30 return fmt::format("{} ({})", to_underlying(td), ItemAt(td, trackdir_names, "UNK", INVALID_TRACKDIR, "INV"));
31}
32
38std::string ValueStr(TrackdirBits td_bits)
39{
40 return fmt::format("{} ({})", to_underlying(td_bits), ComposeName(td_bits, trackdir_names, "UNK", INVALID_TRACKDIR_BIT, "INV"));
41}
42
43
45static const std::string_view diagdir_names[] = {
46 "NE", "SE", "SW", "NW",
47};
48
54std::string ValueStr(DiagDirection dd)
55{
56 return fmt::format("{} ({})", to_underlying(dd), ItemAt(dd, diagdir_names, "UNK", INVALID_DIAGDIR, "INV"));
57}
58
59
61static const std::string_view signal_type_names[] = {
62 "NORMAL", "ENTRY", "EXIT", "COMBO", "PBS", "NOENTRY",
63};
64
70std::string ValueStr(SignalType t)
71{
72 return fmt::format("{} ({})", to_underlying(t), ItemAt(t, signal_type_names, "UNK"));
73}
74
75
81std::string TileStr(TileIndex tile)
82{
83 return fmt::format("0x{:04X} ({}, {})", tile.base(), TileX(tile), TileY(tile));
84}
85
90/* static */ size_t DumpTarget::NewTypeId()
91{
92 static size_t last_type_id = 0;
93 return ++last_type_id;
94}
95
101{
102 if (this->cur_struct.empty()) return {};
103
104 /* we are inside some named struct, return its name */
105 return this->cur_struct.top();
106}
107
114std::optional<std::string> DumpTarget::FindKnownAsName(size_t type_id, const void *ptr)
115{
116 KnownNamesMap::const_iterator it = this->known_names.find(KnownStructKey(type_id, ptr));
117 if (it == this->known_names.end()) return std::nullopt;
118
119 return fmt::format("known_as.{}", it->second);
120}
121
124{
125 int num_spaces = 2 * this->indent;
126 if (num_spaces > 0) {
127 this->output_buffer += std::string(num_spaces, ' ');
128 }
129}
130
136void DumpTarget::WriteTile(std::string_view name, TileIndex tile)
137{
138 this->WriteIndent();
139 format_append(this->output_buffer, "{} = {}\n", name, TileStr(tile));
140}
141
148void DumpTarget::BeginStruct(size_t type_id, std::string_view name, const void *ptr)
149{
150 /* make composite name */
151 std::string cur_name = this->GetCurrentStructName();
152 if (!cur_name.empty()) {
153 /* add name delimiter (we use structured names) */
154 cur_name += ".";
155 }
156 cur_name += name;
157
158 /* put the name onto stack (as current struct name) */
159 this->cur_struct.push(cur_name);
160
161 /* put it also to the map of known structures */
162 this->known_names.insert(KnownNamesMap::value_type(KnownStructKey(type_id, ptr), cur_name));
163
164 this->WriteIndent();
165 format_append(this->output_buffer, "{} = {{\n", name);
166 this->indent++;
167}
168
173{
174 this->indent--;
175 this->WriteIndent();
176 this->output_buffer += "}\n";
177
178 /* remove current struct name from the stack */
179 this->cur_struct.pop();
180}
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 ComposeName(E value, std::span< const std::string_view > names, std::string_view unknown_name, E invalid_index, std::string_view invalid_name)
Helper template function that returns compound bitfield name that is concatenation of names of each s...
Definition dbg_helpers.h:71
std::string_view ItemAt(E idx, std::span< const std::string_view > names, std::string_view unknown_name)
Helper template function that returns item of array at given index or unknown_name when index is out ...
Definition dbg_helpers.h:30
DiagDirection
Enumeration for diagonal directions.
@ INVALID_DIAGDIR
Flag for an invalid DiagDirection.
Type (helpers) for enums.
constexpr std::underlying_type_t< enum_type > to_underlying(enum_type e)
Implementation of std::to_underlying (from C++23).
Definition enum_type.hpp:21
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition map_func.h:430
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition map_func.h:420
Hides the direct accesses to the map array with map accessors.
A number of safeguards to prevent using unsafe methods.
SignalType
Type of signal, i.e.
Definition signal_type.h:23
Definition of base types and functions in a cross-platform compatible way.
Used as a key into map of known object instances.
KnownNamesMap known_names
Map of known object instances and their structured names.
std::stack< std::string > cur_struct
Tracker of the current structure name.
void WriteTile(std::string_view name, TileIndex t)
Write name & TileIndex to the output.
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>'.
int indent
Current indent/nesting level.
std::string GetCurrentStructName()
Return structured name of the current class/structure.
std::string output_buffer
The output string.
std::optional< std::string > FindKnownAsName(size_t type_id, const void *ptr)
Find the given instance in our anti-recursion repository.
void WriteIndent()
Write some leading spaces into the output.
void EndStruct()
Close structure '}<LF>'.
static size_t NewTypeId()
Create a new type_id.
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > > TileIndex
The index/ID of a Tile.
Definition tile_type.h:92
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