diff options
author | John Ankarström <john@ankarstrom.se> | 2022-07-21 22:10:22 +0200 |
---|---|---|
committer | John Ankarström <john@ankarstrom.se> | 2022-07-21 22:10:22 +0200 |
commit | 595dfe169eb0ad0dbddf24cf3f94b954718b3cb5 (patch) | |
tree | 3ab65b55d3eb9c07437fa7361273364dd5d9fcf7 | |
parent | 4e03ae2fef20449de094baf52c5a8acf27a77adb (diff) | |
download | EpisodeBrowser-595dfe169eb0ad0dbddf24cf3f94b954718b3cb5.tar.gz |
Simplify OnTerminate.
No, not "awful", AWFUN!
Speaking of AWFUN, here is an alternative implementation of it:
#define AWFUN(t, f) cond_fun<t, f##A, f##W>
template <typename T, auto F, auto G>
std::enable_if_t<std::is_same_v<T, char>, decltype(F)> cond_fun = F;
template <auto F, auto G>
auto cond_fun<wchar_t, F, G> = G;
This implementation uses a variable template instead of a function
template, but I decided against it, as (at least I think) it would
instantiate useless variables that merely point to pre-existing API
functions. Like,
auto cond_fun__wchar_t__blablabla = MessageBoxW;
auto cond_fun__char__blablabla = MessageBoxA;
which is quite useless. Better to just have a constexpr function,
which the compiler may inline, return the real function pointer.
-rw-r--r-- | c/common.h | 13 | ||||
-rw-r--r-- | c/main.cpp | 36 |
2 files changed, 34 insertions, 15 deletions
@@ -74,6 +74,19 @@ inline T prefer(const T x) return x; } +/* Conditionally select between ANSI and wide string literal. */ +#define AWTEXT(t, s) cond_text<t>(s, L"" s) +template <typename T> constexpr const T* cond_text(const char*, const wchar_t*); +template <> constexpr const char* cond_text<char>(const char* sz, const wchar_t*) { return sz; } +template <> constexpr const wchar_t* cond_text<wchar_t>(const char*, const wchar_t* wsz) { return wsz; } + +/* Conditionally select between ANSI and wide Windows API function. */ +#define AWFUN(t, f) cond_fun<t, f##A, f##W>() +template <typename T, auto F, auto G> +constexpr std::enable_if_t<std::is_same_v<T, char>, decltype(F)> cond_fun() { return F; } +template <typename T, auto F, auto G> +constexpr std::enable_if_t<std::is_same_v<T, wchar_t>, decltype(G)> cond_fun() { return G; } + /* Return integer scaled for current DPI. */ inline int Dpi(const int i) { @@ -1,4 +1,5 @@ #include <exception> +#include <sstream> #include <windows.h> #include <commctrl.h> #include <uxtheme.h> @@ -46,30 +47,35 @@ static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); static INT_PTR CALLBACK AboutDlgProc(HWND, UINT, WPARAM, LPARAM); static void UpdateTheme(); +template <typename T> +void TerminateMsg(const T* sz1, const T* sz2) noexcept +{ + std::basic_ostringstream<T> ss; + ss << AWTEXT(T, "Episode Browser was terminated due to "); + ss << sz1; + if (sz2) + ss << ": " << sz2; + else + ss << "."; + AWFUN(T, MessageBox)((HWND)NULL, ss.str().c_str(), AWTEXT(T, "Fatal Error"), MB_ICONERROR); +} + void OnTerminate() noexcept { try { std::rethrow_exception(std::current_exception()); } catch (const term_t& t) { - std::basic_string<TCHAR> tstr = TEXT("Episode Browser was terminated due to a Prolog exception: "); TCHAR* tsz; - if (PL_get_tchars(t, &tsz, CVT_WRITE)) { - tstr += tsz; - MessageBox(NULL, tstr.c_str(), TEXT("Fatal Error"), MB_ICONERROR); - } else - MessageBoxA(NULL, "Episode Browser was terminated due to a Prolog exception.", - "Fatal Error", MB_ICONERROR); + if (PL_get_tchars(t, &tsz, CVT_WRITE)) + TerminateMsg<TCHAR>(TEXT("a Prolog exception"), tsz); + else + TerminateMsg<char>("a Prolog exception", NULL); } catch (Win32Error& e) { - std::basic_string<TCHAR> tstr = TEXT("Episode Browser was terminated due to a Windows error: "); - tstr += e.what<TCHAR>(); - MessageBox(NULL, tstr.c_str(), TEXT("Fatal Error"), MB_ICONERROR); + TerminateMsg<TCHAR>(TEXT("a Windows error"), e.what<TCHAR>()); } catch (std::exception& e) { - std::string str = "Episode Browser was terminated due to an exception: "; - str += e.what(); - MessageBoxA(NULL, str.c_str(), "Fatal Error", MB_ICONERROR); + TerminateMsg<char>("an exception", e.what()); } catch (...) { - MessageBoxA(NULL, "Episode Browser was terminated due to an exception.", - "Fatal Error", MB_ICONERROR); + TerminateMsg<char>("an exception", NULL); } _Exit(1); } |