From 595dfe169eb0ad0dbddf24cf3f94b954718b3cb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Thu, 21 Jul 2022 22:10:22 +0200 Subject: Simplify OnTerminate. No, not "awful", AWFUN! Speaking of AWFUN, here is an alternative implementation of it: #define AWFUN(t, f) cond_fun template std::enable_if_t, decltype(F)> cond_fun = F; template auto cond_fun = 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. --- c/common.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'c/common.h') diff --git a/c/common.h b/c/common.h index 0343914..4b42f45 100644 --- a/c/common.h +++ b/c/common.h @@ -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(s, L"" s) +template constexpr const T* cond_text(const char*, const wchar_t*); +template <> constexpr const char* cond_text(const char* sz, const wchar_t*) { return sz; } +template <> constexpr const wchar_t* cond_text(const char*, const wchar_t* wsz) { return wsz; } + +/* Conditionally select between ANSI and wide Windows API function. */ +#define AWFUN(t, f) cond_fun() +template +constexpr std::enable_if_t, decltype(F)> cond_fun() { return F; } +template +constexpr std::enable_if_t, decltype(G)> cond_fun() { return G; } + /* Return integer scaled for current DPI. */ inline int Dpi(const int i) { -- cgit v1.2.3