From 0c3f6c415834daa9af50585ab3f970935e4e5fbb Mon Sep 17 00:00:00 2001 From: Reece Wilson Date: Thu, 17 Nov 2022 02:48:58 +0000 Subject: [PATCH] [+] Improved AuSwap [*] AuWin32CloseHandle should work without including windows.h [*] AU_CFG_ID_SHIP disables null pointer checks (shared ptr abi) --- Include/auROXTL/AU_Z.hpp | 4 +++ Include/auROXTL/auContainerUtils.hpp | 33 +++++++----------- Include/auROXTL/auSwapExchangeUtils.hpp | 45 ++++++++++++++++++++++++- Include/auROXTL/auWin32Utils.hpp | 20 +++++++---- 4 files changed, 75 insertions(+), 27 deletions(-) diff --git a/Include/auROXTL/AU_Z.hpp b/Include/auROXTL/AU_Z.hpp index 66461bb..a1da76c 100644 --- a/Include/auROXTL/AU_Z.hpp +++ b/Include/auROXTL/AU_Z.hpp @@ -54,6 +54,10 @@ // Memory, AuSPtr, and Friends //////////////////////////////////////////////////////////////////////////////////////////////// +#if defined(AU_CFG_ID_SHIP) + #define AURORA_ROXTL_NULL_POINTER_CHECKS_DISABLED +#endif + #if defined(AURORA_ROXTL_NULL_POINTER_CHECKS_DISABLED) #define _AURORA_NULLEXPT_ENABLE_UB #endif diff --git a/Include/auROXTL/auContainerUtils.hpp b/Include/auROXTL/auContainerUtils.hpp index f64e2e1..9c47988 100644 --- a/Include/auROXTL/auContainerUtils.hpp +++ b/Include/auROXTL/auContainerUtils.hpp @@ -14,27 +14,20 @@ namespace __audetail { - template - struct AuHascapacity - { - template static constexpr AuTrueType Test(decltype(&C::capacity)); - template static constexpr AuFalseType Test(...); - using type = decltype(Test(0)); - }; +#define _AUROXTL_DETAIAL_HAS(name) \ + template \ + struct AuHas ## name \ + { \ + template static constexpr AuTrueType Test(decltype(&C::name)); \ + template static constexpr AuFalseType Test(...); \ + using type = decltype(Test(0)); \ + }; \ + \ + template \ + constexpr inline bool AuHas ## name ## _v = AuHas ## name::type::value; - template - constexpr inline bool AuHascapacity_v = AuHascapacity::type::value; - - template - struct AuHasreserve - { - template static constexpr AuTrueType Test(decltype(&C::reserve)); - template static constexpr AuFalseType Test(...); - using type = decltype(Test(0)); - }; - - template - constexpr inline bool AuHasreserve_v = AuHasreserve::type::value; + _AUROXTL_DETAIAL_HAS(capacity) + _AUROXTL_DETAIAL_HAS(reserve) template constexpr inline bool AuIsPreallocatable_v = AuHascapacity_v && AuHasreserve_v; diff --git a/Include/auROXTL/auSwapExchangeUtils.hpp b/Include/auROXTL/auSwapExchangeUtils.hpp index 810486c..a75647a 100644 --- a/Include/auROXTL/auSwapExchangeUtils.hpp +++ b/Include/auROXTL/auSwapExchangeUtils.hpp @@ -6,6 +6,31 @@ ***/ #pragma once +namespace __audetail +{ + template + struct AuHasSwap + { + template static constexpr AuTrueType Test(decltype(static_cast(&C::Swap))); + template static constexpr AuFalseType Test(...); + using type = decltype(Test(0)); + }; + + template + struct AuHasswap + { + template static constexpr AuTrueType Test(decltype(static_cast(&C::swap))); + template static constexpr AuFalseType Test(...); + using type = decltype(Test(0)); + }; + + template + constexpr inline bool AuAuHasSwap_v = AuHasSwap::type::value; + + template + constexpr inline bool AuAuHasswap_v = AuHasswap::type::value; +} + template inline T AuExchange(T &obj, U &&newValue) { @@ -14,11 +39,29 @@ inline T AuExchange(T &obj, U &&newValue) return oldValue; } -#if !defined(AURORA_RUNTIME_SWAP) +#if !defined(AURORA_RUNTIME_SWAP) && !defined(AURORA_SWAP_COCKBLOCK) #define AURORA_RUNTIME_SWAP std::swap #endif + template inline void AuSwap(T &a, T &b) { + if constexpr (__audetail::AuAuHasSwap_v) + { + a.Swap(b); + return; + } + + if constexpr (__audetail::AuAuHasswap_v) + { + a.swap(b); + return; + } + +#if defined(AURORA_RUNTIME_SWAP) AURORA_RUNTIME_SWAP(a, b); + return; +#else + static_assert(false, "cannot swap T"); +#endif } \ No newline at end of file diff --git a/Include/auROXTL/auWin32Utils.hpp b/Include/auROXTL/auWin32Utils.hpp index d791d4b..e1b82d0 100644 --- a/Include/auROXTL/auWin32Utils.hpp +++ b/Include/auROXTL/auWin32Utils.hpp @@ -10,17 +10,25 @@ ***/ #pragma once -#if defined(AURORA_IS_MODERNNT_DERIVED) && (defined(_WINDOWS_) || defined(_OTHER_MS_MAIN_HEADER_GUARDS_HERE)) +#if defined(AURORA_IS_MODERNNT_DERIVED) -#define _AU_SAW_WIN32_EARLY - -static auline void AuWin32CloseHandle(HANDLE &handle) +extern "C" { - HANDLE local; + __declspec(dllimport) int __stdcall CloseHandle( + void *hObject + ); +} - if ((local = AuExchange(handle, INVALID_HANDLE_VALUE)) != INVALID_HANDLE_VALUE) +static auline void AuWin32CloseHandle(void *&handle) +{ + void *local; + + static void *const kInvalidHandle = (void *)((AuUInt)-1); + + if ((local = AuExchange(handle, kInvalidHandle)) != kInvalidHandle) { CloseHandle(local); } } + #endif \ No newline at end of file