From c883d9cf5673fe0af8d69120b048d642e122bdbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Fri, 29 Jul 2022 15:31:26 +0200 Subject: Use swprintf_s instead of string streams. I find it much simpler. It is very safe, as wszf only accepts fixed-size arrays. There is, of course, the chance that swprintf_s fails and writes nothing into the array. This can be handled by the caller, if desired. --- c/common.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'c/common.h') diff --git a/c/common.h b/c/common.h index 3a2d598..8b88635 100644 --- a/c/common.h +++ b/c/common.h @@ -42,6 +42,12 @@ T* Library::GetProcAddress(const char* const szProc) return (T*)(void*)::GetProcAddress(m_hModule, szProc); } +template +inline auto wszf(wchar_t (&wsz)[N], const wchar_t* wszFmt, T... xs) +{ + return swprintf_s(wsz, N, wszFmt, xs...); +} + /* Create and return an object of type C. If construction fails, * return nothing. The returned value must be checked before being * used, as dereferencing is undefined if the value is empty. */ @@ -57,7 +63,7 @@ std::optional maybe_make(U... xs) /* Variable template for caching values from GetSystemMetrics. */ template -const auto Metric = GetSystemMetrics(I); +auto Metric = GetSystemMetrics(I); /* Check result of Windows API call, throwing error on NULL. */ template -- cgit v1.2.3