Changeset - r24756:c7f30e66c106
[Not reviewed]
master
0 3 0
Michael Lutz - 3 years ago 2020-12-13 23:22:04
michi@icosahedron.de
Codechange: Remove all remaining uses of cpp_offset.
3 files changed with 16 insertions and 19 deletions:
0 comments (0 inline, 0 general)
src/newgrf_debug_gui.cpp
Show inline comments
 
@@ -70,44 +70,46 @@ static inline uint GetInspectWindowNumbe
 
	return (feature << 24) | index;
 
}
 

	
 
/**
 
 * The type of a property to show. This is used to
 
 * provide an appropriate representation in the GUI.
 
 */
 
enum NIType {
 
	NIT_INT,   ///< The property is a simple integer
 
	NIT_CARGO, ///< The property is a cargo
 
};
 

	
 
typedef const void *NIOffsetProc(const void *b);
 

	
 
/** Representation of the data from a NewGRF property. */
 
struct NIProperty {
 
	const char *name;       ///< A (human readable) name for the property
 
	ptrdiff_t offset;       ///< Offset of the variable in the class
 
	byte read_size;         ///< Number of bytes (i.e. byte, word, dword etc)
 
	byte prop;              ///< The number of the property
 
	const char *name;          ///< A (human readable) name for the property
 
	NIOffsetProc *offset_proc; ///< Callback proc to get the actual variable address in memory
 
	byte read_size;            ///< Number of bytes (i.e. byte, word, dword etc)
 
	byte prop;                 ///< The number of the property
 
	byte type;
 
};
 

	
 

	
 
/**
 
 * Representation of the available callbacks with
 
 * information on when they actually apply.
 
 */
 
struct NICallback {
 
	const char *name; ///< The human readable name of the callback
 
	ptrdiff_t offset; ///< Offset of the variable in the class
 
	byte read_size;   ///< The number of bytes (i.e. byte, word, dword etc) to read
 
	byte cb_bit;      ///< The bit that needs to be set for this callback to be enabled
 
	uint16 cb_id;     ///< The number of the callback
 
	const char *name;          ///< The human readable name of the callback
 
	NIOffsetProc *offset_proc; ///< Callback proc to get the actual variable address in memory
 
	byte read_size;            ///< The number of bytes (i.e. byte, word, dword etc) to read
 
	byte cb_bit;               ///< The bit that needs to be set for this callback to be enabled
 
	uint16 cb_id;              ///< The number of the callback
 
};
 
/** Mask to show no bit needs to be enabled for the callback. */
 
static const int CBM_NO_BIT = UINT8_MAX;
 

	
 
/** Representation on the NewGRF variables. */
 
struct NIVariable {
 
	const char *name;
 
	byte var;
 
};
 

	
 
/** Helper class to wrap some functionality/queries in. */
 
class NIHelper {
 
@@ -482,25 +484,25 @@ struct NewGRFInspectWindow : Window {
 
			} else {
 
				this->DrawString(r, i++, "Persistent storage:");
 
			}
 
			assert(psa_size % 4 == 0);
 
