-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandle.h
174 lines (142 loc) · 4.21 KB
/
handle.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#pragma once
#include <windows.h>
#include "debug.h"
namespace KennyKerr
{
template <typename Traits>
class unique_handle
{
using pointer = typename Traits::pointer;
pointer m_value;
auto close() throw() -> void
{
if (*this)
{
Traits::close(m_value);
}
}
public:
unique_handle(unique_handle const &) = delete;
auto operator=(unique_handle const &) -> unique_handle & = delete;
explicit unique_handle(pointer value = Traits::invalid()) throw() :
m_value { value }
{
}
unique_handle(unique_handle && other) throw() :
m_value { other.release() }
{
}
auto operator=(unique_handle && other) throw() -> unique_handle &
{
if (this != &other)
{
reset(other.release());
}
return *this;
}
~unique_handle() throw()
{
close();
}
explicit operator bool() const throw()
{
return m_value != Traits::invalid();
}
auto get() const throw() -> pointer
{
return m_value;
}
auto get_address_of() throw() -> pointer *
{
ASSERT(!*this);
return &m_value;
}
auto release() throw() -> pointer
{
auto value = m_value;
m_value = Traits::invalid();
return value;
}
auto reset(pointer value = Traits::invalid()) throw() -> bool
{
if (m_value != value)
{
close();
m_value = value;
}
return static_cast<bool>(*this);
}
auto swap(unique_handle<Traits> & other) throw() -> void
{
std::swap(m_value, other.m_value);
}
};
template <typename Traits>
auto swap(unique_handle<Traits> & left,
unique_handle<Traits> & right) throw() -> void
{
left.swap(right);
}
template <typename Traits>
auto operator==(unique_handle<Traits> const & left,
unique_handle<Traits> const & right) throw() -> bool
{
return left.get() == right.get();
}
template <typename Traits>
auto operator!=(unique_handle<Traits> const & left,
unique_handle<Traits> const & right) throw() -> bool
{
return left.get() != right.get();
}
template <typename Traits>
auto operator<(unique_handle<Traits> const & left,
unique_handle<Traits> const & right) throw() -> bool
{
return left.get() < right.get();
}
template <typename Traits>
auto operator>=(unique_handle<Traits> const & left,
unique_handle<Traits> const & right) throw() -> bool
{
return left.get() >= right.get();
}
template <typename Traits>
auto operator>(unique_handle<Traits> const & left,
unique_handle<Traits> const & right) throw() -> bool
{
return left.get() > right.get();
}
template <typename Traits>
auto operator<=(unique_handle<Traits> const & left,
unique_handle<Traits> const & right) throw() -> bool
{
return left.get() <= right.get();
}
struct null_handle_traits
{
using pointer = HANDLE;
static auto invalid() throw() -> pointer
{
return nullptr;
}
static auto close(pointer value) throw() -> void
{
VERIFY(CloseHandle(value));
}
};
struct invalid_handle_traits
{
using pointer = HANDLE;
static auto invalid() throw() -> pointer
{
return INVALID_HANDLE_VALUE;
}
static auto close(pointer value) throw() -> void
{
VERIFY(CloseHandle(value));
}
};
using null_handle = unique_handle<null_handle_traits>;
using invalid_handle = unique_handle<invalid_handle_traits>;
}