aboutsummaryrefslogtreecommitdiff
path: root/c/common.h
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2022-07-20 20:15:22 +0200
committerJohn Ankarström <john@ankarstrom.se>2022-07-20 20:15:22 +0200
commit0d5736538a98b2c4001c45d073affc6bc0d772e5 (patch)
treeb14f4e8e7ee0c0ab64294c1c53b83ea8f44f3225 /c/common.h
parent400d4d12b7a70b81629a119844e7d5ad16aa6012 (diff)
downloadEpisodeBrowser-0d5736538a98b2c4001c45d073affc6bc0d772e5.tar.gz
Use GetLastError() as default for Win32Error.
Diffstat (limited to 'c/common.h')
-rw-r--r--c/common.h15
1 files changed, 8 insertions, 7 deletions
diff --git a/c/common.h b/c/common.h
index d84590d..24c98c8 100644
--- a/c/common.h
+++ b/c/common.h
@@ -15,6 +15,7 @@ int EBMessageBox(const TCHAR* tszText, const TCHAR* tszCaption, unsigned uType);
struct Win32Error : public std::exception
{
+ Win32Error();
Win32Error(DWORD dwErr);
~Win32Error();
template <typename T = char> const T* what() const noexcept;
@@ -57,19 +58,19 @@ std::optional<T> maybe_make(U... xs)
template <auto F, typename... T>
inline auto require(T... xs)
{
- auto r = F(xs...);
- if (!r) throw Win32Error(GetLastError());
- return r;
+ if (auto r = F(xs...)) return r;
+ else throw Win32Error();
}
/* Call Windows API function, showing a warning on NULL. */
template <auto F, typename... T>
inline auto prefer(T... xs)
{
- try {
- return require<F>(xs...);
- } catch (Win32Error& e) {
- EBMessageBox(e.what<TCHAR>(), TEXT("System Error"), MB_ICONWARNING);
+ if (auto r = F(xs...))
+ return r;
+ else {
+ EBMessageBox(Win32Error().what<TCHAR>(),
+ TEXT("System Error"), MB_ICONWARNING);
return (decltype(F(std::declval<T>()...)))NULL;
}
}