diff options
author | John Ankarström <john@ankarstrom.se> | 2022-08-02 23:31:21 +0200 |
---|---|---|
committer | John Ankarström <john@ankarstrom.se> | 2022-08-02 23:31:21 +0200 |
commit | edce20642b6d177ee9877775cbfa9e05ebb4e404 (patch) | |
tree | f37c2d7121b3de0491561a5bd33ecf004d3a67d2 | |
parent | ad4a1e4ac6b9452bb28936623035d01bb03f36f2 (diff) | |
download | EpisodeBrowser-edce20642b6d177ee9877775cbfa9e05ebb4e404.tar.gz |
Replace maybe_make.
It seems unnecessary to throw exceptions when simply checking whether
a library exists.
-rw-r--r-- | c/common.cpp | 10 | ||||
-rw-r--r-- | c/common.h | 21 | ||||
-rw-r--r-- | c/main.cpp | 9 |
3 files changed, 17 insertions, 23 deletions
diff --git a/c/common.cpp b/c/common.cpp index fcf1da2..5d786da 100644 --- a/c/common.cpp +++ b/c/common.cpp @@ -1,3 +1,4 @@ +#include <utility> #include <windows.h> #include "common.h" @@ -104,6 +105,15 @@ const wchar_t* Win32Error::WhatW() const noexcept /* Library: Wrapper for loading and freeing dynamically linked libraries. */ +std::optional<Library> Library::Maybe(const wchar_t* const lib) +{ + HMODULE hModule = LoadLibrary(lib); + if (!hModule) return {}; + return Library(hModule); +} + +Library::Library(const HMODULE hModule) : m_hModule(hModule) {} + Library::Library(const wchar_t* const lib) { m_hModule = LoadLibrary(lib); @@ -5,12 +5,6 @@ #include <optional> #include <windows.h> -#ifdef UNICODE -#define WA "W" -#else -#define WA "A" -#endif - int EBMessageBox(const wchar_t* wszText, const wchar_t* wszCaption, unsigned uType); int GetRelativeCursorPos(HWND hWnd, POINT* pt); @@ -46,10 +40,12 @@ private: struct Library { + static std::optional<Library> Maybe(const wchar_t* lib); Library(const wchar_t* lib); ~Library(); template <class T> T* GetProcAddress(const char* szProc); private: + Library(HMODULE hModule); HMODULE m_hModule; }; @@ -65,19 +61,6 @@ inline int wszf(wchar_t (&buf)[N], const wchar_t* const fmt, T... xs) return _snwprintf_s(buf, N, _TRUNCATE, fmt, xs...); } -/* Create and return an object of type C. If construction fails, - * return nothing. The returned value must be checked before being - * used, as dereferencing is undefined if the value is empty. */ -template <typename T, typename... U> -std::optional<T> maybe_make(U... xs) -{ - try { - return T(xs...); - } catch (...) { - return {}; - } -} - /* Variable template for caching values from GetSystemMetrics. */ template <int I> auto Metric = GetSystemMetrics(I); @@ -1,4 +1,5 @@ #include <exception> +#include <stdexcept> #include <windows.h> #include <commctrl.h> #include <uxtheme.h> @@ -165,16 +166,16 @@ static LRESULT CALLBACK CBTProc(const int nCode, const WPARAM wParam, const LPAR g_hWnd = (HWND)wParam; /* Look up constants. */ - if (auto lib = maybe_make<Library>(L"User32.dll"); + if (auto lib = Library::Maybe(L"User32.dll"); auto GetDpiForWindow = lib->GetProcAddress<UINT(HWND)>("GetDpiForWindow")) g_dpi = GetDpiForWindow(g_hWnd); - if (auto lib = maybe_make<Library>(L"uxtheme.dll"); + if (auto lib = Library::Maybe(L"uxtheme.dll"); lib->GetProcAddress<void>("SetWindowTheme")) g_bThemes = 1; - if (auto lib = maybe_make<Library>(L"User32.dll"); - lib->GetProcAddress<void>("SystemParametersInfo" WA)) { + if (auto lib = Library::Maybe(L"User32.dll"); + lib->GetProcAddress<void>("SystemParametersInfoW")) { NONCLIENTMETRICS m; m.cbSize = sizeof(NONCLIENTMETRICS); require(SystemParametersInfo(SPI_GETNONCLIENTMETRICS, |