-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathSafeHandle.h
43 lines (38 loc) · 897 Bytes
/
SafeHandle.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#pragma once
class SafeHandle
{
public:
SafeHandle() noexcept : m_hHandle(INVALID_HANDLE_VALUE) {}
SafeHandle(HANDLE hHandle) noexcept : m_hHandle(hHandle) {}
SafeHandle(const SafeHandle &o) = delete;
SafeHandle(SafeHandle &&o) noexcept : m_hHandle(o.m_hHandle)
{
o.m_hHandle = INVALID_HANDLE_VALUE;
}
~SafeHandle() noexcept
{
Close();
}
SafeHandle &operator =(const SafeHandle &o) = delete;
SafeHandle &operator =(SafeHandle &&o) noexcept
{
m_hHandle = o.m_hHandle;
o.m_hHandle = INVALID_HANDLE_VALUE;
}
SafeHandle &operator =(HANDLE hHandle) noexcept
{
m_hHandle = hHandle;
}
bool IsValid() noexcept { return m_hHandle != INVALID_HANDLE_VALUE; }
HANDLE Get() noexcept { return m_hHandle; }
void Close() noexcept
{
if (IsValid())
{
CloseHandle(m_hHandle);
m_hHandle = INVALID_HANDLE_VALUE;
}
}
private:
HANDLE m_hHandle = INVALID_HANDLE_VALUE;
};