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
|
/*
* mptOSException.h
* ----------------
* Purpose: platform-specific exception/signal handling
* Notes : (currently none)
* Authors: OpenMPT Devs
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
*/
#pragma once
#include "openmpt/all/BuildSettings.hpp"
#include "mptBaseMacros.h"
#include "mptBaseTypes.h"
#if MPT_OS_WINDOWS
#include <windows.h>
#endif // MPT_OS_WINDOWS
OPENMPT_NAMESPACE_BEGIN
#if MPT_OS_WINDOWS
namespace Windows
{
namespace SEH
{
struct Code
{
private:
DWORD m_Code;
public:
constexpr Code(DWORD code) noexcept
: m_Code(code)
{
return;
}
public:
constexpr DWORD code() const noexcept
{
return m_Code;
}
};
template <typename Tfn, typename Tfilter, typename Thandler>
auto TryFilterHandleThrow(const Tfn &fn, const Tfilter &filter, const Thandler &handler) -> decltype(fn())
{
static_assert(std::is_trivially_copy_assignable<decltype(fn())>::value);
static_assert(std::is_trivially_copy_constructible<decltype(fn())>::value);
static_assert(std::is_trivially_move_assignable<decltype(fn())>::value);
static_assert(std::is_trivially_move_constructible<decltype(fn())>::value);
DWORD code = 0;
__try
{
return fn();
} __except(filter(GetExceptionCode(), GetExceptionInformation()))
{
code = GetExceptionCode();
}
throw Windows::SEH::Code(code);
}
template <typename Tfn, typename Tfilter, typename Thandler>
void TryFilterHandleVoid(const Tfn &fn, const Tfilter &filter, const Thandler &handler)
{
static_assert(std::is_same<decltype(fn()), void>::value || std::is_trivially_copy_assignable<decltype(fn())>::value);
static_assert(std::is_same<decltype(fn()), void>::value || std::is_trivially_copy_constructible<decltype(fn())>::value);
static_assert(std::is_same<decltype(fn()), void>::value || std::is_trivially_move_assignable<decltype(fn())>::value);
static_assert(std::is_same<decltype(fn()), void>::value || std::is_trivially_move_constructible<decltype(fn())>::value);
__try
{
fn();
return;
} __except(filter(GetExceptionCode(), GetExceptionInformation()))
{
DWORD code = GetExceptionCode();
handler(code);
}
return;
}
template <typename Tfn, typename Tfilter, typename Thandler>
auto TryFilterHandleDefault(const Tfn &fn, const Tfilter &filter, const Thandler &handler, decltype(fn()) def = decltype(fn()){}) -> decltype(fn())
{
static_assert(std::is_trivially_copy_assignable<decltype(fn())>::value);
static_assert(std::is_trivially_copy_constructible<decltype(fn())>::value);
static_assert(std::is_trivially_move_assignable<decltype(fn())>::value);
static_assert(std::is_trivially_move_constructible<decltype(fn())>::value);
auto result = def;
__try
{
result = fn();
} __except(filter(GetExceptionCode(), GetExceptionInformation()))
{
DWORD code = GetExceptionCode();
result = handler(code);
}
return result;
}
template <typename Tfn>
auto TryReturnOrThrow(const Tfn &fn) -> decltype(fn())
{
return TryFilterHandleThrow(
fn,
[](auto code, auto eptr)
{
MPT_UNREFERENCED_PARAMETER(code);
MPT_UNREFERENCED_PARAMETER(eptr);
return EXCEPTION_EXECUTE_HANDLER;
},
[](auto code)
{
throw Windows::SEH::Code(code);
});
}
template <typename Tfn>
DWORD TryOrError(const Tfn &fn)
{
DWORD result = DWORD{0};
TryFilterHandleVoid(
fn,
[](auto code, auto eptr)
{
MPT_UNREFERENCED_PARAMETER(code);
MPT_UNREFERENCED_PARAMETER(eptr);
return EXCEPTION_EXECUTE_HANDLER;
},
[&result](auto code)
{
result = code;
});
return result;
}
template <typename Tfn>
auto TryReturnOrDefault(const Tfn &fn, decltype(fn()) def = decltype(fn()){}) -> decltype(fn())
{
return TryFilterHandleDefault(
fn,
[](auto code, auto eptr)
{
MPT_UNREFERENCED_PARAMETER(code);
MPT_UNREFERENCED_PARAMETER(eptr);
return EXCEPTION_EXECUTE_HANDLER;
},
[def](auto code)
{
MPT_UNREFERENCED_PARAMETER(code);
return def;
});
}
} // namspace SEH
} // namespace Windows
#endif // MPT_OS_WINDOWS
OPENMPT_NAMESPACE_END
|