From 21e96c692595f204b91431a90123419e4a1780c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Fri, 22 Jul 2022 23:52:22 +0200 Subject: Cache GetSystemMetrics values with variable template. The variable template could be generalized like this: template const auto cache = F(A...); and instantiated like: cache It would still be limited to constant function arguments, which usually isn't a problem for GetSystemMetrics, but might be for other functions. --- c/common.h | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'c/common.h') diff --git a/c/common.h b/c/common.h index 2c0be13..c6c7dbd 100644 --- a/c/common.h +++ b/c/common.h @@ -54,7 +54,11 @@ std::optional maybe_make(U... xs) } } -/* Call Windows API function, throwing error on NULL. */ +/* Variable template for caching values from GetSystemMetrics. */ +template +const auto Metric = GetSystemMetrics(I); + +/* Check result of Windows API call, throwing error on NULL. */ template inline T require(const T x) { @@ -62,7 +66,7 @@ inline T require(const T x) return x; } -/* Call Windows API function, showing a warning on NULL. */ +/* Check result of Windows API call, showing a warning on NULL. */ template inline T prefer(const T x) { @@ -75,7 +79,7 @@ inline T prefer(const T x) } /* Conditionally choose between two values. This template is necessary - * because the ternary conditional operator chooses only between + * 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; } @@ -83,10 +87,10 @@ 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>(s, L"" s) +#define AWTEXT(t, s) choose>(L"" s, s) /* Conditionally choose between ANSI and wide Windows API function. */ -#define AWFUN(t, f) choose>(f##A, f##W) +#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