| 1 | // |
| 2 | // Copyright (c) 2009-2011 Artyom Beilis (Tonkikh) |
| 3 | // Copyright (c) 2021-2022 Alexander Grund |
| 4 | // |
| 5 | // Distributed under the Boost Software License, Version 1.0. |
| 6 | // https://www.boost.org/LICENSE_1_0.txt |
| 7 | |
| 8 | #ifndef BOOST_LOCALE_PREDEFINED_FORMATTERS_HPP_INCLUDED |
| 9 | #define BOOST_LOCALE_PREDEFINED_FORMATTERS_HPP_INCLUDED |
| 10 | |
| 11 | #include <boost/locale/config.hpp> |
| 12 | #include "boost/locale/icu/icu_util.hpp" |
| 13 | #include <boost/thread/tss.hpp> |
| 14 | #include <locale> |
| 15 | |
| 16 | #ifdef BOOST_MSVC |
| 17 | # pragma warning(push) |
| 18 | # pragma warning(disable : 4251) // "identifier" : class "type" needs to have dll-interface... |
| 19 | #endif |
| 20 | #include <unicode/locid.h> |
| 21 | #include <unicode/numfmt.h> |
| 22 | #include <unicode/smpdtfmt.h> |
| 23 | #ifdef BOOST_MSVC |
| 24 | # pragma warning(pop) |
| 25 | #endif |
| 26 | |
| 27 | namespace boost { namespace locale { namespace impl_icu { |
| 28 | |
| 29 | enum class format_len { |
| 30 | Short, |
| 31 | Medium, |
| 32 | Long, |
| 33 | Full, |
| 34 | }; |
| 35 | |
| 36 | enum class num_fmt_type { number, sci, curr_nat, curr_iso, percent, spell, ordinal }; |
| 37 | |
| 38 | class formatters_cache : public std::locale::facet { |
| 39 | public: |
| 40 | static std::locale::id id; |
| 41 | |
| 42 | formatters_cache(const icu::Locale& locale); |
| 43 | |
| 44 | icu::NumberFormat& number_format(num_fmt_type type) const; |
| 45 | |
| 46 | const icu::UnicodeString& date_format(format_len f) const { return date_format_[int(f)]; } |
| 47 | |
| 48 | const icu::UnicodeString& time_format(format_len f) const { return time_format_[int(f)]; } |
| 49 | |
| 50 | const icu::UnicodeString& date_time_format(format_len d, format_len t) const |
| 51 | { |
| 52 | return date_time_format_[int(d)][int(t)]; |
| 53 | } |
| 54 | |
| 55 | const icu::UnicodeString& default_date_format() const { return default_date_format_; } |
| 56 | const icu::UnicodeString& default_time_format() const { return default_time_format_; } |
| 57 | const icu::UnicodeString& default_date_time_format() const { return default_date_time_format_; } |
| 58 | |
| 59 | icu::SimpleDateFormat* date_formatter() const; |
| 60 | |
| 61 | private: |
| 62 | icu::NumberFormat* create_number_format(num_fmt_type type, UErrorCode& err) const; |
| 63 | |
| 64 | static constexpr auto num_fmt_type_count = static_cast<unsigned>(num_fmt_type::ordinal) + 1; |
| 65 | static constexpr auto format_len_count = static_cast<unsigned>(format_len::Full) + 1; |
| 66 | |
| 67 | mutable boost::thread_specific_ptr<icu::NumberFormat> number_format_[num_fmt_type_count]; |
| 68 | icu::UnicodeString date_format_[format_len_count]; |
| 69 | icu::UnicodeString time_format_[format_len_count]; |
| 70 | icu::UnicodeString date_time_format_[format_len_count][format_len_count]; |
| 71 | icu::UnicodeString default_date_format_, default_time_format_, default_date_time_format_; |
| 72 | mutable boost::thread_specific_ptr<icu::SimpleDateFormat> date_formatter_; |
| 73 | icu::Locale locale_; |
| 74 | }; |
| 75 | |
| 76 | }}} // namespace boost::locale::impl_icu |
| 77 | |
| 78 | #endif |
| 79 | |