summaryrefslogtreecommitdiff
path: root/src/misc/dbg_helpers.h
blob: 7cbe57810c3e19c81fcf462d7fc51a8a7f8d6670 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/* $Id$ */

/** @file dbg_helpers.h Functions to be used for debug printings. */

#ifndef DBG_HELPERS
#define DBG_HELPERS

#include <new>
#include <map>
#include <stack>

#include "blob.hpp"
#include "str.hpp"

/** Helper template class that provides C array length and item type */
template <typename T> struct ArrayT;

/** Helper template class that provides C array length and item type */
template <typename T, size_t N> struct ArrayT<T[N]> {
	static const size_t length = N;
	typedef T item_t;
};


/**
* Helper template function that returns item of array at given index
* or t_unk when index is out of bounds.
*/
template <typename E, typename T>
inline typename ArrayT<T>::item_t ItemAtT(E idx, T &t, typename ArrayT<T>::item_t t_unk)
{
	if ((size_t)idx >= ArrayT<T>::length) {
		return t_unk;
	}
	return t[idx];
}

/**
* Helper template function that returns item of array at given index
* or t_inv when index == idx_inv
* or t_unk when index is out of bounds.
*/
template <typename E, typename T>
inline typename ArrayT<T>::item_t ItemAtT(E idx, T &t, typename ArrayT<T>::item_t t_unk, E idx_inv, typename ArrayT<T>::item_t t_inv)
{
	if ((size_t)idx < ArrayT<T>::length) {
		return t[idx];
	}
	if (idx == idx_inv) {
		return t_inv;
	}
	return t_unk;
}

/**
* Helper template function that returns compound bitfield name that is
* concatenation of names of each set bit in the given value
* or t_inv when index == idx_inv
* 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)
{
	CStrA out;
	if (value == val_inv) {
		out = name_inv;
	} else if (value == 0) {
		out = "<none>";
	} else {
		for (size_t i = 0; i < ArrayT<T>::length; i++) {
			if ((value & (1 << i)) == 0) continue;
			out.AddFormat("%s%s", (out.Size() > 0 ? "+" : ""), t[i]);
			value &= ~(E)(1 << i);
		}
		if (value != 0) out.AddFormat("%s%s", (out.Size() > 0 ? "+" : ""), t_unk);
	}
	return out.Transfer();
}

CStrA ValueStr(Trackdir td);
CStrA ValueStr(TrackdirBits td_bits);
CStrA ValueStr(DiagDirection dd);
CStrA ValueStr(SignalType t);

/** Class that represents the dump-into-string target. */
struct DumpTarget {

	/** Used as a key into map of known object instances. */
	struct KnownStructKey {
		size_t      m_type_id;
		const void *m_ptr;

		KnownStructKey(size_t type_id, const void *ptr)
			: m_type_id(type_id)
			, 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;
			if ((size_t)m_ptr > (size_t)other.m_ptr) return false;
			if (m_type_id < other.m_type_id) return true;
			return false;
		}
	};

	typedef std::map<KnownStructKey, CStrA> 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

	DumpTarget()
		: m_indent(0)
	{}

	static size_t& LastTypeId();
	CStrA GetCurrentStructName();
	bool FindKnownName(size_t type_id, const void *ptr, CStrA &name);

	void WriteIndent();

	void WriteLine(const char *format, ...);
	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());
	}

	void BeginStruct(size_t type_id, const char *name, const void *ptr);
	void EndStruct();

	/** Dump nested object (or only its name if this instance is already known). */
	template <typename S> void WriteStructT(const char *name, const S *s)
	{
		static size_t type_id = ++LastTypeId();

		if (s == NULL) {
			/* No need to dump NULL struct. */
			WriteLine("%s = <null>", name);
			return;
		}
		CStrA 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());
		} else {
			/* Still unknown, dump it */
			BeginStruct(type_id, name, s);
			s->Dump(*this);
			EndStruct();
		}
	}
};

#endif /* DBG_HELPERS */