forked from microsoft/cppwinrt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase_handle.h
More file actions
115 lines (91 loc) · 2.23 KB
/
base_handle.h
File metadata and controls
115 lines (91 loc) · 2.23 KB
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
WINRT_EXPORT namespace winrt
{
template <typename T>
struct handle_type
{
using type = typename T::type;
handle_type() noexcept = default;
explicit handle_type(type value) noexcept : m_value(value)
{
}
handle_type(handle_type&& other) noexcept : m_value(other.detach())
{
}
handle_type& operator=(handle_type&& other) noexcept
{
if (this != &other)
{
attach(other.detach());
}
return*this;
}
~handle_type() noexcept
{
close();
}
void close() noexcept
{
if (*this)
{
T::close(m_value);
m_value = T::invalid();
}
}
explicit operator bool() const noexcept
{
return T::invalid() != m_value;
}
type get() const noexcept
{
return m_value;
}
type* put() noexcept
{
close();
return &m_value;
}
void attach(type value) noexcept
{
close();
*put() = value;
}
type detach() noexcept
{
type value = m_value;
m_value = T::invalid();
return value;
}
friend void swap(handle_type& left, handle_type& right) noexcept
{
std::swap(left.m_value, right.m_value);
}
private:
type m_value = T::invalid();
};
struct handle_traits
{
using type = void*;
static void close(type value) noexcept
{
WINRT_VERIFY_(1, WINRT_IMPL_CloseHandle(value));
}
static constexpr type invalid() noexcept
{
return nullptr;
}
};
using handle = handle_type<handle_traits>;
struct file_handle_traits
{
using type = void*;
static void close(type value) noexcept
{
WINRT_VERIFY_(1, WINRT_IMPL_CloseHandle(value));
}
static type invalid() noexcept
{
return reinterpret_cast<type>(-1);
}
};
using file_handle = handle_type<file_handle_traits>;
}