Update to 12.0-beta1

This commit is contained in:
dP
2021-08-15 14:57:29 +03:00
parent ac7d3eba75
commit 9df4f2c4fc
666 changed files with 61302 additions and 20466 deletions

View File

@@ -1,7 +1,6 @@
add_files(
array.hpp
binaryheap.hpp
blob.hpp
countedobj.cpp
countedptr.hpp
dbg_helpers.cpp
@@ -11,5 +10,4 @@ add_files(
getoptdata.h
hashtable.hpp
lrucache.hpp
str.hpp
)

View File

@@ -11,7 +11,7 @@
#define ARRAY_HPP
#include "fixedsizearray.hpp"
#include "str.hpp"
#include "../string_func.h"
/**
* Flexible array with size limit. Implemented as fixed size
@@ -103,14 +103,14 @@ public:
*/
template <typename D> void Dump(D &dmp) const
{
dmp.WriteLine("capacity = %d", Tcapacity);
dmp.WriteValue("capacity", Tcapacity);
uint num_items = Length();
dmp.WriteLine("num_items = %d", num_items);
CStrA name;
dmp.WriteValue("num_items", num_items);
for (uint i = 0; i < num_items; i++) {
const T &item = (*this)[i];
name.Format("item[%d]", i);
dmp.WriteStructT(name.Data(), &item);
char name[32];
seprintf(name, lastof(name), "item[%d]", i);
dmp.WriteStructT(name, &item);
}
}
};

View File

