aboutsummaryrefslogtreecommitdiff
path: root/c/common.h
diff options
context:
space:
mode:
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)
{