]> git.saurik.com Git - wxWidgets.git/commitdiff
added AutoHANDLE class to close a HANDLE automatically
authorVadim Zeitlin <vadim@wxwidgets.org>
Sun, 19 Jun 2005 23:48:43 +0000 (23:48 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Sun, 19 Jun 2005 23:48:43 +0000 (23:48 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@34708 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/msw/private.h

index a7f98d9804b61bae98a34be387d478c1c1c88425..9489b2fd68c776e42eb7008efc14d4f30b721f8b 100644 (file)
@@ -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>