diff options
author | Shauren <shauren.trinity@gmail.com> | 2024-10-01 21:03:44 +0200 |
---|---|---|
committer | Shauren <shauren.trinity@gmail.com> | 2024-10-01 21:03:44 +0200 |
commit | b13b5142f1009a71ff06786ac8c8db92891f566a (patch) | |
tree | da6b6ab4b0b47e5ac9b219507c9049c4b59d798b /src/common/Debugging | |
parent | 0d496b14d54d723090ea36760ee0e8d47e53891c (diff) |
Core/Utilities: Extend make_unique_ptr_with_deleter functionality to allow it to create deleters with compile time constant functions (reduces its size to just sizeof(void*))
Diffstat (limited to 'src/common/Debugging')
-rw-r--r-- | src/common/Debugging/Windows/WheatyExceptionReport.cpp | 57 |
1 files changed, 28 insertions, 29 deletions
diff --git a/src/common/Debugging/Windows/WheatyExceptionReport.cpp b/src/common/Debugging/Windows/WheatyExceptionReport.cpp index 5cb29c56aae..8bdce95aee5 100644 --- a/src/common/Debugging/Windows/WheatyExceptionReport.cpp +++ b/src/common/Debugging/Windows/WheatyExceptionReport.cpp @@ -6,6 +6,7 @@ #include "WheatyExceptionReport.h" #include "Errors.h" #include "GitRevision.h" +#include "Memory.h" #include <stdexcept> #include <algorithm> #include <utility> @@ -411,6 +412,12 @@ BOOL WheatyExceptionReport::_GetWindowsVersion(TCHAR* szVersion, DWORD cntMax) return TRUE; } +template <std::derived_from<IUnknown> T> +using com_unique_ptr_deleter = decltype(Trinity::unique_ptr_deleter<T*, [](T* ptr) { ptr->Release(); }>()); + +template <std::derived_from<IUnknown> T> +using com_unique_ptr = std::unique_ptr<T, com_unique_ptr_deleter<T>>; + BOOL WheatyExceptionReport::_GetWindowsVersionFromWMI(TCHAR* szVersion, DWORD cntMax) { // Step 1: -------------------------------------------------- @@ -419,10 +426,7 @@ BOOL WheatyExceptionReport::_GetWindowsVersionFromWMI(TCHAR* szVersion, DWORD cn if (FAILED(hres)) return FALSE; - std::shared_ptr<void> com(nullptr, [](void*) - { - CoUninitialize(); - }); + auto com = Trinity::make_unique_ptr_with_deleter<[](void*) { CoUninitialize(); }>(&hres); // Step 2: -------------------------------------------------- // Set general COM security levels -------------------------- @@ -443,7 +447,7 @@ BOOL WheatyExceptionReport::_GetWindowsVersionFromWMI(TCHAR* szVersion, DWORD cn // Step 3: --------------------------------------------------- // Obtain the initial locator to WMI ------------------------- - std::shared_ptr<IWbemLocator> loc = []() -> std::shared_ptr<IWbemLocator> + com_unique_ptr<IWbemLocator> loc([] { IWbemLocator* tmp = nullptr; HRESULT hres = CoCreateInstance( @@ -453,11 +457,8 @@ BOOL WheatyExceptionReport::_GetWindowsVersionFromWMI(TCHAR* szVersion, DWORD cn IID_IWbemLocator, reinterpret_cast<LPVOID*>(&tmp)); - if (FAILED(hres)) - return nullptr; - - return { tmp, [](IWbemLocator* ptr) { if (ptr) ptr->Release(); } }; - }(); + return SUCCEEDED(hres) ? tmp : nullptr; + }()); if (!loc) return FALSE; @@ -466,7 +467,7 @@ BOOL WheatyExceptionReport::_GetWindowsVersionFromWMI(TCHAR* szVersion, DWORD cn // Connect to the root\cimv2 namespace with // the current user and obtain pointer pSvc // to make IWbemServices calls. - std::shared_ptr<IWbemServices> svc = [loc]() ->std::shared_ptr<IWbemServices> + com_unique_ptr<IWbemServices> svc([&] { IWbemServices* tmp = nullptr; HRESULT hres = loc->ConnectServer( @@ -480,11 +481,8 @@ BOOL WheatyExceptionReport::_GetWindowsVersionFromWMI(TCHAR* szVersion, DWORD cn &tmp // pointer to IWbemServices proxy ); - if (FAILED(hres)) - return nullptr; - - return { tmp, [](IWbemServices* ptr) { if (ptr) ptr->Release(); } }; - }(); + return SUCCEEDED(hres) ? tmp : nullptr; + }()); if (!svc) return FALSE; @@ -509,7 +507,7 @@ BOOL WheatyExceptionReport::_GetWindowsVersionFromWMI(TCHAR* szVersion, DWORD cn // Use the IWbemServices pointer to make requests of WMI ---- // For example, get the name of the operating system - std::shared_ptr<IEnumWbemClassObject> queryResult = [svc]() -> std::shared_ptr<IEnumWbemClassObject> + com_unique_ptr<IEnumWbemClassObject> queryResult([&] { IEnumWbemClassObject* tmp = nullptr; HRESULT hres = svc->ExecQuery( @@ -519,11 +517,8 @@ BOOL WheatyExceptionReport::_GetWindowsVersionFromWMI(TCHAR* szVersion, DWORD cn nullptr, &tmp); - if (FAILED(hres)) - return nullptr; - - return { tmp, [](IEnumWbemClassObject* ptr) { if (ptr) ptr->Release(); } }; - }(); + return SUCCEEDED(hres) ? tmp : nullptr; + }()); BOOL result = FALSE; // Step 7: ------------------------------------------------- @@ -532,11 +527,17 @@ BOOL WheatyExceptionReport::_GetWindowsVersionFromWMI(TCHAR* szVersion, DWORD cn { do { - IWbemClassObject* fields = nullptr; - - ULONG rows = 0; - queryResult->Next(WBEM_INFINITE, 1, &fields, &rows); - if (!rows) + auto [fields, rows] = [&] + { + IWbemClassObject* fields = nullptr; + ULONG rows = 0; + HRESULT hres = queryResult->Next(WBEM_INFINITE, 1, &fields, &rows); + return SUCCEEDED(hres) && rows + ? std::pair(com_unique_ptr<IWbemClassObject>(fields), rows) + : std::pair(com_unique_ptr<IWbemClassObject>(), ULONG(0)); + }(); + + if (!fields || !rows) break; VARIANT field; @@ -558,8 +559,6 @@ BOOL WheatyExceptionReport::_GetWindowsVersionFromWMI(TCHAR* szVersion, DWORD cn } VariantClear(&field); - fields->Release(); - result = TRUE; } while (true); } |