From bce104d97b9cf5385487291a1197dc0eb9c5aa30 Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Thu, 14 Dec 2023 16:16:21 +0100 Subject: [PATCH] locale: redesign locale to behave like locale_t on demand This avoids painful `.get()` in every `stdex::sprintf()` call. We could use C++ polymorphism to add other sprintf variants, but the argument combinations would explode. Signed-off-by: Simon Rozman --- include/stdex/locale.hpp | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/include/stdex/locale.hpp b/include/stdex/locale.hpp index ada2b92b4..7b1da7909 100644 --- a/include/stdex/locale.hpp +++ b/include/stdex/locale.hpp @@ -52,16 +52,38 @@ namespace stdex } }; - /// - /// locale_t helper class to free_locale when going out of scope - /// + /// \cond internal #if defined(_WIN32) - using locale = std::unique_ptr<__crt_locale_pointers, free_locale_delete>; + using _locale_t_ref = __crt_locale_pointers; #elif defined(__APPLE__) - using locale = std::unique_ptr; + using _locale_t_ref = struct _xlocale; #else - using locale = std::unique_ptr; + using _locale_t_ref = struct __locale_struct; #endif + /// \endcond + + /// + /// locale_t helper class to free_locale when going out of scope. + /// + class locale : public std::unique_ptr<_locale_t_ref, free_locale_delete> + { + public: + locale() = default; + + locale(_In_ locale_t ptr) : + std::unique_ptr<_locale_t_ref, free_locale_delete>(ptr) + {} + + locale(_In_ int category, _In_z_ const char* locale) : + locale(create_locale(category, locale)) + {} + + locale(_In_ int category, _In_z_ const wchar_t* locale) : + locale(create_locale(category, locale)) + {} + + operator locale_t() const { return get(); } + }; /// /// Reusable C-locale