From 5feefe8ea4ffe43fda13ac647cf29ef829546328 Mon Sep 17 00:00:00 2001 From: Michael Hansen Date: Sat, 3 Jan 2015 17:08:54 -0800 Subject: [PATCH 1/2] Fix/remove some incorrect uses of the arrsize macro --- Sources/Plasma/CoreLib/HeadSpin.h | 3 +++ .../pnAsyncCoreExe/Private/Win32/pnAceW32Dns.cpp | 4 +++- .../PubUtilLib/plNetClientComm/plNetClientComm.cpp | 14 -------------- 3 files changed, 6 insertions(+), 15 deletions(-) diff --git a/Sources/Plasma/CoreLib/HeadSpin.h b/Sources/Plasma/CoreLib/HeadSpin.h index 69c6d7f7..b84140fc 100644 --- a/Sources/Plasma/CoreLib/HeadSpin.h +++ b/Sources/Plasma/CoreLib/HeadSpin.h @@ -239,6 +239,9 @@ inline double hsSwapEndianDouble(double dvalue) * ***/ #define arrsize(a) (sizeof(a) / sizeof((a)[0])) +/* TODO: Use this safer version when MSVC finally supports constexpr */ +//template +//constexpr size_t arrsize(_T (&)[_Sz]) { return _Sz; } /**************************************************************************** diff --git a/Sources/Plasma/NucleusLib/pnAsyncCoreExe/Private/Win32/pnAceW32Dns.cpp b/Sources/Plasma/NucleusLib/pnAsyncCoreExe/Private/Win32/pnAceW32Dns.cpp index ce0a9027..e3b4338e 100644 --- a/Sources/Plasma/NucleusLib/pnAsyncCoreExe/Private/Win32/pnAceW32Dns.cpp +++ b/Sources/Plasma/NucleusLib/pnAsyncCoreExe/Private/Win32/pnAceW32Dns.cpp @@ -102,7 +102,9 @@ static void LookupProcess (Lookup * lookup, unsigned error) { in_addr const * const * const inAddr = (in_addr **) host.h_addr_list; // allocate a buffer large enough to hold all the addresses - size_t count = arrsize(inAddr); + size_t count = 0; + while (inAddr[count]) + ++count; plNetAddress* addrs = new plNetAddress[count]; // fill in address data diff --git a/Sources/Plasma/PubUtilLib/plNetClientComm/plNetClientComm.cpp b/Sources/Plasma/PubUtilLib/plNetClientComm/plNetClientComm.cpp index d16634e7..d5d7447f 100644 --- a/Sources/Plasma/PubUtilLib/plNetClientComm/plNetClientComm.cpp +++ b/Sources/Plasma/PubUtilLib/plNetClientComm/plNetClientComm.cpp @@ -561,20 +561,6 @@ static void INetAuthFileListRequestCallback ( msg->Send(); } -//============================================================================ -static void INetCliAuthFileRequestCallback ( - ENetError result, - void * param, - const wchar_t filename[], - hsStream * writer -) { - plNetCommFileDownloadMsg * msg = new plNetCommFileDownloadMsg; - msg->result = result; - msg->writer = writer; - StrCopy(msg->filename, filename, arrsize(filename)); - msg->Send(); -} - //============================================================================ static void INetCliGameJoinAgeRequestCallback ( ENetError result, From 10a827ba52ab4a7c6f3ce3093ed5e1b28e01d60b Mon Sep 17 00:00:00 2001 From: Michael Hansen Date: Sat, 3 Jan 2015 17:17:54 -0800 Subject: [PATCH 2/2] Let CMake decide when to start using constexpr --- Sources/Plasma/CoreLib/HeadSpin.h | 10 ++++++---- cmake/CompilerChecks.cmake | 5 +++++ cmake/check_constexpr.cpp | 6 ++++++ cmake/hsCompilerSpecific.h.cmake | 2 ++ 4 files changed, 19 insertions(+), 4 deletions(-) create mode 100644 cmake/check_constexpr.cpp diff --git a/Sources/Plasma/CoreLib/HeadSpin.h b/Sources/Plasma/CoreLib/HeadSpin.h index b84140fc..c09e6102 100644 --- a/Sources/Plasma/CoreLib/HeadSpin.h +++ b/Sources/Plasma/CoreLib/HeadSpin.h @@ -238,10 +238,12 @@ inline double hsSwapEndianDouble(double dvalue) * StrPrintf(buffer, arrsize(buffer), "%u", value); * ***/ -#define arrsize(a) (sizeof(a) / sizeof((a)[0])) -/* TODO: Use this safer version when MSVC finally supports constexpr */ -//template -//constexpr size_t arrsize(_T (&)[_Sz]) { return _Sz; } +#ifdef HAVE_CONSTEXPR + template + constexpr size_t arrsize(_T(&)[_Sz]) { return _Sz; } +#else +# define arrsize(a) (sizeof(a) / sizeof((a)[0])) +#endif /**************************************************************************** diff --git a/cmake/CompilerChecks.cmake b/cmake/CompilerChecks.cmake index 8b7e21fb..727bcd07 100644 --- a/cmake/CompilerChecks.cmake +++ b/cmake/CompilerChecks.cmake @@ -51,6 +51,11 @@ try_compile(HAVE_GCC_DEPRECATED_ATTR ${PROJECT_BINARY_DIR} COMPILE_DEFINITIONS -DTRY_GCC_ATTR OUTPUT_VARIABLE OUTPUT) +# Look for C++11 constexpr support +try_compile(HAVE_CONSTEXPR ${PROJECT_BINARY_DIR} + ${PROJECT_SOURCE_DIR}/cmake/check_constexpr.cpp + OUTPUT_VARIABLE OUTPUT) + configure_file(${PROJECT_SOURCE_DIR}/cmake/hsCompilerSpecific.h.cmake ${PROJECT_BINARY_DIR}/hsCompilerSpecific.h) include_directories(${PROJECT_BINARY_DIR}) diff --git a/cmake/check_constexpr.cpp b/cmake/check_constexpr.cpp new file mode 100644 index 00000000..bff6467e --- /dev/null +++ b/cmake/check_constexpr.cpp @@ -0,0 +1,6 @@ +constexpr int foo() { return 5; } + +int main(int, char **) +{ + return foo(); +} diff --git a/cmake/hsCompilerSpecific.h.cmake b/cmake/hsCompilerSpecific.h.cmake index 02b8e5d1..242c4cf1 100644 --- a/cmake/hsCompilerSpecific.h.cmake +++ b/cmake/hsCompilerSpecific.h.cmake @@ -51,4 +51,6 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com #cmakedefine HAVE_CXX14_DEPRECATED_ATTR #cmakedefine HAVE_GCC_DEPRECATED_ATTR +#cmakedefine HAVE_CONSTEXPR + #endif