-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandles.hpp
354 lines (291 loc) · 7.59 KB
/
handles.hpp
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#if !defined HDR_W32_HANDLES_HPP
#define HDR_W32_HANDLES_HPP
#define WIN32_LEAN_AND_MEAN
#if !defined _WIN32_WINNT
#define _WIN32_WINNT 0x500
#endif
#include <windows.h>
#define THROW_HANDLE_WIN32_ERROR() throw w32::exception()
namespace w32
{
template < typename val_type, typename ret_type = BOOL >
struct func_helper
{
typedef ret_type (WINAPI * type)(val_type);
};
template < typename val_type >
struct base_close_policy
{
typedef val_type value_type;
static bool close( value_type hndl )
{
return true;
}
};
template < typename val_type, val_type invalid >
struct invalid_holder
{
typedef val_type value_type;
static const value_type invalid_value;
};
template< typename val_type, val_type invalid >
const val_type invalid_holder< val_type, invalid>::invalid_value = invalid;
template < typename val_type, val_type invalid >
struct base_clone_policy : invalid_holder<val_type, invalid>
{
static value_type clone( value_type hndl )
{
return hndl;
}
};
template<typename val_type, val_type invalid>
struct win32_clone_policy : invalid_holder<val_type, invalid>
{
static value_type clone( value_type hndl )
{
value_type ret = invalid;
HANDLE process = ::GetCurrentProcess();
if ( FALSE != ::DuplicateHandle(
process, hndl,
process, &ret,
0, FALSE, DUPLICATE_SAME_ACCESS ) )
return ret;
else
return invalid;
}
};
template<
typename val_type,
typename ret_type,
ret_type (WINAPI * close_fn)(val_type)
>
struct win32_close_policy : base_close_policy<val_type>
{
private :
using base_close_policy<val_type>::close;
public :
static bool close( value_type hndl )
{
return !!close_fn( hndl );
}
};
//*
template<
typename val_type,
typename close_pl,
typename clone_pl
>
struct full_handle_traits
//: invalid_holder<val_type, clone_pl::invalid_value>
{
typedef val_type value_type;
typedef close_pl close_policy_type;
typedef clone_pl clone_policy_type;
static const value_type invalid_value;
static bool close( value_type hndl )
{
return close_pl::close( hndl );
}
static value_type clone( value_type hndl )
{
return clone_pl::clone( hndl );
}
static value_type dupl( value_type hndl )
{
return clone( hndl );
}
template <typename T = value_type>
struct rebind : full_handle_traits<val_type, close_pl, clone_pl> {};
};
template<
typename val_type,
typename close_pl,
typename clone_pl
>
const val_type
full_handle_traits< val_type, close_pl, clone_pl>::invalid_value = clone_pl::invalid_value;
template < typename val_type >
struct base_handle_traits : full_handle_traits<
val_type,
win32_close_policy< val_type, BOOL, ::CloseHandle >,
win32_clone_policy< val_type, NULL >
>
{
};
template < typename val_type, BOOL ( WINAPI * close_fn )( val_type ) >
struct detailed_handle_traits : full_handle_traits<
val_type,
win32_close_policy< val_type, BOOL, close_fn >,
win32_clone_policy< val_type, NULL >
>
{
};
template < typename val_type, BOOL (WINAPI * close_fn)(val_type), val_type invalid >
struct auto_handle_traits : full_handle_traits<
val_type,
win32_close_policy< val_type, BOOL, close_fn >,
base_clone_policy< val_type, invalid >
>
{
};
//*/
//template < typename val_type >
struct find_detailed_handle_traits : public detailed_handle_traits< HANDLE, &::FindClose >
{};
template < typename val_type >
struct light_handle_traits : full_handle_traits<
val_type,
base_close_policy< val_type >,
base_clone_policy< val_type, base_handle_traits<val_type>::invalid_value >
>
{
};
template < typename val_type = HANDLE, class traits = base_handle_traits< val_type > >
struct handle_holder
{
typedef traits traits_type;
typedef typename traits_type::value_type value_type;
value_type handle_;
handle_holder() : handle_( traits_type::invalid_value ) {}
handle_holder( value_type h ) : handle_(h) {}
handle_holder( const handle_holder& other ) : handle_(traits_type::invalid_value)
{
if ( other.valid() )
this->handle_ = traits_type::clone( other.value() );
}
template < class traits_other >
handle_holder( const handle_holder< value_type, traits_other >& other ) : handle_(traits_type::invalid_value)
{
//attach( other.value() );
if ( other.valid() )
this->handle_ = traits_type::clone( other.value() );
}
const handle_holder& operator = ( const handle_holder& rhs )
{
this->attach(
rhs.valid() ? traits_type::clone( rhs.value() ) : traits_type::invalid_value
);
return *this;
}
const handle_holder& operator = ( value_type rhs )
{
this->attach( rhs );
return *this;
}
bool attach( value_type other )
{
this->close();
this->handle_ = other;
return true;
}
template < class traits_other >
void duplicate( const handle_holder< value_type, traits_other >& from )
{
this->attach(
traits_type::dupl( from.value() )
);
}
handle_holder< value_type, light_handle_traits<value_type> > duplicate() const
{
return handle_holder< light_handle_traits<value_type> >( traits_type::dupl( this->value() ) )
}
bool operator ! () const
{
return ( !this->valid() );
}
value_type value() const
{
return this->handle_;
}
bool valid() const
{
return ( this->handle_ != traits_type::invalid_value );
}
bool close() //const
{
if ( this->valid() )
{
if ( traits_type::close( this->handle_ ) )
this->handle_ = traits_type::invalid_value;
}
return !this->valid();
}
DWORD wait( DWORD ms = INFINITE ) const
{
return ::WaitForSingleObject( this->value(), ms );
}
template < class traits_other >
DWORD signal_and_wait(
const handle_holder< value_type, traits_other >& signal,
DWORD ms,
bool alert
) const
{
return ::SignalObjectAndWait( signal.value(), this->value(), ms, static_cast<BOOL>(alert) );
}
bool register_wait
( PHANDLE pnNewObject
, WAITORTIMERCALLBACK pfnCallback
, PVOID pContext = NULL
, ULONG nMilliseconds = INFINITE
, ULONG nFlags = WT_EXECUTEDEFAULT
) const
{
return !!::RegisterWaitForSingleObject(
pnNewObject, this->value(),
pfnCallback, pContext, nMilliseconds, nFlags
);
}
bool unregister_wait( HANDLE wait_object ) const
{
return !!::UnregisterWait( wait_object );
}
bool unregister_wait( HANDLE wait_object, HANDLE completion_event ) const
{
return !!::UnregisterWaitEx( wait_object, completion_event );
}
bool get_information( LPDWORD flags ) const
{
return !!::GetHandleInformation( this->value(), flags );
}
DWORD get_information() const
{
DWORD flags = 0;
if ( TRUE != ::GetHandleInformation( this->value(), &flags ) )
THROW_HANDLE_WIN32_ERROR();
return flags;
}
bool set_information( DWORD mask, DWORD flags ) const
{
return !!::SetHandleInformation( this->value(), mask, flags );
}
~handle_holder()
{
this->close();
}
};
template <
typename val_type = HANDLE,
typename func_helper<val_type>::type close_fn = &::CloseHandle,
val_type invalid = INVALID_HANDLE_VALUE >
struct auto_handle :
handle_holder<
val_type,
auto_handle_traits<val_type, close_fn, invalid >
>
{
typedef handle_holder<
val_type,
auto_handle_traits<val_type, close_fn, invalid >
> base_t;
using base_t::operator =;
using base_t::operator !;
auto_handle() {}
auto_handle( value_type h ) : base_t(h) {}
template < class traits_other >
auto_handle( const handle_holder< value_type, traits_other >& other ) : base_t(other) {}
private :
auto_handle( const auto_handle& other ) : base_t(other) {}
};
}
#endif /* HDR_W32_HANDLES_HPP */