From fb1c8d4fd8bb1cd78a230c8f940a45013b3378c7 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 2 Jun 2014 01:15:20 +0000 Subject: [PATCH] Allow wxMSW AutoHANDLE helper to be used with more kinds of handles. Make AutoHANDLE a template to allow specifying the invalid handle value, which is inconsistent across Win32 API and is INVALID_HANDLE_VALUE for most kinds of handles but 0 for some others, e.g. event object handles. See #16233. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@76653 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/msw/private.h | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/include/wx/msw/private.h b/include/wx/msw/private.h index ecfe7a6960..001d489037 100644 --- a/include/wx/msw/private.h +++ b/include/wx/msw/private.h @@ -185,18 +185,34 @@ extern LONG APIENTRY _EXPORT #endif // close the handle in the class dtor +template class AutoHANDLE { public: - wxEXPLICIT AutoHANDLE(HANDLE handle) : m_handle(handle) { } + wxEXPLICIT AutoHANDLE(HANDLE handle = INVALID_VALUE) : m_handle(handle) { } - bool IsOk() const { return m_handle != INVALID_HANDLE_VALUE; } + bool IsOk() const { return m_handle != INVALID_VALUE; } operator HANDLE() const { return m_handle; } - ~AutoHANDLE() { if ( IsOk() ) ::CloseHandle(m_handle); } + ~AutoHANDLE() { if ( IsOk() ) DoClose(); } + + void Close() + { + wxCHECK_RET(IsOk(), wxT("Handle must be valid")); + + DoClose(); + + m_handle = INVALID_VALUE; + } protected: - HANDLE m_handle; + void DoClose() + { + if ( !::CloseHandle(m_handle) ) + wxLogLastError(wxT("CloseHandle")); + } + + WXHANDLE m_handle; }; // a template to make initializing Windows styructs less painful: it zeroes all