aboutsummaryrefslogtreecommitdiff
path: root/c/common.h
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2022-07-19 17:26:24 +0200
committerJohn Ankarström <john@ankarstrom.se>2022-07-19 17:26:24 +0200
commitff53b8efce55f5668af61f13b656fdb54dee7755 (patch)
tree3b5802f9a1ac2b5bc7b32578528659a70c4c1b4c /c/common.h
parent0635058400597e43d698c87caf3d3ca4f802e0bd (diff)
downloadEpisodeBrowser-ff53b8efce55f5668af61f13b656fdb54dee7755.tar.gz
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).
Diffstat (limited to 'c/common.h')
-rw-r--r--c/common.h24
1 files changed, 23 insertions, 1 deletions
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 <class C, typename ...T>
-std::optional<C> try_make(T ...args)
+std::optional<C> maybe_make(T ...args)
{
try {
return C(args...);
@@ -53,6 +53,28 @@ std::optional<C> try_make(T ...args)
}
}
+/* Call Windows API function and throw error if NULL is returned. */
+template <auto f, typename ...T>
+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 <auto f, typename ...T>
+inline auto warn_nil(T ...args)
+{
+ decltype(f(args...)) r;
+ try {
+ r = throw_nil<f>(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)
{