diff options
-rw-r--r-- | c/common.h | 25 |
1 files changed, 13 insertions, 12 deletions
@@ -74,18 +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; } +/* Conditionally choose between two values. This template is necessary + * because the ternary conditional operator chooses only between + * values of the same type. */ +template <bool B, typename X, typename Y> +constexpr std::enable_if_t<B == true, X> choose(X x, Y) { return x; } +template <bool B, typename X, typename Y> +constexpr std::enable_if_t<B == false, Y> choose(X, Y y) { return y; } + +/* Conditionally choose between ANSI and wide string literal. */ +#define AWTEXT(t, s) choose<std::is_same_v<t, char>>(s, L"" s) + +/* Conditionally choose between ANSI and wide Windows API function. */ +#define AWFUN(t, f) choose<std::is_same_v<t, char>>(f##A, f##W) /* Return integer scaled for current DPI. */ inline int Dpi(const int i) |