diff options
author | John Ankarström <john@ankarstrom.se> | 2022-07-22 23:52:22 +0200 |
---|---|---|
committer | John Ankarström <john@ankarstrom.se> | 2022-07-22 23:52:22 +0200 |
commit | 21e96c692595f204b91431a90123419e4a1780c4 (patch) | |
tree | fbd273dc24fa590626283dac09bc7671f0562d59 /c/common.h | |
parent | c1bbba366067f27881195f74cee5d063d1b270b0 (diff) | |
download | EpisodeBrowser-21e96c692595f204b91431a90123419e4a1780c4.tar.gz |
Cache GetSystemMetrics values with variable template.
The variable template could be generalized like this:
template <auto F, auto... A> const auto cache = F(A...);
and instantiated like:
cache<GetSystemMetrics, SM_CXVSCROLL>
It would still be limited to constant function arguments, which
usually isn't a problem for GetSystemMetrics, but might be for
other functions.
Diffstat (limited to 'c/common.h')
-rw-r--r-- | c/common.h | 14 |
1 files changed, 9 insertions, 5 deletions
@@ -54,7 +54,11 @@ std::optional<T> maybe_make(U... xs) } } -/* Call Windows API function, throwing error on NULL. */ +/* Variable template for caching values from GetSystemMetrics. */ +template <int I> +const auto Metric = GetSystemMetrics(I); + +/* Check result of Windows API call, throwing error on NULL. */ template <typename T> 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 <typename T> 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 <bool B, typename X, typename Y> constexpr std::enable_if_t<B == true, X> choose(X x, Y) { return x; } @@ -83,10 +87,10 @@ 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) +#define AWTEXT(t, s) choose<std::is_same_v<t, wchar_t>>(L"" s, 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) +#define AWFUN(t, f) choose<std::is_same_v<t, wchar_t>>(f##W, f##A) /* Return integer scaled for current DPI. */ inline int Dpi(const int i) |