Skip to content

Instantly share code, notes, and snippets.

@noorus
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noorus/9177965 to your computer and use it in GitHub Desktop.
Save noorus/9177965 to your computer and use it in GitHub Desktop.
unique_ptr for WinAPI HANDLEs
//! \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