summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Lutz <michi@icosahedron.de>2020-12-14 00:22:04 +0100
committerMichael Lutz <michi@icosahedron.de>2021-02-13 20:08:53 +0100
commit84636fc2af4dfc77b43479909273ebadb4f9b7cb (patch)
treea4e33da78ab32008675bef8df22e9ca6a2ffcf1a
parent7845434270524d4c777a6711d2e17ac89eeeeb89 (diff)
downloadopenttd-84636fc2af4dfc77b43479909273ebadb4f9b7cb.tar.xz
Codechange: Remove all remaining uses of cpp_offset.
-rw-r--r--src/newgrf_debug_gui.cpp24
-rw-r--r--src/stdafx.h7
-rw-r--r--src/table/newgrf_debug_data.h4
3 files changed, 16 insertions, 19 deletions
diff --git a/src/newgrf_debug_gui.cpp b/src/newgrf_debug_gui.cpp
index da3ddff0a..f82f4d93b 100644
--- a/src/newgrf_debug_gui.cpp
+++ b/src/newgrf_debug_gui.cpp
@@ -79,12 +79,14 @@ enum NIType {
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;
};
@@ -94,11 +96,11 @@ struct NIProperty {
* 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;
@@ -491,7 +493,7 @@ struct NewGRFInspectWindow : Window {
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;
@@ -525,7 +527,7 @@ struct NewGRFInspectWindow : Window {
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;
diff --git a/src/stdafx.h b/src/stdafx.h
index e5cb9205f..7ee3ce204 100644
--- a/src/stdafx.h
+++ b/src/stdafx.h
@@ -392,18 +392,13 @@ static_assert(SIZE_MAX >= UINT32_MAX);
*/
#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.
diff --git a/src/table/newgrf_debug_data.h b/src/table/newgrf_debug_data.h
index e31d09338..e35b2eecb 100644
--- a/src/table/newgrf_debug_data.h
+++ b/src/table/newgrf_debug_data.h
@@ -12,11 +12,11 @@
#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 */