From 1d266aa8347fadfb0db34ae27df7b728c0ab2171 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Sat, 19 Apr 2014 02:38:32 -0700 Subject: Add a GCC-specific STATIC_UPCAST macro that checks the object type The check is compile time, and is functionally identical to the old/alternate version. --- OpenAL32/Include/alMain.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'OpenAL32/Include/alMain.h') diff --git a/OpenAL32/Include/alMain.h b/OpenAL32/Include/alMain.h index ad305c4e..8b66e4e7 100644 --- a/OpenAL32/Include/alMain.h +++ b/OpenAL32/Include/alMain.h @@ -348,8 +348,15 @@ static const union { #define DERIVE_FROM_TYPE(t) t t##_parent #define STATIC_CAST(to, obj) (&(obj)->to##_parent) +#ifdef __GNUC__ +#define STATIC_UPCAST(to, from, obj) __extension__({ \ + static_assert(__builtin_types_compatible_p(from, __typeof(*(obj))), \ + "Invalid upcast object from type"); \ + (to*)((char*)(obj) - offsetof(to, from##_parent)); \ +}) +#else #define STATIC_UPCAST(to, from, obj) ((to*)((char*)(obj) - offsetof(to, from##_parent))) - +#endif #define DECLARE_FORWARD(T1, T2, rettype, func) \ rettype T1##_##func(T1 *obj) \ -- cgit v1.2.3