-
Notifications
You must be signed in to change notification settings - Fork 2
/
wrappers.h
27 lines (24 loc) · 828 Bytes
/
wrappers.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#pragma once
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
template <typename THandle, typename TCloseFunc, TCloseFunc CloseFunc, THandle ZeroValue = THandle()>
class HandleWrapper
{
private:
THandle m_handle;
public:
HandleWrapper() : m_handle(ZeroValue) {}
HandleWrapper(THandle handle) : m_handle(handle) {}
~HandleWrapper()
{
if (m_handle != ZeroValue)
CloseFunc(m_handle);
}
HandleWrapper(const HandleWrapper&) = delete;
HandleWrapper& operator=(const HandleWrapper&) = delete;
operator THandle() { return m_handle; }
THandle* operator&() { return &m_handle; }
};
typedef HandleWrapper<HANDLE, decltype(CloseHandle), CloseHandle> Handle;
typedef HandleWrapper<HANDLE, decltype(CloseHandle), CloseHandle, INVALID_HANDLE_VALUE> HFile;
typedef HandleWrapper<HKEY, decltype(RegCloseKey), RegCloseKey> HKey;