@@ -11,6 +11,9 @@
#include "../rail_map.h"
#include "dbg_helpers.h"
#include <sstream>
#include <iomanip>
#include "../safeguards.h"
/** Trackdir & TrackdirBits short names. */
@@ -20,19 +23,15 @@ static const char * const trackdir_names[] = {
};
/** Return name of given Trackdir. */
CStrA ValueStr(Trackdir td)
std::string ValueStr(Trackdir td)
{
CStrA out;
out.Format("%d (%s)", td, ItemAtT(td, trackdir_names, "UNK", INVALID_TRACKDIR, "INV"));
return out.Transfer();
return std::to_string(td) + " (" + ItemAtT(td, trackdir_names, "UNK", INVALID_TRACKDIR, "INV") + ")";
}
/** Return composed name of given TrackdirBits. */
CStrA ValueStr(TrackdirBits td_bits)
std::string ValueStr(TrackdirBits td_bits)
{
CStrA out;
out.Format("%d (%s)", td_bits, ComposeNameT(td_bits, trackdir_names, "UNK", INVALID_TRACKDIR_BIT, "INV").Data());
return out.Transfer();
return std::to_string(td_bits) + " (" + ComposeNameT(td_bits, trackdir_names, "UNK", INVALID_TRACKDIR_BIT, "INV") + ")";
}
@@ -42,11 +41,9 @@ static const char * const diagdir_names[] = {
};
/** Return name of given DiagDirection. */
CStrA ValueStr(DiagDirection dd)
std::string ValueStr(DiagDirection dd)
{
CStrA out;
out.Format("%d (%s)", dd, ItemAtT(dd, diagdir_names, "UNK", INVALID_DIAGDIR, "INV"));
return out.Transfer();
return std::to_string(dd) + " (" + ItemAtT(dd, diagdir_names, "UNK", INVALID_DIAGDIR, "INV") + ")";
}
@@ -56,20 +53,19 @@ static const char * const signal_type_names[] = {
};
/** Return name of given SignalType. */
CStrA ValueStr(SignalType t)
std::string ValueStr(SignalType t)
{
CStrA out;
out.Format("%d (%s)", t, ItemAtT(t, signal_type_names, "UNK"));
return out.Transfer();
return std::to_string(t) + " (" + ItemAtT(t, signal_type_names, "UNK") + ")";
}
/** Translate TileIndex into string. */
CStrA TileStr(TileIndex tile)
std::string TileStr(TileIndex tile)
{
CStrA out;
out.Format("0x%04X (%d, %d)", tile, TileX(tile), TileY(tile));
return out.Transfer();
std::stringstream ss;
ss << "0x" << std::setfill('0') << std::setw(4) << std::hex << tile; // 0x%04X
ss << " (" << TileX(tile) << ", " << TileY(tile) << ")";
return ss.str();
}
/**
@@ -81,21 +77,21 @@ CStrA TileStr(TileIndex tile)
}
/** Return structured name of the current class/structure. */
CStrA DumpTarget::GetCurrentStructName()
std::string DumpTarget::GetCurrentStructName()
{
CStrA out;
std::string out;
if (!m_cur_struct.empty()) {
/* we are inside some named struct, return its name */
out = m_cur_struct.top();
}
return out.Transfer();
return out;
}
/**
* Find the given instance in our anti-recursion repository.
* Return true and set name when object was found.
*/
bool DumpTarget::FindKnownName(size_t type_id, const void *ptr, CStrA &name)
bool DumpTarget::FindKnownName(size_t type_id, const void *ptr, std::string &name)
{
KNOWN_NAMES::const_iterator it = m_known_names.find(KnownStructKey(type_id, ptr));
if (it != m_known_names.end()) {
@@ -111,33 +107,29 @@ void DumpTarget::WriteIndent()
{
int num_spaces = 2 * m_indent;
if (num_spaces > 0) {
memset(m_out.GrowSizeNC(num_spaces), ' ', num_spaces);
m_out += std::string(num_spaces, ' ');
}
}
/** Write a line with indent at the beginning and \<LF\> at the end. */
void DumpTarget::WriteLine(const char *format, ...)
/** Write 'name = value' with indent and new-line. */
void DumpTarget::WriteValue(const char *name, int value)
{
WriteIndent();
va_list args;
va_start(args, format);
m_out.AddFormatL(format, args);
va_end(args);
m_out.AppendStr("\n");
m_out += std::string(name) + " = " + std::to_string(value) + "\n";
}
/** Write 'name = value' with indent and new-line. */
void DumpTarget::WriteValue(const char *name, const char *value_str)
{
WriteIndent();
m_out.AddFormat("%s = %s\n", name, value_str);
m_out += std::string(name) + " = " + value_str + "\n";
}
/** Write name & TileIndex to the output. */
void DumpTarget::WriteTile(const char *name, TileIndex tile)
{
WriteIndent();
m_out.AddFormat("%s = %s\n", name, TileStr(tile).Data());
m_out += std::string(name) + " = " + TileStr(tile) + "\n";
}
/**
@@ -146,12 +138,12 @@ void DumpTarget::WriteTile(const char *name, TileIndex tile)
void DumpTarget::BeginStruct(size_t type_id, const char *name, const void *ptr)
{
/* make composite name */
CStrA cur_name = GetCurrentStructName().Transfer();
if (cur_name.Size() > 0) {
std::string cur_name = GetCurrentStructName();
if (cur_name.size() > 0) {
/* add name delimiter (we use structured names) */
cur_name.AppendStr(".");
cur_name += ".";
}
cur_name.AppendStr(name);
cur_name += name;
/* put the name onto stack (as current struct name) */
m_cur_struct.push(cur_name);
@@ -160,7 +152,7 @@ void DumpTarget::BeginStruct(size_t type_id, const char *name, const void *ptr)
m_known_names.insert(KNOWN_NAMES::value_type(KnownStructKey(type_id, ptr), cur_name));
WriteIndent();
m_out.AddFormat("%s = {\n", name);
m_out += std::string(name) + " = {\n";
m_indent++;
}
@@ -171,11 +163,8 @@ void DumpTarget::EndStruct()
{
m_indent--;
WriteIndent();
m_out.AddFormat("}\n");
m_out += "}\n";
/* remove current struct name from the stack */
m_cur_struct.pop();
}
/** Just to silence an unsilencable GCC 4.4+ warning */
/* static */ ByteBlob::BlobHeader ByteBlob::hdrEmpty[] = {{0, 0}, {0, 0}};

View File

@@ -12,8 +12,7 @@
#include <map>
#include <stack>
#include "str.hpp"
#include <string>
#include "../direction_type.h"
#include "../signal_type.h"
@@ -67,9 +66,9 @@ inline typename ArrayT<T>::item_t ItemAtT(E idx, const T &t, typename ArrayT<T>:
* or t_unk when index is out of bounds.
*/
template <typename E, typename T>
inline CStrA ComposeNameT(E value, T &t, const char *t_unk, E val_inv, const char *name_inv)
inline std::string ComposeNameT(E value, T &t, const char *t_unk, E val_inv, const char *name_inv)
{
CStrA out;
std::string out;
if (value == val_inv) {
out = name_inv;
} else if (value == 0) {
@@ -77,18 +76,22 @@ inline CStrA ComposeNameT(E value, T &t, const char *t_unk, E val_inv, const cha
} else {
for (size_t i = 0; i < ArrayT<T>::length; i++) {
if ((value & (1 << i)) == 0) continue;
out.AddFormat("%s%s", (out.Size() > 0 ? "+" : ""), (const char*)t[i]);
out += (!out.empty() ? "+" : "");
out += t[i];
value &= ~(E)(1 << i);
}
if (value != 0) out.AddFormat("%s%s", (out.Size() > 0 ? "+" : ""), t_unk);
if (value != 0) {
out += (!out.empty() ? "+" : "");
out += t_unk;
}
}
return out.Transfer();
return out;
}
CStrA ValueStr(Trackdir td);
CStrA ValueStr(TrackdirBits td_bits);
CStrA ValueStr(DiagDirection dd);
CStrA ValueStr(SignalType t);
std::string ValueStr(Trackdir td);
std::string ValueStr(TrackdirBits td_bits);
std::string ValueStr(DiagDirection dd);
std::string ValueStr(SignalType t);
/** Class that represents the dump-into-string target. */
struct DumpTarget {
@@ -103,12 +106,6 @@ struct DumpTarget {
, m_ptr(ptr)
{}
KnownStructKey(const KnownStructKey &src)
{
m_type_id = src.m_type_id;
m_ptr = src.m_ptr;
}
bool operator<(const KnownStructKey &other) const
{
if ((size_t)m_ptr < (size_t)other.m_ptr) return true;
@@ -118,31 +115,31 @@ struct DumpTarget {
}
};
typedef std::map<KnownStructKey, CStrA> KNOWN_NAMES;
typedef std::map<KnownStructKey, std::string> KNOWN_NAMES;
CStrA m_out; ///< the output string
int m_indent; ///< current indent/nesting level
std::stack<CStrA> m_cur_struct; ///< here we will track the current structure name
KNOWN_NAMES m_known_names; ///< map of known object instances and their structured names
std::string m_out; ///< the output string
int m_indent; ///< current indent/nesting level
std::stack<std::string> m_cur_struct; ///< here we will track the current structure name
KNOWN_NAMES m_known_names; ///< map of known object instances and their structured names
DumpTarget()
: m_indent(0)
{}
static size_t& LastTypeId();
CStrA GetCurrentStructName();
bool FindKnownName(size_t type_id, const void *ptr, CStrA &name);
std::string GetCurrentStructName();
bool FindKnownName(size_t type_id, const void *ptr, std::string &name);
void WriteIndent();
void CDECL WriteLine(const char *format, ...) WARN_FORMAT(2, 3);
void WriteValue(const char *name, int value);
void WriteValue(const char *name, const char *value_str);
void WriteTile(const char *name, TileIndex t);
/** Dump given enum value (as a number and as named value) */
template <typename E> void WriteEnumT(const char *name, E e)
{
WriteValue(name, ValueStr(e).Data());
WriteValue(name, ValueStr(e).c_str());
}
void BeginStruct(size_t type_id, const char *name, const void *ptr);
@@ -155,13 +152,14 @@ struct DumpTarget {
if (s == nullptr) {
/* No need to dump nullptr struct. */
WriteLine("%s = <null>", name);
WriteValue(name, "<null>");
return;
}
CStrA known_as;
std::string known_as;
if (FindKnownName(type_id, s, known_as)) {
/* We already know this one, no need to dump it. */
WriteLine("%s = known_as.%s", name, known_as.Data());
std::string known_as_str = std::string("known_as.") + name;
WriteValue(name, known_as_str.c_str());
} else {
/* Still unknown, dump it */
BeginStruct(type_id, name, s);

View File

@@ -239,8 +239,7 @@ public:
/** non-const item search & removal */
void Pop(Titem_ &item)
{
bool ret = TryPop(item);
(void)ret; // assert only
[[maybe_unused]] bool ret = TryPop(item);
assert(ret);
}