-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathHandle.h
201 lines (157 loc) · 4.09 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#pragma once
#define WIN32_LEAN_AND_MEAN
// WIN32_FAT_AND_STUPID
#include <utility>
#include <stdio.h>
#include <Windows.h>
template<typename T>
struct HandleTraits {
using type = T;
static type default_value() noexcept {
return T{};
}
};
struct FileHandleTraits : public HandleTraits<FILE*> {
static void close(FILE* file) noexcept {
if(file) {
fclose(file);
}
}
};
struct ThreadHandleTraits : public HandleTraits<HANDLE> {
static void close(HANDLE handle) noexcept {
if(handle) {
CloseHandle(handle);
}
}
};
struct ModuleHandleTraits : public HandleTraits<HMODULE> {
static void close(HMODULE handle) noexcept {
if(handle) {
FreeLibrary(handle);
}
}
};
struct FindHandleTraits : public HandleTraits<HANDLE> {
static HANDLE default_value() noexcept {
return INVALID_HANDLE_VALUE;
}
static void close(HANDLE handle) noexcept {
if(handle != INVALID_HANDLE_VALUE) {
FindClose(handle);
}
}
};
struct LocalAllocHandleTraits : public HandleTraits<HLOCAL> {
static void close(HLOCAL handle) noexcept {
LocalFree(handle);
}
};
// owns a resource. not copyable, but movable.
template <typename Traits>
struct Handle {
using value_type = typename Traits::type;
Handle() noexcept = default;
explicit Handle(value_type value) noexcept
: Value(value)
{ }
Handle(Handle const&) = delete;
Handle(Handle&& other) noexcept
: Value(other.release())
{ }
~Handle() noexcept {
if(*this) {
Traits::close(this->Value);
}
}
Handle& operator = (Handle const&) = delete;
Handle& operator = (Handle&& other) noexcept {
this->reset(other.release());
return *this;
}
explicit operator bool() const noexcept {
return this->Value != Traits::default_value();
}
operator value_type () const noexcept {
return this->Value;
}
value_type get() const noexcept {
return this->Value;
}
value_type release() noexcept {
return std::exchange(this->Value, Traits::default_value());
}
void reset(value_type value) noexcept {
Handle(this->Value);
this->Value = value;
}
void clear() noexcept {
Handle(std::move(*this));
}
value_type* set() noexcept {
this->clear();
return &this->Value;
}
void swap(Handle& other) noexcept {
using std::swap;
swap(this->Value, other.Value);
}
friend void swap(Handle& lhs, Handle& rhs) noexcept {
lhs.swap(rhs);
}
private:
value_type Value{ Traits::default_value() };
};
using FileHandle = Handle<FileHandleTraits>;
using ThreadHandle = Handle<ThreadHandleTraits>;
using ModuleHandle = Handle<ModuleHandleTraits>;
using FindHandle = Handle<FindHandleTraits>;
using LocalAllocHandle = Handle<LocalAllocHandleTraits>;
struct VirtualMemoryHandle {
VirtualMemoryHandle() noexcept = default;
VirtualMemoryHandle(HANDLE process, LPVOID address, SIZE_T size) noexcept
: Process(process)
{
if(process && size) {
this->Value = VirtualAllocEx(process, address, size, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);
}
}
VirtualMemoryHandle(LPVOID allocated, HANDLE process) noexcept
: Value(allocated), Process(process)
{ }
VirtualMemoryHandle(VirtualMemoryHandle const&) = delete;
VirtualMemoryHandle(VirtualMemoryHandle&& other) noexcept :
Value(std::exchange(other.Value, nullptr)),
Process(std::exchange(other.Process, nullptr))
{ }
~VirtualMemoryHandle() noexcept {
if(this->Value && this->Process) {
VirtualFreeEx(this->Process, this->Value, 0, MEM_RELEASE);
}
}
VirtualMemoryHandle& operator = (VirtualMemoryHandle const&) = delete;
VirtualMemoryHandle& operator = (VirtualMemoryHandle&& other) noexcept {
VirtualMemoryHandle{ std::move(other) }.swap(*this);
return *this;
}
operator BYTE*() const noexcept {
return this->get();
}
BYTE* get() const noexcept {
return static_cast<BYTE*>(this->Value);
}
void clear() noexcept {
VirtualMemoryHandle(std::move(*this));
}
void swap(VirtualMemoryHandle& other) noexcept {
using std::swap;
swap(this->Value, other.Value);
swap(this->Process, other.Process);
}
friend void swap(VirtualMemoryHandle& lhs, VirtualMemoryHandle& rhs) noexcept {
lhs.swap(rhs);
}
private:
LPVOID Value{ nullptr };
HANDLE Process{ nullptr };
};