From 8a669bc96ffdb9d0f6f54e464da11e3375c80a55 Mon Sep 17 00:00:00 2001 From: Michael Smith Date: Thu, 17 Apr 2025 01:39:10 +0100 Subject: Add type-safety to virtual calls and accessors This probably should have been the design from the start. It's still possible to use void pointers, and this is done in a couple of places for simplicity, but wherever possible, we have actual structs for things now. Additionally, in places where vtables are fiddled with, e.g. vtable hooks, we have actual struct definitions with vtable pointers so there's need for pointer-casting horror. --- src/accessor.h | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) (limited to 'src/accessor.h') diff --git a/src/accessor.h b/src/accessor.h index dcd9f28..ec63b3c 100644 --- a/src/accessor.h +++ b/src/accessor.h @@ -27,15 +27,16 @@ #endif /* - * Defines a function to offset a pointer from a struct/class to a field based + * Defines a function to offset a pointer from a struct/class to a member based * on a corresponding offset value off_. Such an offset would be * generally defined in gamedata. The function will be named getptr_. * Essentially allows easy access to an opaque thing contained with another * opaque thing. */ -#define DEF_PTR_ACCESSOR(type, field) \ - _ACCESSOR_UNUSED static inline typeof(type) *getptr_##field(void *obj) { \ - return mem_offset(obj, off_##field); \ +#define DEF_PTR_ACCESSOR(class, type, member) \ + _ACCESSOR_UNUSED static inline typeof(type) *getptr_##member( \ + typeof(class) *obj) { \ + return mem_offset(obj, off_##member); \ } /* @@ -43,21 +44,22 @@ * Requires that the field type is complete - that is, either scalar or a fully * defined struct. */ -#define DEF_ACCESSORS(type, field) \ - DEF_PTR_ACCESSOR(type, field) \ - _ACCESSOR_UNUSED static inline typeof(type) get_##field(const void *obj) { \ - return *getptr_##field((void *)obj); \ +#define DEF_ACCESSORS(class, type, member) \ + DEF_PTR_ACCESSOR(class, type, member) \ + _ACCESSOR_UNUSED static inline typeof(type) get_##member( \ + const typeof(class) *obj) { \ + return *getptr_##member((typeof(class) *)obj); \ } \ - _ACCESSOR_UNUSED static inline void set_##field(const void *obj, \ + _ACCESSOR_UNUSED static inline void set_##member(typeof(class) *obj, \ typeof(type) val) { \ - *getptr_##field((void *)obj) = val; \ + *getptr_##member(obj) = val; \ } /* - * Defines an array indexing function arrayidx_ which allows offsetting - * an opaque pointer by sz_ bytes. This size value would generally be - * defined in gamedata. Allows iterating over structs/classes with sizes that - * vary by game and are thus unknown at compile time. + * Defines an array indexing function arrayidx_ which allows + * offsetting an opaque pointer by sz_ bytes. This size value would + * generally be defined in gamedata. Allows iterating over structs/classes with + * sizes that vary by game and are thus unknown at compile time. * * Note that idx is signed so this can also be used for relative pointer offsets * in either direction. @@ -68,10 +70,10 @@ * single load of the global followed by repeated addition with no need for * multiplication, given that we use LTO, so... don't worry about it! It's fine! */ -#define DEF_ARRAYIDX_ACCESSOR(class) \ - _ACCESSOR_UNUSED static inline struct class *arrayidx_##class(void *array, \ - ssize idx) { \ - return mem_offset(array, idx * sz_##class); \ +#define DEF_ARRAYIDX_ACCESSOR(type, classname) \ + _ACCESSOR_UNUSED static inline typeof(type) *arrayidx_##classname( \ + typeof(type) *array, ssize idx) { \ + return mem_offset(array, idx * sz_##classname); \ } #endif -- cgit v1.2.3-54-g00ecf