From c9852a870b355499d4de32d89e217f65dee25ae0 Mon Sep 17 00:00:00 2001 From: Armin Novak Date: Sat, 29 Jul 2023 12:48:35 +0200 Subject: [PATCH] [winpr] use C++ compatible casts use a macro for casts to avoid C++ warnings --- winpr/include/winpr/crt.h | 8 +++++++- winpr/include/winpr/error.h | 22 ++++++++++++++-------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/winpr/include/winpr/crt.h b/winpr/include/winpr/crt.h index 81b983d1e..6937f9ff1 100644 --- a/winpr/include/winpr/crt.h +++ b/winpr/include/winpr/crt.h @@ -104,7 +104,13 @@ static INLINE UINT64 _byteswap_uint64(UINT64 _val) static INLINE UINT16 _byteswap_ushort(UINT16 _val) { - return (UINT16)(((_val) >> 8U) | ((_val) << 8U)); +#ifdef __cplusplus +#define winpr_byteswap_cast(t, val) static_cast(val) +#else +#define winpr_byteswap_cast(t, val) (t)(val) +#endif + return winpr_byteswap_cast(UINT16, ((_val) >> 8U) | ((_val) << 8U)); +#undef winpr_byteswap_cast } #endif /* (__GNUC__ > 4) || ... */ diff --git a/winpr/include/winpr/error.h b/winpr/include/winpr/error.h index 2ceed5795..314ea3035 100644 --- a/winpr/include/winpr/error.h +++ b/winpr/include/winpr/error.h @@ -144,12 +144,17 @@ #pragma clang diagnostic ignored "-Wreserved-id-macro" #endif +#ifdef __cplusplus +#define ERROR_CAST(t, val) static_cast(val) +#else +#define ERROR_CAST(t, val) (t)(val) +#endif static INLINE HRESULT HRESULT_FROM_WIN32(unsigned long x) { - HRESULT hx = (HRESULT)x; + HRESULT hx = ERROR_CAST(HRESULT, x); if (hx <= 0) return hx; - return (HRESULT)((((x)&0x0000FFFF) | (FACILITY_WIN32 << 16) | 0x80000000)); + return ERROR_CAST(HRESULT, (((x)&0x0000FFFF) | (FACILITY_WIN32 << 16) | 0x80000000)); } #if defined(__clang__) @@ -160,18 +165,19 @@ static INLINE HRESULT HRESULT_FROM_WIN32(unsigned long x) #define SUCCEEDED(hr) (((hr)) >= 0) #define FAILED(hr) (((hr)) < 0) -#define IS_ERROR(Status) (((unsigned long)(Status)) >> 31 == SEVERITY_ERROR) +#define IS_ERROR(Status) ((ERROR_CAST(unsigned long, Status)) >> 31 == SEVERITY_ERROR) -#define MAKE_HRESULT(sev, fac, code) \ - ((HRESULT)(((unsigned long)(sev) << 31) | ((unsigned long)(fac) << 16) | \ - ((unsigned long)(code)))) +#define MAKE_HRESULT(sev, fac, code) \ + ((HRESULT)((ERROR_CAST(unsigned long, sev) << 31) | (ERROR_CAST(unsigned long, fac) << 16) | \ + (ERROR_CAST(unsigned long, code)))) #define SCODE_CODE(sc) ((sc)&0xFFFF) #define SCODE_FACILITY(sc) (((sc) >> 16) & 0x1FFF) #define SCODE_SEVERITY(sc) (((sc) >> 31) & 0x1) -#define MAKE_SCODE(sev, fac, code) \ - ((SCODE)(((unsigned long)(sev) << 31) | ((unsigned long)(fac) << 16) | ((unsigned long)(code)))) +#define MAKE_SCODE(sev, fac, code) \ + ((SCODE)((ERROR_CAST(unsigned long, sev) << 31) | (ERROR_CAST(unsigned long, fac) << 16) | \ + (ERROR_CAST(unsigned long, code)))) #define S_OK (0L) #define S_FALSE (1L)