diff options
Diffstat (limited to 'c/common.h')
-rw-r--r-- | c/common.h | 24 |
1 files changed, 23 insertions, 1 deletions
@@ -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) { |