From dccea47e9bc322d654902a1db4fc52cbf6dd0cd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Tue, 26 Jul 2022 18:51:15 +0200 Subject: Improve Win32Error, library handling code. --- c/common.h | 44 ++++++++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 16 deletions(-) (limited to 'c/common.h') diff --git a/c/common.h b/c/common.h index 709a258..3f27ba4 100644 --- a/c/common.h +++ b/c/common.h @@ -14,19 +14,45 @@ template std::basic_string BstrFromSz(const char* sz, int iCp = CP_UTF8); int EBMessageBox(const TCHAR* tszText, const TCHAR* tszCaption, unsigned uType); +/* Conditionally choose between two values. */ +template +constexpr std::enable_if_t choose(X x, Y) { return x; } +template +constexpr std::enable_if_t choose(X, Y y) { return y; } + +/* Conditionally choose between ANSI and wide string literal. */ +#define AWTEXT(t, s) choose>(L"" s, s) + +/* Conditionally choose between ANSI and wide Windows API function. */ +#define AWFUN(t, f) choose>(f##W, f##A) + struct Win32Error : public std::exception { Win32Error(); Win32Error(DWORD dwErr); ~Win32Error(); template const T* what() const noexcept; - + DWORD dwErr; private: - DWORD m_dwErr; char* m_szMsg = NULL; wchar_t* m_wszMsg = NULL; }; +template +const T* Win32Error::what() const noexcept +{ + TCHAR* tszMsg = choose>(m_wszMsg, m_szMsg); + if (!tszMsg) + AWFUN(T, FormatMessage)( + FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + dwErr, + MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), + (TCHAR*)&tszMsg, + 0, NULL); + return tszMsg; +} + struct Library { Library(const TCHAR* tszLibrary); @@ -79,20 +105,6 @@ inline T prefer(const T x) return x; } -/* Conditionally choose between two values. This template is necessary - * because the ternary conditional operator can choose only between - * values of the same type. */ -template -constexpr std::enable_if_t choose(X x, Y) { return x; } -template -constexpr std::enable_if_t choose(X, Y y) { return y; } - -/* Conditionally choose between ANSI and wide string literal. */ -#define AWTEXT(t, s) choose>(L"" s, s) - -/* Conditionally choose between ANSI and wide Windows API function. */ -#define AWFUN(t, f) choose>(f##W, f##A) - /* Return integer scaled for current DPI. */ inline int Dpi(const int i) { -- cgit v1.2.3