From ab8b828c57c9b239a58634c470a35b282a879cbb Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Sat, 26 Feb 2022 07:42:28 -0800 Subject: [PATCH] Use a more C99-compliant function cast --- examples/alconvolve.c | 13 ++++++++++++- examples/allatency.c | 13 ++++++++++++- examples/almultireverb.c | 13 ++++++++++++- examples/alreverb.c | 13 ++++++++++++- 4 files changed, 48 insertions(+), 4 deletions(-) diff --git a/examples/alconvolve.c b/examples/alconvolve.c index d719abdb..8979e7a3 100644 --- a/examples/alconvolve.c +++ b/examples/alconvolve.c @@ -84,6 +84,17 @@ static LPALGETAUXILIARYEFFECTSLOTIV alGetAuxiliaryEffectSlotiv; static LPALGETAUXILIARYEFFECTSLOTF alGetAuxiliaryEffectSlotf; static LPALGETAUXILIARYEFFECTSLOTFV alGetAuxiliaryEffectSlotfv; +/* C doesn't allow casting between function and non-function pointer types, so + * with C99 we need to use a union to reinterpret the pointer type. Pre-C99 + * still needs to use a normal cast and live with the warning (C++ is fine with + * a regular reinterpret_cast). + */ +#if __STDC_VERSION__ >= 199901L +#define FUNCTION_CAST(T, ptr) (union{void *p; T f;}){ptr}.f +#else +#define FUNCTION_CAST(T, ptr) (T)(ptr) +#endif + /* This stuff defines a simple streaming player object, the same as alstream.c. * Comments are removed for brevity, see alstream.c for more details. @@ -438,7 +449,7 @@ int main(int argc, char **argv) } /* Define a macro to help load the function pointers. */ -#define LOAD_PROC(T, x) ((x) = (T)alGetProcAddress(#x)) +#define LOAD_PROC(T, x) ((x) = FUNCTION_CAST(T, alGetProcAddress(#x))) LOAD_PROC(LPALGENFILTERS, alGenFilters); LOAD_PROC(LPALDELETEFILTERS, alDeleteFilters); LOAD_PROC(LPALISFILTER, alIsFilter); diff --git a/examples/allatency.c b/examples/allatency.c index 5aa9e864..3b141ac0 100644 --- a/examples/allatency.c +++ b/examples/allatency.c @@ -51,6 +51,17 @@ static LPALGETSOURCEI64SOFT alGetSourcei64SOFT; static LPALGETSOURCE3I64SOFT alGetSource3i64SOFT; static LPALGETSOURCEI64VSOFT alGetSourcei64vSOFT; +/* C doesn't allow casting between function and non-function pointer types, so + * with C99 we need to use a union to reinterpret the pointer type. Pre-C99 + * still needs to use a normal cast and live with the warning (C++ is fine with + * a regular reinterpret_cast). + */ +#if __STDC_VERSION__ >= 199901L +#define FUNCTION_CAST(T, ptr) (union{void *p; T f;}){ptr}.f +#else +#define FUNCTION_CAST(T, ptr) (T)(ptr) +#endif + /* LoadBuffer loads the named audio file into an OpenAL buffer object, and * returns the new buffer ID. */ @@ -164,7 +175,7 @@ int main(int argc, char **argv) } /* Define a macro to help load the function pointers. */ -#define LOAD_PROC(T, x) ((x) = (T)alGetProcAddress(#x)) +#define LOAD_PROC(T, x) ((x) = FUNCTION_CAST(T, alGetProcAddress(#x))) LOAD_PROC(LPALSOURCEDSOFT, alSourcedSOFT); LOAD_PROC(LPALSOURCE3DSOFT, alSource3dSOFT); LOAD_PROC(LPALSOURCEDVSOFT, alSourcedvSOFT); diff --git a/examples/almultireverb.c b/examples/almultireverb.c index eb874061..447216d1 100644 --- a/examples/almultireverb.c +++ b/examples/almultireverb.c @@ -92,6 +92,17 @@ static LPALGETAUXILIARYEFFECTSLOTIV alGetAuxiliaryEffectSlotiv; static LPALGETAUXILIARYEFFECTSLOTF alGetAuxiliaryEffectSlotf; static LPALGETAUXILIARYEFFECTSLOTFV alGetAuxiliaryEffectSlotfv; +/* C doesn't allow casting between function and non-function pointer types, so + * with C99 we need to use a union to reinterpret the pointer type. Pre-C99 + * still needs to use a normal cast and live with the warning (C++ is fine with + * a regular reinterpret_cast). + */ +#if __STDC_VERSION__ >= 199901L +#define FUNCTION_CAST(T, ptr) (union{void *p; T f;}){ptr}.f +#else +#define FUNCTION_CAST(T, ptr) (T)(ptr) +#endif + /* LoadEffect loads the given initial reverb properties into the given OpenAL * effect object, and returns non-zero on success. @@ -519,7 +530,7 @@ int main(int argc, char **argv) } /* Define a macro to help load the function pointers. */ -#define LOAD_PROC(T, x) ((x) = (T)alGetProcAddress(#x)) +#define LOAD_PROC(T, x) ((x) = FUNCTION_CAST(T, alGetProcAddress(#x))) LOAD_PROC(LPALGENFILTERS, alGenFilters); LOAD_PROC(LPALDELETEFILTERS, alDeleteFilters); LOAD_PROC(LPALISFILTER, alIsFilter); diff --git a/examples/alreverb.c b/examples/alreverb.c index 56acdd82..0d62e210 100644 --- a/examples/alreverb.c +++ b/examples/alreverb.c @@ -67,6 +67,17 @@ static LPALGETAUXILIARYEFFECTSLOTIV alGetAuxiliaryEffectSlotiv; static LPALGETAUXILIARYEFFECTSLOTF alGetAuxiliaryEffectSlotf; static LPALGETAUXILIARYEFFECTSLOTFV alGetAuxiliaryEffectSlotfv; +/* C doesn't allow casting between function and non-function pointer types, so + * with C99 we need to use a union to reinterpret the pointer type. Pre-C99 + * still needs to use a normal cast and live with the warning (C++ is fine with + * a regular reinterpret_cast). + */ +#if __STDC_VERSION__ >= 199901L +#define FUNCTION_CAST(T, ptr) (union{void *p; T f;}){ptr}.f +#else +#define FUNCTION_CAST(T, ptr) (T)(ptr) +#endif + /* LoadEffect loads the given reverb properties into a new OpenAL effect * object, and returns the new effect ID. */ @@ -259,7 +270,7 @@ int main(int argc, char **argv) } /* Define a macro to help load the function pointers. */ -#define LOAD_PROC(T, x) ((x) = (T)alGetProcAddress(#x)) +#define LOAD_PROC(T, x) ((x) = FUNCTION_CAST(T, alGetProcAddress(#x))) LOAD_PROC(LPALGENEFFECTS, alGenEffects); LOAD_PROC(LPALDELETEEFFECTS, alDeleteEffects); LOAD_PROC(LPALISEFFECT, alIsEffect); -- 2.11.4.GIT