123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550 |
- #ifndef BOOST_LOCALE_MESSAGE_HPP_INCLUDED
- #define BOOST_LOCALE_MESSAGE_HPP_INCLUDED
- #include <boost/locale/detail/facet_id.hpp>
- #include <boost/locale/detail/is_supported_char.hpp>
- #include <boost/locale/formatting.hpp>
- #include <boost/locale/util/string.hpp>
- #include <locale>
- #include <memory>
- #include <set>
- #include <string>
- #include <type_traits>
- #ifdef BOOST_MSVC
- # pragma warning(push)
- # pragma warning(disable : 4275 4251 4231 4660)
- #endif
- #ifdef gettext
- # undef gettext
- # undef ngettext
- # undef dgettext
- # undef dngettext
- #endif
- namespace boost { namespace locale {
-
-
-
-
-
-
-
-
- using count_type = long long;
-
- template<typename CharType>
- class BOOST_SYMBOL_VISIBLE message_format : public std::locale::facet,
- public detail::facet_id<message_format<CharType>> {
- BOOST_LOCALE_ASSERT_IS_SUPPORTED(CharType);
- public:
-
- typedef CharType char_type;
-
- typedef std::basic_string<CharType> string_type;
-
- message_format(size_t refs = 0) : std::locale::facet(refs) {}
-
-
-
-
-
-
-
- virtual const char_type* get(int domain_id, const char_type* context, const char_type* id) const = 0;
-
-
-
-
-
-
-
-
-
-
- virtual const char_type*
- get(int domain_id, const char_type* context, const char_type* single_id, count_type n) const = 0;
-
- virtual int domain(const std::string& domain) const = 0;
-
-
-
-
-
-
- virtual const char_type* convert(const char_type* msg, string_type& buffer) const = 0;
- };
-
- namespace detail {
- inline bool is_us_ascii_char(char c)
- {
-
- return 0 < c && c < 0x7F;
- }
- inline bool is_us_ascii_string(const char* msg)
- {
- while(*msg) {
- if(!is_us_ascii_char(*msg++))
- return false;
- }
- return true;
- }
- template<typename CharType>
- struct string_cast_traits {
- static const CharType* cast(const CharType* msg, std::basic_string<CharType>& ) { return msg; }
- };
- template<>
- struct string_cast_traits<char> {
- static const char* cast(const char* msg, std::string& buffer)
- {
- if(is_us_ascii_string(msg))
- return msg;
- buffer.reserve(strlen(msg));
- char c;
- while((c = *msg++) != 0) {
- if(is_us_ascii_char(c))
- buffer += c;
- }
- return buffer.c_str();
- }
- };
- }
-
-
-
-
-
-
- template<typename CharType>
- class basic_message {
- public:
- typedef CharType char_type;
- typedef std::basic_string<char_type> string_type;
- typedef message_format<char_type> facet_type;
-
- basic_message() : n_(0), c_id_(nullptr), c_context_(nullptr), c_plural_(nullptr) {}
-
-
- explicit basic_message(const char_type* id) : n_(0), c_id_(id), c_context_(nullptr), c_plural_(nullptr) {}
-
-
-
-
- explicit basic_message(const char_type* single, const char_type* plural, count_type n) :
- n_(n), c_id_(single), c_context_(nullptr), c_plural_(plural)
- {}
-
-
-
- explicit basic_message(const char_type* context, const char_type* id) :
- n_(0), c_id_(id), c_context_(context), c_plural_(nullptr)
- {}
-
-
-
-
- explicit basic_message(const char_type* context,
- const char_type* single,
- const char_type* plural,
- count_type n) :
- n_(n),
- c_id_(single), c_context_(context), c_plural_(plural)
- {}
-
- explicit basic_message(const string_type& id) :
- n_(0), c_id_(nullptr), c_context_(nullptr), c_plural_(nullptr), id_(id)
- {}
-
-
-
- explicit basic_message(const string_type& single, const string_type& plural, count_type number) :
- n_(number), c_id_(nullptr), c_context_(nullptr), c_plural_(nullptr), id_(single), plural_(plural)
- {}
-
- explicit basic_message(const string_type& context, const string_type& id) :
- n_(0), c_id_(nullptr), c_context_(nullptr), c_plural_(nullptr), id_(id), context_(context)
- {}
-
-
-
- explicit basic_message(const string_type& context,
- const string_type& single,
- const string_type& plural,
- count_type number) :
- n_(number),
- c_id_(nullptr), c_context_(nullptr), c_plural_(nullptr), id_(single), context_(context), plural_(plural)
- {}
-
- basic_message(const basic_message&) = default;
- basic_message(basic_message&&) noexcept = default;
-
- basic_message& operator=(const basic_message&) = default;
- basic_message&
- operator=(basic_message&&) noexcept(std::is_nothrow_move_assignable<string_type>::value) = default;
-
- void
- swap(basic_message& other) noexcept(noexcept(std::declval<string_type&>().swap(std::declval<string_type&>())))
- {
- using std::swap;
- swap(n_, other.n_);
- swap(c_id_, other.c_id_);
- swap(c_context_, other.c_context_);
- swap(c_plural_, other.c_plural_);
- swap(id_, other.id_);
- swap(context_, other.context_);
- swap(plural_, other.plural_);
- }
- friend void swap(basic_message& x, basic_message& y) noexcept(noexcept(x.swap(y))) { x.swap(y); }
-
- operator string_type() const { return str(); }
-
- string_type str() const { return str(std::locale()); }
-
- string_type str(const std::locale& locale) const { return str(locale, 0); }
-
- string_type str(const std::locale& locale, const std::string& domain_id) const
- {
- int id = 0;
- if(std::has_facet<facet_type>(locale))
- id = std::use_facet<facet_type>(locale).domain(domain_id);
- return str(locale, id);
- }
-
- string_type str(const std::string& domain_id) const { return str(std::locale(), domain_id); }
-
- string_type str(const std::locale& loc, int id) const
- {
- string_type buffer;
- const char_type* ptr = write(loc, id, buffer);
- if(ptr != buffer.c_str())
- buffer = ptr;
- return buffer;
- }
-
-
- void write(std::basic_ostream<char_type>& out) const
- {
- const std::locale& loc = out.getloc();
- int id = ios_info::get(out).domain_id();
- string_type buffer;
- out << write(loc, id, buffer);
- }
- private:
- const char_type* plural() const
- {
- if(c_plural_)
- return c_plural_;
- if(plural_.empty())
- return nullptr;
- return plural_.c_str();
- }
- const char_type* context() const
- {
- if(c_context_)
- return c_context_;
- if(context_.empty())
- return nullptr;
- return context_.c_str();
- }
- const char_type* id() const { return c_id_ ? c_id_ : id_.c_str(); }
- const char_type* write(const std::locale& loc, int domain_id, string_type& buffer) const
- {
- static const char_type empty_string[1] = {0};
- const char_type* id = this->id();
- const char_type* context = this->context();
- const char_type* plural = this->plural();
- if(*id == 0)
- return empty_string;
- const facet_type* facet = nullptr;
- if(std::has_facet<facet_type>(loc))
- facet = &std::use_facet<facet_type>(loc);
- const char_type* translated = nullptr;
- if(facet) {
- if(!plural)
- translated = facet->get(domain_id, context, id);
- else
- translated = facet->get(domain_id, context, id, n_);
- }
- if(!translated) {
- const char_type* msg = plural ? (n_ == 1 ? id : plural) : id;
- if(facet)
- translated = facet->convert(msg, buffer);
- else
- translated = detail::string_cast_traits<char_type>::cast(msg, buffer);
- }
- return translated;
- }
-
- count_type n_;
- const char_type* c_id_;
- const char_type* c_context_;
- const char_type* c_plural_;
- string_type id_;
- string_type context_;
- string_type plural_;
- };
-
- typedef basic_message<char> message;
-
- typedef basic_message<wchar_t> wmessage;
- #ifndef BOOST_LOCALE_NO_CXX20_STRING8
-
- typedef basic_message<char8_t> u8message;
- #endif
- #ifdef BOOST_LOCALE_ENABLE_CHAR16_T
-
- typedef basic_message<char16_t> u16message;
- #endif
- #ifdef BOOST_LOCALE_ENABLE_CHAR32_T
-
- typedef basic_message<char32_t> u32message;
- #endif
-
- template<typename CharType>
- std::basic_ostream<CharType>& operator<<(std::basic_ostream<CharType>& out, const basic_message<CharType>& msg)
- {
- msg.write(out);
- return out;
- }
-
-
-
- template<typename CharType>
- inline basic_message<CharType> translate(const CharType* msg)
- {
- return basic_message<CharType>(msg);
- }
-
- template<typename CharType>
- inline basic_message<CharType> translate(const CharType* context, const CharType* msg)
- {
- return basic_message<CharType>(context, msg);
- }
-
- template<typename CharType>
- inline basic_message<CharType> translate(const CharType* single, const CharType* plural, count_type n)
- {
- return basic_message<CharType>(single, plural, n);
- }
-
- template<typename CharType>
- inline basic_message<CharType>
- translate(const CharType* context, const CharType* single, const CharType* plural, count_type n)
- {
- return basic_message<CharType>(context, single, plural, n);
- }
-
- template<typename CharType>
- inline basic_message<CharType> translate(const std::basic_string<CharType>& msg)
- {
- return basic_message<CharType>(msg);
- }
-
- template<typename CharType>
- inline basic_message<CharType> translate(const std::basic_string<CharType>& context,
- const std::basic_string<CharType>& msg)
- {
- return basic_message<CharType>(context, msg);
- }
-
- template<typename CharType>
- inline basic_message<CharType> translate(const std::basic_string<CharType>& context,
- const std::basic_string<CharType>& single,
- const std::basic_string<CharType>& plural,
- count_type n)
- {
- return basic_message<CharType>(context, single, plural, n);
- }
-
- template<typename CharType>
- inline basic_message<CharType>
- translate(const std::basic_string<CharType>& single, const std::basic_string<CharType>& plural, count_type n)
- {
- return basic_message<CharType>(single, plural, n);
- }
-
-
-
- template<typename CharType>
- std::basic_string<CharType> gettext(const CharType* id, const std::locale& loc = std::locale())
- {
- return basic_message<CharType>(id).str(loc);
- }
-
- template<typename CharType>
- std::basic_string<CharType>
- ngettext(const CharType* s, const CharType* p, count_type n, const std::locale& loc = std::locale())
- {
- return basic_message<CharType>(s, p, n).str(loc);
- }
-
- template<typename CharType>
- std::basic_string<CharType> dgettext(const char* domain, const CharType* id, const std::locale& loc = std::locale())
- {
- return basic_message<CharType>(id).str(loc, domain);
- }
-
- template<typename CharType>
- std::basic_string<CharType> dngettext(const char* domain,
- const CharType* s,
- const CharType* p,
- count_type n,
- const std::locale& loc = std::locale())
- {
- return basic_message<CharType>(s, p, n).str(loc, domain);
- }
-
- template<typename CharType>
- std::basic_string<CharType>
- pgettext(const CharType* context, const CharType* id, const std::locale& loc = std::locale())
- {
- return basic_message<CharType>(context, id).str(loc);
- }
-
- template<typename CharType>
- std::basic_string<CharType> npgettext(const CharType* context,
- const CharType* s,
- const CharType* p,
- count_type n,
- const std::locale& loc = std::locale())
- {
- return basic_message<CharType>(context, s, p, n).str(loc);
- }
-
- template<typename CharType>
- std::basic_string<CharType>
- dpgettext(const char* domain, const CharType* context, const CharType* id, const std::locale& loc = std::locale())
- {
- return basic_message<CharType>(context, id).str(loc, domain);
- }
-
- template<typename CharType>
- std::basic_string<CharType> dnpgettext(const char* domain,
- const CharType* context,
- const CharType* s,
- const CharType* p,
- count_type n,
- const std::locale& loc = std::locale())
- {
- return basic_message<CharType>(context, s, p, n).str(loc, domain);
- }
-
- namespace as {
-
- namespace detail {
- struct set_domain {
- std::string domain_id;
- };
- template<typename CharType>
- std::basic_ostream<CharType>& operator<<(std::basic_ostream<CharType>& out, const set_domain& dom)
- {
- int id = std::use_facet<message_format<CharType>>(out.getloc()).domain(dom.domain_id);
- ios_info::get(out).domain_id(id);
- return out;
- }
- }
-
-
-
-
-
-
-
-
- inline
- #ifdef BOOST_LOCALE_DOXYGEN
- unspecified_type
- #else
- detail::set_domain
- #endif
- domain(const std::string& id)
- {
- detail::set_domain tmp = {id};
- return tmp;
- }
-
- }
- }}
- #ifdef BOOST_MSVC
- # pragma warning(pop)
- #endif
- #endif
|