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
This commit is contained in:
Vadim Zeitlin 2014-06-02 01:15:20 +00:00
parent 793b8d519e
commit fb1c8d4fd8

View File

@ -185,18 +185,34 @@ extern LONG APIENTRY _EXPORT
#endif
// close the handle in the class dtor
template <int INVALID_VALUE = INVALID_HANDLE_VALUE>
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