From ff53b8efce55f5668af61f13b656fdb54dee7755 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Tue, 19 Jul 2022 17:26:24 +0200 Subject: Check Windows API calls for errors more consistently. Some of the checks are likely redundant, but the Windows API documentation rarely makes it clear WHICH errors may be returned (and under which circumstances) rather than simply WHETHER errors may be returned (under any circumstances, including those that do not apply in the given case). --- c/common.h | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'c/common.h') diff --git a/c/common.h b/c/common.h index bffc9e5..acb8f26 100644 --- a/c/common.h +++ b/c/common.h @@ -44,7 +44,7 @@ T* Library::GetProcAddress(const char* const szProc) * return nothing. The returned value must be checked before being * used, as dereferencing is undefined if the value is empty. */ template -std::optional try_make(T ...args) +std::optional maybe_make(T ...args) { try { return C(args...); @@ -53,6 +53,28 @@ std::optional try_make(T ...args) } } +/* Call Windows API function and throw error if NULL is returned. */ +template +inline auto throw_nil(T ...args) +{ + auto r = f(args...); + if (!r) throw Win32Error(GetLastError()); + return r; +} + +/* Call Windows API function and show a warning if NULL is returned. */ +template +inline auto warn_nil(T ...args) +{ + decltype(f(args...)) r; + try { + r = throw_nil(args...); + } catch (Win32Error& e) { + EBMessageBox(e.twhat(), TEXT("System Error"), MB_ICONWARNING); + } + return r; +} + /* Return integer scaled for current DPI. */ inline int Dpi(const int i) { -- cgit v1.2.3