From df42108d1f4c1628fe62874e48e87282fab6bc65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Wed, 20 Jul 2022 02:12:32 +0200 Subject: Fix bug in and rename throw_nil, warn_nil. In warn_nil, the return value was undefined on exception -- I think. While informative, the names throw_nil and warn_nil don't work very well in conditionals: if (warn_nil(...)) g(); sounds like g should be called if f returns nil and a warning is issued. But it is actually the other way around; g is called if f is successful. if (prefer(...)) g(); sounds less like that. --- c/common.cpp | 4 ++-- c/common.h | 9 ++++----- c/episodelistview.cpp | 4 ++-- c/listview.cpp | 4 ++-- c/main.cpp | 38 +++++++++++++++++++------------------- 5 files changed, 29 insertions(+), 30 deletions(-) diff --git a/c/common.cpp b/c/common.cpp index b85be49..7981801 100644 --- a/c/common.cpp +++ b/c/common.cpp @@ -87,8 +87,8 @@ static LRESULT CALLBACK CBTProc(const int nCode, const WPARAM wParam, const LPAR int EBMessageBox(const TCHAR* const tszText, const TCHAR* const tszCaption, const unsigned uType) { extern HWND g_hWnd; - HHOOK hHook = throw_nil(WH_CBT, CBTProc, (HINSTANCE)NULL, GetCurrentThreadId()); + HHOOK hHook = require(WH_CBT, CBTProc, (HINSTANCE)NULL, GetCurrentThreadId()); int r = MessageBox(g_hWnd, tszText, tszCaption, uType); - throw_nil(hHook); + require(hHook); return r; } diff --git a/c/common.h b/c/common.h index a4f73e5..6b29ed5 100644 --- a/c/common.h +++ b/c/common.h @@ -55,7 +55,7 @@ std::optional maybe_make(U... xs) /* Call Windows API function, throwing error on NULL. */ template -inline auto throw_nil(T... xs) +inline auto require(T... xs) { auto r = F(xs...); if (!r) throw Win32Error(GetLastError()); @@ -64,15 +64,14 @@ inline auto throw_nil(T... xs) /* Call Windows API function, showing a warning on NULL. */ template -inline auto warn_nil(T... xs) +inline auto prefer(T... xs) { - decltype(F(std::declval()...)) r; try { - r = throw_nil(xs...); + return require(xs...); } catch (Win32Error& e) { EBMessageBox(e.twhat(), TEXT("System Error"), MB_ICONWARNING); + return (decltype(F(std::declval()...)))NULL; } - return r; } /* Return integer scaled for current DPI. */ diff --git a/c/episodelistview.cpp b/c/episodelistview.cpp index e70cc06..6ed9146 100644 --- a/c/episodelistview.cpp +++ b/c/episodelistview.cpp @@ -90,7 +90,7 @@ LRESULT EpisodeListView::HandleNotify(const LPARAM lParam) { extern HFONT g_hfBold; if (!Pl("track_episodes","watched",pLvCd->nmcd.lItemlParam)) { - throw_nil(pLvCd->nmcd.hdc, g_hfBold); + require(pLvCd->nmcd.hdc, g_hfBold); return CDRF_NEWFONT; } break; @@ -125,7 +125,7 @@ LRESULT EpisodeListView::HandleNotify(const LPARAM lParam) { extern HMENU g_hPopupMenu; const DWORD dwPos = GetMessagePos(); - throw_nil(g_hPopupMenu, TPM_RIGHTBUTTON, + require(g_hPopupMenu, TPM_RIGHTBUTTON, LOWORD(dwPos), HIWORD(dwPos), 0, m_hWndParent, (const RECT*)NULL); break; diff --git a/c/listview.cpp b/c/listview.cpp index fa99793..056a17b 100644 --- a/c/listview.cpp +++ b/c/listview.cpp @@ -13,7 +13,7 @@ ListView::ListView(const HWND hWndParent, const HMENU hMenu, const DWORD dwStyle { m_hWndParent = hWndParent; m_bHeader = !(dwStyle & LVS_NOCOLUMNHEADER); - hWnd = throw_nil( + hWnd = require( WS_EX_CLIENTEDGE, WC_LISTVIEW, TEXT(""), @@ -21,7 +21,7 @@ ListView::ListView(const HWND hWndParent, const HMENU hMenu, const DWORD dwStyle 0, 0, 0, 0, m_hWndParent, hMenu, GetModuleHandle(NULL), this); - if (throw_nil(hWnd, TEXT("this"), (HANDLE)this)) + if (require(hWnd, TEXT("this"), (HANDLE)this)) m_prevProc = (WNDPROC)SetWindowLongPtr(hWnd, GWLP_WNDPROC, (LONG_PTR)::WndProc); diff --git a/c/main.cpp b/c/main.cpp index f9bdced..1a50c6f 100644 --- a/c/main.cpp +++ b/c/main.cpp @@ -89,10 +89,10 @@ int WINAPI WinMain(const HINSTANCE hInstance, const HINSTANCE, char* const, cons INITCOMMONCONTROLSEX icc; icc.dwSize = sizeof(icc); icc.dwICC = ICC_WIN95_CLASSES; - throw_nil(&icc); + require(&icc); - g_hPopupMenu = throw_nil((HINSTANCE)NULL, MAKEINTRESOURCE(IDR_POPUPMENU)); - g_hPopupMenu = throw_nil(g_hPopupMenu, 0); + g_hPopupMenu = require((HINSTANCE)NULL, MAKEINTRESOURCE(IDR_POPUPMENU)); + g_hPopupMenu = require(g_hPopupMenu, 0); WNDCLASSEX wc; memset(&wc, 0, sizeof(WNDCLASSEX)); @@ -105,23 +105,23 @@ int WINAPI WinMain(const HINSTANCE hInstance, const HINSTANCE, char* const, cons wc.lpszMenuName = MAKEINTRESOURCE(IDR_MENU); wc.lpszClassName = TEXT("Episode Browser"); wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); - throw_nil(&wc); + require(&wc); /* Create window. A CBT hook is used to initialize important * global variables before any messages are sent to the new * window. It is vital that the hook is set up correctly. */ - const HHOOK hHook = throw_nil(WH_CBT, CBTProc, + const HHOOK hHook = require(WH_CBT, CBTProc, (HINSTANCE)NULL, GetCurrentThreadId()); - const HWND hWnd = throw_nil( + const HWND hWnd = require( 0, TEXT("Episode Browser"), TEXT("Episode Browser"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, (HWND)NULL, (HMENU)NULL, hInstance, (void*)NULL); - throw_nil(hHook); + require(hHook); - g_hWndStatus = throw_nil( + g_hWndStatus = require( 0, STATUSCLASSNAME, (const TCHAR*)NULL, @@ -171,17 +171,17 @@ static LRESULT CALLBACK CBTProc(const int nCode, const WPARAM wParam, const LPAR if (opLib->GetProcAddress("SystemParametersInfo" WA)) { NONCLIENTMETRICS m; m.cbSize = sizeof(NONCLIENTMETRICS); - throw_nil(SPI_GETNONCLIENTMETRICS, + require(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &m, 0); - g_hfNormal = throw_nil(&m.lfMessageFont); + g_hfNormal = require(&m.lfMessageFont); } } else - g_hfNormal = static_cast(throw_nil(DEFAULT_GUI_FONT)); + g_hfNormal = static_cast(require(DEFAULT_GUI_FONT)); LOGFONT lf; - throw_nil(g_hfNormal, sizeof(LOGFONT), &lf); + require(g_hfNormal, sizeof(LOGFONT), &lf); lf.lfWeight = FW_BOLD; - g_hfBold = throw_nil(&lf); + g_hfBold = require(&lf); /* Create child windows. */ g_pDlv = new DataListView(g_hWnd); @@ -237,7 +237,7 @@ LRESULT CALLBACK WndProc(const HWND hWnd, const UINT uMsg, const WPARAM wParam, { const RECT* const lpr = (RECT*)lParam; g_iDPI = HIWORD(wParam); - warn_nil(hWnd, (HWND)NULL, + prefer(hWnd, (HWND)NULL, lpr->left, lpr->top, lpr->right-lpr->left, lpr->bottom-lpr->top, @@ -280,7 +280,7 @@ LRESULT CALLBACK WndProc(const HWND hWnd, const UINT uMsg, const WPARAM wParam, TEXT(""))); } else { i = 0; - warn_nil(hWnd, IDT_TIMER); + prefer(hWnd, IDT_TIMER); g_pElv->Update(); } break; @@ -302,8 +302,8 @@ LRESULT CALLBACK WndProc(const HWND hWnd, const UINT uMsg, const WPARAM wParam, case IDM_FILE_FETCH_SCREENWRITERS: if (g_bThread) break; Pl("episode_data","thread_create","update_screenwriters",&g_aThread); - t: warn_nil(hWnd, IDT_TIMER); - if (!SetTimer(hWnd, IDT_TIMER, 500, (TIMERPROC)NULL)) break; + t: KillTimer(hWnd, IDT_TIMER); + if (!prefer(hWnd, IDT_TIMER, 500, (TIMERPROC)NULL)) break; SendMessage(g_hWndStatus, SB_SETTEXT, MAKEWPARAM(1,0), (LPARAM)TEXT(".")); g_bThread = 1; break; @@ -514,14 +514,14 @@ void UpdateLayout() /* Resize data list view. */ cyDlv = rc.bottom-yStatus-g_pDlv->Height(); - throw_nil(g_pDlv->hWnd, 0, cyDlv, rc.right, rc.bottom-yStatus-cyDlv, TRUE); + require(g_pDlv->hWnd, 0, cyDlv, rc.right, rc.bottom-yStatus-cyDlv, TRUE); ListView_SetColumnWidth(g_pDlv->hWnd, DLVSIKEY, LVSCW_AUTOSIZE); cxColumn = ListView_GetColumnWidth(g_pDlv->hWnd, 0)+4; ListView_SetColumnWidth(g_pDlv->hWnd, DLVSIKEY, cxColumn); ListView_SetColumnWidth(g_pDlv->hWnd, DLVSIVALUE, rc.right-cxColumn-cxVScroll-4); /* Resize episode list view. */ - throw_nil(g_pElv->hWnd, 0, 0, rc.right, cyDlv+1, TRUE); + require(g_pElv->hWnd, 0, 0, rc.right, cyDlv+1, TRUE); ListView_SetColumnWidth(g_pElv->hWnd, ELVSIEPISODE, LVSCW_AUTOSIZE); cxColumn = ListView_GetColumnWidth(g_pElv->hWnd, ELVSIEPISODE)+4; ListView_SetColumnWidth(g_pElv->hWnd, ELVSIEPISODE, cxColumn); -- cgit v1.2.3