From 2f7b69d6d4cf18ca9ca04d9a44aaa6871ce51160 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Wed, 7 Sep 2022 00:40:26 +0200 Subject: Improve error handling. --- c/win32.h | 54 ------------------------------------------------------ 1 file changed, 54 deletions(-) (limited to 'c/win32.h') diff --git a/c/win32.h b/c/win32.h index c18e70f..0473f6c 100644 --- a/c/win32.h +++ b/c/win32.h @@ -6,8 +6,6 @@ #include #include -#include "err.h" - /* Convert narrow to wide string. */ std::wstring WideFromNarrow(const std::string_view src, const int cp = CP_UTF8); @@ -23,39 +21,6 @@ int GetRelativeCursorPos(HWND hWnd, POINT* pt) noexcept; /* Cached values from GetSystemMetrics. */ template auto Metric = GetSystemMetrics(I); -struct WideException : public std::exception -{ - virtual const char* what() const noexcept = 0; - virtual const wchar_t* What() const noexcept = 0; -}; - -/* Exception for Windows API errors. */ -struct Win32Error : public WideException -{ - Win32Error(DWORD code = GetLastError(), HMODULE hModule = nullptr) noexcept; - ~Win32Error() noexcept; - const char* what() const noexcept override; - const wchar_t* What() const noexcept override; - DWORD code; -private: - HMODULE hModule; - char* m_szMsg = nullptr; - wchar_t* m_wszMsg = nullptr; -}; - -/* Exception for extended Wininet errors. */ -struct InternetError : public WideException -{ - InternetError(DWORD codeSystem = GetLastError()); - ~InternetError() noexcept; - const char* what() const noexcept override; - const wchar_t* What() const noexcept override; - DWORD code; -private: - char* m_szMsg = nullptr; - wchar_t* m_wszMsg = nullptr; -}; - /* Wrapper for loading and freeing dynamically linked libraries. */ struct Library { @@ -78,25 +43,6 @@ T* Library::GetProcAddress(const char* const szProc) noexcept return (T*)(void*)::GetProcAddress(m_hModule, szProc); } -/* Check result of Windows API call, throwing error on null. */ -template -inline T Require(const T x) -{ - if (!x) Err(WINDOWS, L"System error: %s"); - return x; -} - -/* Check result of Windows API call, showing a warning on null. */ -template -inline T Prefer(const T x) -{ - if (!x) { - EBMessageBox(Win32Error().What(), L"System Error", MB_ICONWARNING); - return (T)0; - } - return x; -} - /* Return integer scaled for current DPI. */ inline int Dpi(const int i) { -- cgit v1.2.3