From: Vadim Zeitlin <vadim@wxwidgets.org>
Date: Sun, 19 Jun 2005 23:48:43 +0000 (+0000)
Subject: added AutoHANDLE class to close a HANDLE automatically
X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/0cdd4e199e078e34076b7f28ea65be81ffb7960c?ds=sidebyside

added AutoHANDLE class to close a HANDLE automatically


git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@34708 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
---

diff --git a/include/wx/msw/private.h b/include/wx/msw/private.h
index a7f98d9804..9489b2fd68 100644
--- a/include/wx/msw/private.h
+++ b/include/wx/msw/private.h
@@ -193,6 +193,21 @@ extern LONG APIENTRY _EXPORT
     #define wxGetOSFHandle(fd) ((HANDLE)_get_osfhandle(fd))
 #endif
 
+// close the handle in the class dtor
+class AutoHANDLE
+{
+public:
+    wxEXPLICIT AutoHANDLE(HANDLE handle) : m_handle(handle) { }
+
+    bool IsOk() const { return m_handle != INVALID_HANDLE_VALUE; }
+    operator HANDLE() const { return m_handle; }
+
+    ~AutoHANDLE() { if ( IsOk() ) ::CloseHandle(m_handle); }
+
+protected:
+    HANDLE m_handle;
+};
+
 #if wxUSE_GUI
 
 #include <wx/gdicmn.h>