From 3190fc267f6fb5e773250a45f494ad186ac20baa Mon Sep 17 00:00:00 2001 From: Birunthan Mohanathas Date: Fri, 28 Feb 2014 18:35:09 +0200 Subject: [PATCH] Use decltype instead of typedefs for function pointers --- Common/Platform.cpp | 3 +- Library/Litestep.cpp | 4 +-- Library/MeasureNet.cpp | 8 +++--- Library/MeasureNet.h | 7 ++--- Library/MeterWindow.cpp | 27 ++++++++--------- Library/MeterWindow.h | 13 +++------ Library/System.cpp | 23 +++++++-------- Plugins/PluginNowPlaying/PlayerCAD.cpp | 40 ++++++++++++-------------- Plugins/PluginNowPlaying/PlayerCAD.h | 3 -- 9 files changed, 55 insertions(+), 73 deletions(-) diff --git a/Common/Platform.cpp b/Common/Platform.cpp index 07e89ec8..9e933bc3 100644 --- a/Common/Platform.cpp +++ b/Common/Platform.cpp @@ -89,8 +89,7 @@ bool Is64BitWindows() return true; #endif - typedef BOOL(WINAPI * IsWow64ProcessFunc)(HANDLE hProcess, PBOOL Wow64Process); - auto isWow64Process = (IsWow64ProcessFunc)GetProcAddress(GetModuleHandle(L"kernel32"), "IsWow64Process"); + auto isWow64Process = (decltype(IsWow64Process)*)GetProcAddress(GetModuleHandle(L"kernel32"), "IsWow64Process"); if (isWow64Process) { BOOL isWow64 = FALSE; diff --git a/Library/Litestep.cpp b/Library/Litestep.cpp index f942492c..6e83c537 100644 --- a/Library/Litestep.cpp +++ b/Library/Litestep.cpp @@ -58,14 +58,12 @@ std::wstring GetFormattedString(UINT id, ...) HICON GetIcon(UINT id, bool large) { - typedef HRESULT (WINAPI * FPLOADICONMETRIC)(HINSTANCE hinst, PCWSTR pszName, int lims, HICON* phico); - HINSTANCE hExe = GetModuleHandle(nullptr); HINSTANCE hComctl = GetModuleHandle(L"Comctl32"); if (hComctl) { // Try LoadIconMetric for better quality with high DPI - FPLOADICONMETRIC loadIconMetric = (FPLOADICONMETRIC)GetProcAddress(hComctl, "LoadIconMetric"); + auto loadIconMetric = (decltype(LoadIconMetric)*)GetProcAddress(hComctl, "LoadIconMetric"); if (loadIconMetric) { HICON icon; diff --git a/Library/MeasureNet.cpp b/Library/MeasureNet.cpp index 2782fdbc..a6fad3ba 100644 --- a/Library/MeasureNet.cpp +++ b/Library/MeasureNet.cpp @@ -26,8 +26,8 @@ UINT MeasureNet::c_NumOfTables = 0; std::vector MeasureNet::c_StatValues; std::vector MeasureNet::c_OldStatValues; -FPGETIFTABLE2 MeasureNet::c_GetIfTable2 = nullptr; -FPFREEMIBTABLE MeasureNet::c_FreeMibTable = nullptr; +decltype(GetIfTable2)* MeasureNet::c_GetIfTable2 = nullptr; +decltype(FreeMibTable)* MeasureNet::c_FreeMibTable = nullptr; /* ** The constructor. This is the base class for the net-meters. @@ -694,8 +694,8 @@ void MeasureNet::InitializeStatic() HMODULE IpHlpApiLibrary = GetModuleHandle(L"IpHlpApi.dll"); if (IpHlpApiLibrary) { - c_GetIfTable2 = (FPGETIFTABLE2)GetProcAddress(IpHlpApiLibrary, "GetIfTable2"); - c_FreeMibTable = (FPFREEMIBTABLE)GetProcAddress(IpHlpApiLibrary, "FreeMibTable"); + c_GetIfTable2 = (decltype(c_GetIfTable2))GetProcAddress(IpHlpApiLibrary, "GetIfTable2"); + c_FreeMibTable = (decltype(c_FreeMibTable))GetProcAddress(IpHlpApiLibrary, "FreeMibTable"); } if (!c_GetIfTable2 || !c_FreeMibTable) diff --git a/Library/MeasureNet.h b/Library/MeasureNet.h index 32fb0a16..0c63bf21 100644 --- a/Library/MeasureNet.h +++ b/Library/MeasureNet.h @@ -24,9 +24,6 @@ #include #include "Measure.h" -typedef NETIO_STATUS (NETIOAPI_API_ * FPGETIFTABLE2)(PMIB_IF_TABLE2* Table); -typedef VOID (NETIOAPI_API_ * FPFREEMIBTABLE)(PVOID Memory); - class MeasureNet : public Measure { public: @@ -75,8 +72,8 @@ private: static BYTE* c_Table; static UINT c_NumOfTables; - static FPGETIFTABLE2 c_GetIfTable2; - static FPFREEMIBTABLE c_FreeMibTable; + static decltype(GetIfTable2)* c_GetIfTable2; + static decltype(FreeMibTable)* c_FreeMibTable; }; #endif diff --git a/Library/MeterWindow.cpp b/Library/MeterWindow.cpp index ef09d3cb..50d60145 100644 --- a/Library/MeterWindow.cpp +++ b/Library/MeterWindow.cpp @@ -64,10 +64,10 @@ enum INTERVAL int MeterWindow::c_InstanceCount = 0; HINSTANCE MeterWindow::c_DwmInstance = nullptr; -FPDWMENABLEBLURBEHINDWINDOW MeterWindow::c_DwmEnableBlurBehindWindow = nullptr; -FPDWMGETCOLORIZATIONCOLOR MeterWindow::c_DwmGetColorizationColor = nullptr; -FPDWMSETWINDOWATTRIBUTE MeterWindow::c_DwmSetWindowAttribute = nullptr; -FPDWMISCOMPOSITIONENABLED MeterWindow::c_DwmIsCompositionEnabled = nullptr; +decltype(DwmEnableBlurBehindWindow)* MeterWindow::c_DwmEnableBlurBehindWindow = nullptr; +decltype(DwmGetColorizationColor)* MeterWindow::c_DwmGetColorizationColor = nullptr; +decltype(DwmSetWindowAttribute)* MeterWindow::c_DwmSetWindowAttribute = nullptr; +decltype(DwmIsCompositionEnabled)* MeterWindow::c_DwmIsCompositionEnabled = nullptr; /* ** Constructor @@ -142,16 +142,17 @@ MeterWindow::MeterWindow(const std::wstring& folderPath, const std::wstring& fil m_FontCollection(), m_ToolTipHidden(false) { - if (!c_DwmInstance && IsWindowsVistaOrGreater()) + if (!c_DwmInstance && IsWindowsVistaOrGreater() && + (c_DwmInstance = System::RmLoadLibrary(L"dwmapi.dll")) != nullptr) { - c_DwmInstance = System::RmLoadLibrary(L"dwmapi.dll"); - if (c_DwmInstance) - { - c_DwmEnableBlurBehindWindow = (FPDWMENABLEBLURBEHINDWINDOW)GetProcAddress(c_DwmInstance, "DwmEnableBlurBehindWindow"); - c_DwmGetColorizationColor = (FPDWMGETCOLORIZATIONCOLOR)GetProcAddress(c_DwmInstance, "DwmGetColorizationColor"); - c_DwmSetWindowAttribute = (FPDWMSETWINDOWATTRIBUTE)GetProcAddress(c_DwmInstance, "DwmSetWindowAttribute"); - c_DwmIsCompositionEnabled = (FPDWMISCOMPOSITIONENABLED)GetProcAddress(c_DwmInstance, "DwmIsCompositionEnabled"); - } + c_DwmEnableBlurBehindWindow = + (decltype(c_DwmEnableBlurBehindWindow))GetProcAddress(c_DwmInstance, "DwmEnableBlurBehindWindow"); + c_DwmGetColorizationColor = + (decltype(c_DwmGetColorizationColor))GetProcAddress(c_DwmInstance, "DwmGetColorizationColor"); + c_DwmSetWindowAttribute = + (decltype(c_DwmSetWindowAttribute))GetProcAddress(c_DwmInstance, "DwmSetWindowAttribute"); + c_DwmIsCompositionEnabled = + (decltype(c_DwmIsCompositionEnabled))GetProcAddress(c_DwmInstance, "DwmIsCompositionEnabled"); } if (c_InstanceCount == 0) diff --git a/Library/MeterWindow.h b/Library/MeterWindow.h index 3b210f6e..99a7d6a2 100644 --- a/Library/MeterWindow.h +++ b/Library/MeterWindow.h @@ -40,11 +40,6 @@ #define RI_MOUSE_HORIZONTAL_WHEEL 0x0800 -typedef HRESULT (WINAPI * FPDWMENABLEBLURBEHINDWINDOW)(HWND hWnd, const DWM_BLURBEHIND* pBlurBehind); -typedef HRESULT (WINAPI * FPDWMGETCOLORIZATIONCOLOR)(DWORD* pcrColorization, BOOL* pfOpaqueBlend); -typedef HRESULT (WINAPI * FPDWMSETWINDOWATTRIBUTE)(HWND hwnd, DWORD dwAttribute, LPCVOID pvAttribute, DWORD cbAttribute); -typedef HRESULT (WINAPI * FPDWMISCOMPOSITIONENABLED)(BOOL* pfEnabled); - enum BUTTONPROC { BUTTONPROC_DOWN, @@ -433,10 +428,10 @@ private: static HINSTANCE c_DwmInstance; - static FPDWMENABLEBLURBEHINDWINDOW c_DwmEnableBlurBehindWindow; - static FPDWMGETCOLORIZATIONCOLOR c_DwmGetColorizationColor; - static FPDWMSETWINDOWATTRIBUTE c_DwmSetWindowAttribute; - static FPDWMISCOMPOSITIONENABLED c_DwmIsCompositionEnabled; + static decltype(DwmEnableBlurBehindWindow)* c_DwmEnableBlurBehindWindow; + static decltype(DwmGetColorizationColor)* c_DwmGetColorizationColor; + static decltype(DwmSetWindowAttribute)* c_DwmSetWindowAttribute; + static decltype(DwmIsCompositionEnabled)* c_DwmIsCompositionEnabled; }; #endif diff --git a/Library/System.cpp b/Library/System.cpp index 3ce98988..3b4341c4 100644 --- a/Library/System.cpp +++ b/Library/System.cpp @@ -1000,12 +1000,12 @@ LRESULT CALLBACK System::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lPa */ ULONGLONG System::GetTickCount64() { - typedef ULONGLONG (WINAPI * FPGETTICKCOUNT64)(); - static FPGETTICKCOUNT64 c_GetTickCount64 = (FPGETTICKCOUNT64)GetProcAddress(GetModuleHandle(L"kernel32"), "GetTickCount64"); + static auto s_GetTickCount64 = + (decltype(GetTickCount64)*)GetProcAddress(GetModuleHandle(L"kernel32"), "GetTickCount64"); - if (c_GetTickCount64) + if (s_GetTickCount64) { - return c_GetTickCount64(); + return s_GetTickCount64(); } else { @@ -1089,19 +1089,16 @@ void System::ResetWorkingDirectory() */ void System::InitializeCriticalSection(LPCRITICAL_SECTION lpCriticalSection) { - typedef BOOL (WINAPI * FPINITCRITEX)(LPCRITICAL_SECTION lpCriticalSection, DWORD dwSpinCount, DWORD Flags); - static FPINITCRITEX InitializeCriticalSectionEx = IsWindowsVistaOrGreater() ? - (FPINITCRITEX)GetProcAddress(GetModuleHandle(L"Kernel32"), "InitializeCriticalSectionEx") : nullptr; + static auto s_InitializeCriticalSectionEx = IsWindowsVistaOrGreater() ? + (decltype(InitializeCriticalSectionEx)*)GetProcAddress(GetModuleHandle(L"kernel32"), "InitializeCriticalSectionEx") : nullptr; - if (InitializeCriticalSectionEx) + if (s_InitializeCriticalSectionEx && + s_InitializeCriticalSectionEx(lpCriticalSection, 0, CRITICAL_SECTION_NO_DEBUG_INFO)) { - if (InitializeCriticalSectionEx(lpCriticalSection, 0, CRITICAL_SECTION_NO_DEBUG_INFO)) - { - return; - } + return; } - ::InitializeCriticalSectionAndSpinCount(lpCriticalSection, 0); + InitializeCriticalSectionAndSpinCount(lpCriticalSection, 0); } /* diff --git a/Plugins/PluginNowPlaying/PlayerCAD.cpp b/Plugins/PluginNowPlaying/PlayerCAD.cpp index 7489c3d2..7175ab9b 100644 --- a/Plugins/PluginNowPlaying/PlayerCAD.cpp +++ b/Plugins/PluginNowPlaying/PlayerCAD.cpp @@ -91,28 +91,26 @@ void PlayerCAD::Initialize() this); // Add WM_USER/WM_COPYDATA to allowed messages from lower level processes - HMODULE hUser32 = LoadLibrary(L"user32.dll"); - if (hUser32) - { - // Try ChangeWindowMessageFilterEx first (Win7+) - FPCHANGEWINDOWMESSAGEFILTEREX ChangeWindowMessageFilterEx = (FPCHANGEWINDOWMESSAGEFILTEREX)GetProcAddress(hUser32, "ChangeWindowMessageFilterEx"); - if (ChangeWindowMessageFilterEx) - { - ChangeWindowMessageFilterEx(m_Window, WM_USER, MSGFLT_ALLOW, nullptr); - ChangeWindowMessageFilterEx(m_Window, WM_COPYDATA, MSGFLT_ALLOW, nullptr); - } - else - { - // Try ChangeWindowMessageFilter (Vista) - FPCHANGEWINDOWMESSAGEFILTER ChangeWindowMessageFilter = (FPCHANGEWINDOWMESSAGEFILTER)GetProcAddress(hUser32, "ChangeWindowMessageFilter"); - if (ChangeWindowMessageFilter) - { - ChangeWindowMessageFilter(WM_USER, MSGFLT_ALLOW); - ChangeWindowMessageFilter(WM_COPYDATA, MSGFLT_ALLOW); - } - } + const HMODULE hUser32 = GetModuleHandle(L"user32"); - FreeLibrary(hUser32); + // Try ChangeWindowMessageFilterEx first (Win7+) + auto changeWindowMessageFilterEx = + (decltype(ChangeWindowMessageFilterEx)*)GetProcAddress(hUser32, "ChangeWindowMessageFilterEx"); + if (changeWindowMessageFilterEx) + { + changeWindowMessageFilterEx(m_Window, WM_USER, MSGFLT_ALLOW, nullptr); + changeWindowMessageFilterEx(m_Window, WM_COPYDATA, MSGFLT_ALLOW, nullptr); + } + else + { + // Try ChangeWindowMessageFilter (Vista) + auto changeWindowMessageFilter = + (decltype(ChangeWindowMessageFilter)*)GetProcAddress(hUser32, "ChangeWindowMessageFilter"); + if (changeWindowMessageFilter) + { + changeWindowMessageFilter(WM_USER, MSGFLT_ALLOW); + changeWindowMessageFilter(WM_COPYDATA, MSGFLT_ALLOW); + } } WCHAR buffer[MAX_PATH]; diff --git a/Plugins/PluginNowPlaying/PlayerCAD.h b/Plugins/PluginNowPlaying/PlayerCAD.h index 3b4e4ef0..aba56197 100644 --- a/Plugins/PluginNowPlaying/PlayerCAD.h +++ b/Plugins/PluginNowPlaying/PlayerCAD.h @@ -21,9 +21,6 @@ #include "Player.h" -typedef BOOL (WINAPI * FPCHANGEWINDOWMESSAGEFILTER)(UINT message, DWORD dwFlag); -typedef BOOL (WINAPI * FPCHANGEWINDOWMESSAGEFILTEREX)(HWND hWnd, UINT message, DWORD dwFlag, PCHANGEFILTERSTRUCT pChangeFilterStruct); - class PlayerCAD : public Player { public: