aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2022-07-15 03:04:22 +0200
committerJohn Ankarström <john@ankarstrom.se>2022-07-15 03:04:22 +0200
commit7153a258427d3e401914de800bfdf1c7165cab71 (patch)
tree7aa97f2c9e4dd97b6c67bedb09ef03d7f2b3f19d
parent0e56160e859d32adffb7c9df3cd78cde0bff8df8 (diff)
downloadEpisodeBrowser-7153a258427d3e401914de800bfdf1c7165cab71.tar.gz
Replace Library::Load with try_make_unique template.
-rw-r--r--c/common.cpp16
-rw-r--r--c/defs.h13
-rw-r--r--c/main.cpp8
3 files changed, 21 insertions, 16 deletions
diff --git a/c/common.cpp b/c/common.cpp
index bcc5fd0..89cc426 100644
--- a/c/common.cpp
+++ b/c/common.cpp
@@ -1,3 +1,4 @@
+#include <stdexcept>
#include <memory>
#include <windows.h>
#include <SWI-Prolog.h>
@@ -30,23 +31,16 @@ TCHAR *TszFromSz(const char *sz, int iCp)
Library::Library(const TCHAR *tszLibrary)
{
- m_hModule = LoadLibrary(tszLibrary);
+ if (!(m_hModule = LoadLibrary(tszLibrary)))
+ throw std::invalid_argument("Library not found.");
}
Library::~Library()
{
- if (m_hModule)
- FreeLibrary(m_hModule);
+ FreeLibrary(m_hModule);
}
FARPROC Library::GetProcAddress(const char *szProc)
{
- return m_hModule? ::GetProcAddress(m_hModule, szProc): NULL;
-}
-
-std::unique_ptr<Library> Library::Load(const TCHAR *tszLibrary)
-{
- auto upLib = std::make_unique<Library>(tszLibrary);
- if (!upLib->m_hModule) upLib = nullptr;
- return upLib;
+ return ::GetProcAddress(m_hModule, szProc);
}
diff --git a/c/defs.h b/c/defs.h
index 766682e..ffebcb0 100644
--- a/c/defs.h
+++ b/c/defs.h
@@ -13,7 +13,6 @@ struct Library
Library(const TCHAR *);
~Library();
FARPROC GetProcAddress(const char *);
- static std::unique_ptr<Library> Load(const TCHAR *);
private:
HMODULE m_hModule;
};
@@ -144,4 +143,16 @@ inline int Dpi(int i)
return MulDiv(i, g_iDPI, 96);
}
+template <class T, typename ...A>
+std::unique_ptr<T> try_make_unique(A ...args)
+{
+ std::unique_ptr<T> up;
+ try {
+ up = std::make_unique<T>(args...);
+ } catch (...) {
+ up = nullptr;
+ }
+ return up;
+}
+
#endif
diff --git a/c/main.cpp b/c/main.cpp
index 6a43e2c..8bf07fb 100644
--- a/c/main.cpp
+++ b/c/main.cpp
@@ -35,14 +35,14 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR tszErr;
/* Set constant values. */
- if (auto upLib = Library::Load(TEXT("uxtheme.dll")))
+ if (auto upLib = try_make_unique<Library>(TEXT("uxtheme.dll")))
if (upLib->GetProcAddress("SetWindowTheme")) {
g_bThemes = 1;
}
g_cxVScroll = GetSystemMetrics(SM_CXVSCROLL);
/* Setup fonts. */
- if (auto upLib = Library::Load(TEXT("User32.dll"))) {
+ if (auto upLib = try_make_unique<Library>(TEXT("User32.dll"))) {
if (upLib->GetProcAddress("SystemParametersInfo" WA)) {
NONCLIENTMETRICS m;
m.cbSize = sizeof(NONCLIENTMETRICS);
@@ -110,7 +110,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
ShowWindow(hWnd, nCmdShow);
/* Populate episode list view. */
- Pl("track_episodes", "update_tracked_episodes");
+ Pl("track_episodes","update_tracked_episodes");
g_lpElv->Update();
g_lpElv->RestoreFocus();
@@ -140,7 +140,7 @@ static LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam)
g_hWnd = (HWND)wParam;
/* Get DPI. */
- if (auto upLib = Library::Load(TEXT("User32.dll")))
+ if (auto upLib = try_make_unique<Library>(TEXT("User32.dll")))
if (auto GetDpiForWindow = (UINT (*)(HWND))upLib->GetProcAddress("GetDpiForWindow"))
g_iDPI = GetDpiForWindow(g_hWnd);