			for (uint j = 0; j < psa_size; j += 4, psa += 4) {
 
				this->DrawString(r, i++, "  %i: %i %i %i %i", j, psa[0], psa[1], psa[2], psa[3]);
 
			}
 
		}
 

	
 
		if (nif->properties != nullptr) {
 
			this->DrawString(r, i++, "Properties:");
 
			for (const NIProperty *nip = nif->properties; nip->name != nullptr; nip++) {
 
				const void *ptr = (const byte *)base + nip->offset;
 
				const void *ptr = nip->offset_proc(base);
 
				uint value;
 
				switch (nip->read_size) {
 
					case 1: value = *(const uint8  *)ptr; break;
 
					case 2: value = *(const uint16 *)ptr; break;
 
					case 4: value = *(const uint32 *)ptr; break;
 
					default: NOT_REACHED();
 
				}
 

	
 
				StringID string;
 
				SetDParam(0, value);
 
				switch (nip->type) {
 
					case NIT_INT:
 
@@ -516,25 +518,25 @@ struct NewGRFInspectWindow : Window {
 
				}
 

	
 
				char buffer[64];
 
				GetString(buffer, string, lastof(buffer));
 
				this->DrawString(r, i++, "  %02x: %s (%s)", nip->prop, buffer, nip->name);
 
			}
 
		}
 

	
 
		if (nif->callbacks != nullptr) {
 
			this->DrawString(r, i++, "Callbacks:");
 
			for (const NICallback *nic = nif->callbacks; nic->name != nullptr; nic++) {
 
				if (nic->cb_bit != CBM_NO_BIT) {
 
					const void *ptr = (const byte *)base_spec + nic->offset;
 
					const void *ptr = nic->offset_proc(base_spec);
 
					uint value;
 
					switch (nic->read_size) {
 
						case 1: value = *(const uint8  *)ptr; break;
 
						case 2: value = *(const uint16 *)ptr; break;
 
						case 4: value = *(const uint32 *)ptr; break;
 
						default: NOT_REACHED();
 
					}
 

	
 
					if (!HasBit(value, nic->cb_bit)) continue;
 
					this->DrawString(r, i++, "  %03x: %s", nic->cb_id, nic->name);
 
				} else {
 
					this->DrawString(r, i++, "  %03x: %s (unmasked)", nic->cb_id, nic->name);
src/stdafx.h
Show inline comments
 
@@ -383,36 +383,31 @@ static_assert(SIZE_MAX >= UINT32_MAX);
 
 * @return The pointer past to the last element of the array
 
 */
 
#define endof(x) (&x[lengthof(x)])
 

	
 
/**
 
 * Get the last element of an fixed size array.
 
 *
 
 * @param x The pointer to the first element of the array
 
 * @return The pointer to the last element of the array
 
 */
 
#define lastof(x) (&x[lengthof(x) - 1])
 

	
 
#define cpp_offsetof(s, m)   (((size_t)&reinterpret_cast<const volatile char&>((((s*)(char*)8)->m))) - 8)
 
#if !defined(offsetof)
 
#	define offsetof(s, m) cpp_offsetof(s, m)
 
#endif /* offsetof */
 

	
 
/**
 
 * Gets the size of a variable within a class.
 
 * @param base     The class the variable is in.
 
 * @param variable The variable to get the size of.
 
 * @return the size of the variable
 
 */
 
#define cpp_sizeof(base, variable) (sizeof(((base*)8)->variable))
 
#define cpp_sizeof(base, variable) (sizeof(std::declval<base>().variable))
 

	
 
/**
 
 * Gets the length of an array variable within a class.
 
 * @param base     The class the variable is in.
 
 * @param variable The array variable to get the size of.
 
 * @return the length of the array
 
 */
 
#define cpp_lengthof(base, variable) (cpp_sizeof(base, variable) / cpp_sizeof(base, variable[0]))
 

	
 

	
 
/* take care of some name clashes on MacOS */
 
#if defined(__APPLE__)
src/table/newgrf_debug_data.h
Show inline comments
 
@@ -3,29 +3,29 @@
 
 * 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.
 
 * 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.
 
 * 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/>.
 
 */
 

	
 
/** @file newgrf_debug_data.h Data 'tables' for NewGRF debugging. */
 

	
 
#include "../newgrf_house.h"
 
#include "../newgrf_engine.h"
 
#include "../newgrf_roadtype.h"
 

	
 
/* Helper for filling property tables */
 
#define NIP(prop, base, variable, type, name) { name, (ptrdiff_t)cpp_offsetof(base, variable), cpp_sizeof(base, variable), prop, type }
 
#define NIP(prop, base, variable, type, name) { name, [] (const void *b) -> const void * { return std::addressof(static_cast<const base *>(b)->variable); }, cpp_sizeof(base, variable), prop, type }
 
#define NIP_END() { nullptr, 0, 0, 0, 0 }
 

	
 
/* Helper for filling callback tables */
 
#define NIC(cb_id, base, variable, bit) { #cb_id, (ptrdiff_t)cpp_offsetof(base, variable), cpp_sizeof(base, variable), bit, cb_id }
 
#define NIC(cb_id, base, variable, bit) { #cb_id, [] (const void *b) -> const void * { return std::addressof(static_cast<const base *>(b)->variable); }, cpp_sizeof(base, variable), bit, cb_id }
 
#define NIC_END() { nullptr, 0, 0, 0, 0 }
 

	
 
/* Helper for filling variable tables */
 
#define NIV(var, name) { name, var }
 
#define NIV_END() { nullptr, 0 }
 

	
 

	
 
/*** NewGRF Vehicles ***/
 

	
 
#define NICV(cb_id, bit) NIC(cb_id, Engine, info.callback_mask, bit)
 
static const NICallback _nic_vehicles[] = {
 
	NICV(CBID_VEHICLE_VISUAL_EFFECT,         CBM_VEHICLE_VISUAL_EFFECT),
0 comments (0 inline, 0 general)