Last active
August 29, 2015 13:56
-
-
Save noorus/9177965 to your computer and use it in GitHub Desktop.
unique_ptr for WinAPI HANDLEs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//! \class SafeHandle | |
//! Unique_ptr wrapper for WinAPI handles. | |
class SafeHandle: public std::unique_ptr<std::remove_pointer<HANDLE>::type,void(*)( HANDLE )> | |
{ | |
public: | |
SafeHandle( HANDLE handle ): unique_ptr( handle, &SafeHandle::close ) | |
{ | |
} | |
operator HANDLE() | |
{ | |
return get(); | |
} | |
const bool valid() const | |
{ | |
return ( get() != INVALID_HANDLE_VALUE ); | |
} | |
private: | |
static void close( HANDLE handle ) | |
{ | |
if ( handle != INVALID_HANDLE_VALUE ) | |
CloseHandle( handle ); | |
} | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment