From b20b6da937760b2d2b31751247a6c355ddf6dcbf Mon Sep 17 00:00:00 2001 From: frosch Date: Sun, 4 May 2025 19:05:43 +0200 Subject: [PATCH] Codechange: The compiler ensures static variables are initialised only once, no need to track that manually. --- src/console_cmds.cpp | 10 +++++----- src/os/unix/unix.cpp | 39 +++++++++++++++++++-------------------- src/os/windows/win32.cpp | 24 ++++++++---------------- 3 files changed, 32 insertions(+), 41 deletions(-) diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp index 0d6d4b1a94..b18cbd1983 100644 --- a/src/console_cmds.cpp +++ b/src/console_cmds.cpp @@ -2225,11 +2225,11 @@ static void OutputContentState(const ContentInfo &ci) static bool ConContent(std::span argv) { - static ContentCallback *cb = nullptr; - if (cb == nullptr) { - cb = new ConsoleContentCallback(); - _network_content_client.AddCallback(cb); - } + [[maybe_unused]] static ContentCallback *const cb = []() { + auto res = new ConsoleContentCallback(); + _network_content_client.AddCallback(res); + return res; + }(); if (argv.size() <= 1) { IConsolePrint(CC_HELP, "Query, select and download content. Usage: 'content update|upgrade|select [id]|unselect [all|id]|state [filter]|download'."); diff --git a/src/os/unix/unix.cpp b/src/os/unix/unix.cpp index 52bf064816..36ee069637 100644 --- a/src/os/unix/unix.cpp +++ b/src/os/unix/unix.cpp @@ -144,6 +144,19 @@ static std::string convert_tofrom_fs(iconv_t convd, std::string_view name) return buf; } +/** + * Open iconv converter. + */ +static std::optional OpenIconv(std::string from, std::string to) +{ + iconv_t convd = iconv_open(from.c_str(), to.c_str()); + if (convd == reinterpret_cast(-1)) { + Debug(misc, 0, "[iconv] conversion from codeset '{}' to '{}' unsupported", from, to); + return std::nullopt; + } + return convd; +} + /** * Convert from OpenTTD's encoding to that of the local environment * @param name pointer to a valid string that will be converted @@ -151,17 +164,10 @@ static std::string convert_tofrom_fs(iconv_t convd, std::string_view name) */ std::string OTTD2FS(std::string_view name) { - static iconv_t convd = (iconv_t)(-1); - if (convd == (iconv_t)(-1)) { - std::string env{GetLocalCode()}; - convd = iconv_open(env.c_str(), INTERNALCODE); - if (convd == (iconv_t)(-1)) { - Debug(misc, 0, "[iconv] conversion from codeset '{}' to '{}' unsupported", INTERNALCODE, env); - return std::string{name}; - } - } + static const auto convd = OpenIconv(GetLocalCode(), INTERNALCODE); + if (!convd.has_value()) return std::string{name}; - return convert_tofrom_fs(convd, name); + return convert_tofrom_fs(*convd, name); } /** @@ -171,17 +177,10 @@ std::string OTTD2FS(std::string_view name) */ std::string FS2OTTD(std::string_view name) { - static iconv_t convd = (iconv_t)(-1); - if (convd == (iconv_t)(-1)) { - std::string env{GetLocalCode()}; - convd = iconv_open(INTERNALCODE, env.c_str()); - if (convd == (iconv_t)(-1)) { - Debug(misc, 0, "[iconv] conversion from codeset '{}' to '{}' unsupported", env, INTERNALCODE); - return std::string{name}; - } - } + static const auto convd = OpenIconv(INTERNALCODE, GetLocalCode()); + if (!convd.has_value()) return std::string{name}; - return convert_tofrom_fs(convd, name); + return convert_tofrom_fs(*convd, name); } #endif /* WITH_ICONV */ diff --git a/src/os/windows/win32.cpp b/src/os/windows/win32.cpp index c7405a3707..107436a1d8 100644 --- a/src/os/windows/win32.cpp +++ b/src/os/windows/win32.cpp @@ -430,11 +430,16 @@ void Win32SetCurrentLocaleName(std::string iso_code) MultiByteToWideChar(CP_UTF8, 0, iso_code.data(), static_cast(iso_code.size()), _cur_iso_locale, static_cast(std::size(_cur_iso_locale))); } +static LibraryLoader::Function GetKernel32Function(const std::string &symbol_name) +{ + static LibraryLoader _kernel32("Kernel32.dll"); + return _kernel32.GetFunction(symbol_name); +} + int OTTDStringCompare(std::string_view s1, std::string_view s2) { typedef int (WINAPI *PFNCOMPARESTRINGEX)(LPCWSTR, DWORD, LPCWCH, int, LPCWCH, int, LPVOID, LPVOID, LPARAM); - static PFNCOMPARESTRINGEX _CompareStringEx = nullptr; - static bool first_time = true; + static const PFNCOMPARESTRINGEX _CompareStringEx = GetKernel32Function("CompareStringEx"); #ifndef SORT_DIGITSASNUMBERS # define SORT_DIGITSASNUMBERS 0x00000008 // use digits as numbers sort method @@ -443,12 +448,6 @@ int OTTDStringCompare(std::string_view s1, std::string_view s2) # define LINGUISTIC_IGNORECASE 0x00000010 // linguistically appropriate 'ignore case' #endif - if (first_time) { - static LibraryLoader _kernel32("Kernel32.dll"); - _CompareStringEx = _kernel32.GetFunction("CompareStringEx"); - first_time = false; - } - int len_s1 = MultiByteToWideChar(CP_UTF8, 0, s1.data(), (int)s1.size(), nullptr, 0); int len_s2 = MultiByteToWideChar(CP_UTF8, 0, s2.data(), (int)s2.size(), nullptr, 0); @@ -478,14 +477,7 @@ int OTTDStringCompare(std::string_view s1, std::string_view s2) int Win32StringContains(std::string_view str, std::string_view value, bool case_insensitive) { typedef int (WINAPI *PFNFINDNLSSTRINGEX)(LPCWSTR, DWORD, LPCWSTR, int, LPCWSTR, int, LPINT, LPNLSVERSIONINFO, LPVOID, LPARAM); - static PFNFINDNLSSTRINGEX _FindNLSStringEx = nullptr; - static bool first_time = true; - - if (first_time) { - static LibraryLoader _kernel32("Kernel32.dll"); - _FindNLSStringEx = _kernel32.GetFunction("FindNLSStringEx"); - first_time = false; - } + static const PFNFINDNLSSTRINGEX _FindNLSStringEx = GetKernel32Function("FindNLSStringEx"); if (_FindNLSStringEx != nullptr) { int len_str = MultiByteToWideChar(CP_UTF8, 0, str.data(), (int)str.size(), nullptr, 0);