aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2022-07-18 03:25:00 +0200
committerJohn Ankarström <john@ankarstrom.se>2022-07-18 03:25:00 +0200
commit53abfac6acd04dff80ec6c8cfb0dd53f2f74dadb (patch)
treee0e6ef0926e3fb77a96874fa39d7d71fcceae942
parent03d9db8d5ff22faa27ab25264727bb09ba87d66d (diff)
downloadEpisodeBrowser-53abfac6acd04dff80ec6c8cfb0dd53f2f74dadb.tar.gz
Improve Library::GetProcAddress.
It isn't really more safe, but it removes the need for a confusing function pointer cast, which is easy to get wrong. As far as the compiler is concerned, the result is literally the same, but it does force the caller to (indirectly, via the template parameter) cast the return value, which may be a good thing.
-rw-r--r--c/common.cpp5
-rw-r--r--c/common.h30
-rw-r--r--c/main.cpp6
3 files changed, 21 insertions, 20 deletions
diff --git a/c/common.cpp b/c/common.cpp
index c04d986..d503133 100644
--- a/c/common.cpp
+++ b/c/common.cpp
@@ -57,8 +57,3 @@ Library::~Library()
{
FreeLibrary(m_hModule);
}
-
-void* Library::GetProcAddress(const char* const szProc)
-{
- return (void*)::GetProcAddress(m_hModule, szProc);
-}
diff --git a/c/common.h b/c/common.h
index 169b748..a4f1fd4 100644
--- a/c/common.h
+++ b/c/common.h
@@ -27,23 +27,15 @@ struct Library
{
Library(const TCHAR* tszLibrary);
~Library();
- void* GetProcAddress(const char* szProc);
+ template <typename T> T* GetProcAddress(const char* szProc);
private:
HMODULE m_hModule;
};
-inline int Cmp(const int a, const int b)
+template <typename T>
+T* Library::GetProcAddress(const char* const szProc)
{
- if (a == b) return 0;
- if (a > b) return 1;
- return -1;
-}
-
-/* Return integer scaled for current DPI. */
-inline int Dpi(const int i)
-{
- extern int g_iDPI;
- return MulDiv(i, g_iDPI, 96);
+ return (T*)(void*)::GetProcAddress(m_hModule, szProc);
}
/* Create and return an object of type C. If construction fails,
@@ -59,4 +51,18 @@ std::optional<C> try_make(T ...args)
}
}
+/* Return integer scaled for current DPI. */
+inline int Dpi(const int i)
+{
+ extern int g_iDPI;
+ return MulDiv(i, g_iDPI, 96);
+}
+
+inline int Cmp(const int a, const int b)
+{
+ if (a == b) return 0;
+ if (a > b) return 1;
+ return -1;
+}
+
#endif
diff --git a/c/main.cpp b/c/main.cpp
index 2e2fbbf..08e6b96 100644
--- a/c/main.cpp
+++ b/c/main.cpp
@@ -156,16 +156,16 @@ static LRESULT CALLBACK CBTProc(const int nCode, const WPARAM wParam, const LPAR
/* Look up constants. */
if (auto opLib = try_make<Library>(TEXT("User32.dll")))
- if (auto GetDpiForWindow = (UINT (*)(HWND))opLib->GetProcAddress("GetDpiForWindow"))
+ if (auto GetDpiForWindow = opLib->GetProcAddress<UINT(HWND)>("GetDpiForWindow"))
g_iDPI = GetDpiForWindow(g_hWnd);
if (auto opLib = try_make<Library>(TEXT("uxtheme.dll")))
- if (opLib->GetProcAddress("SetWindowTheme"))
+ if (opLib->GetProcAddress<void>("SetWindowTheme"))
g_bThemes = 1;
LOGFONT lf;
if (auto opLib = try_make<Library>(TEXT("User32.dll"))) {
- if (opLib->GetProcAddress("SystemParametersInfo" WA)) {
+ if (opLib->GetProcAddress<void>("SystemParametersInfo" WA)) {
NONCLIENTMETRICS m;
m.cbSize = sizeof(NONCLIENTMETRICS);
SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &m, 0);