generator.hpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. //
  2. // Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // https://www.boost.org/LICENSE_1_0.txt
  6. #ifndef BOOST_LOCALE_GENERATOR_HPP
  7. #define BOOST_LOCALE_GENERATOR_HPP
  8. #include <boost/locale/hold_ptr.hpp>
  9. #include <cstdint>
  10. #include <locale>
  11. #include <memory>
  12. #include <string>
  13. #ifdef BOOST_MSVC
  14. # pragma warning(push)
  15. # pragma warning(disable : 4275 4251 4231 4660)
  16. #endif
  17. namespace boost {
  18. ///
  19. /// \brief This is the main namespace that encloses all localization classes
  20. ///
  21. namespace locale {
  22. class localization_backend;
  23. class localization_backend_manager;
  24. /// Type that specifies the character type that locales can be generated for
  25. ///
  26. /// Supports bitwise OR and bitwise AND (the latter returning if the type is set)
  27. enum class char_facet_t : uint32_t {
  28. nochar = 0, ///< Unspecified character category for character independent facets
  29. char_f = 1 << 0, ///< 8-bit character facets
  30. wchar_f = 1 << 1, ///< wide character facets
  31. #ifdef __cpp_char8_t
  32. char8_f = 1 << 2, ///< C++20 char8_t facets
  33. #endif
  34. #ifdef BOOST_LOCALE_ENABLE_CHAR16_T
  35. char16_f = 1 << 3, ///< C++11 char16_t facets
  36. #endif
  37. #ifdef BOOST_LOCALE_ENABLE_CHAR32_T
  38. char32_f = 1 << 4, ///< C++11 char32_t facets
  39. #endif
  40. };
  41. typedef BOOST_DEPRECATED("Use char_facet_t") char_facet_t character_facet_type;
  42. /// First facet specific for character type
  43. constexpr char_facet_t character_facet_first = char_facet_t::char_f;
  44. /// Last facet specific for character type
  45. constexpr char_facet_t character_facet_last =
  46. #ifdef BOOST_LOCALE_ENABLE_CHAR32_T
  47. char_facet_t::char32_f;
  48. #elif defined BOOST_LOCALE_ENABLE_CHAR16_T
  49. char_facet_t::char16_f;
  50. #elif defined __cpp_char8_t
  51. char_facet_t::char8_f;
  52. #else
  53. char_facet_t::wchar_f;
  54. #endif
  55. /// Special mask -- generate all
  56. constexpr char_facet_t all_characters = char_facet_t(0xFFFFFFFFu);
  57. /// Type used for more fine grained generation of facets
  58. ///
  59. /// Supports bitwise OR and bitwise AND (the latter returning if the type is set)
  60. enum class category_t : uint32_t {
  61. convert = 1 << 0, ///< Generate conversion facets
  62. collation = 1 << 1, ///< Generate collation facets
  63. formatting = 1 << 2, ///< Generate numbers, currency, date-time formatting facets
  64. parsing = 1 << 3, ///< Generate numbers, currency, date-time formatting facets
  65. message = 1 << 4, ///< Generate message facets
  66. codepage = 1 << 5, ///< Generate character set conversion facets (derived from std::codecvt)
  67. boundary = 1 << 6, ///< Generate boundary analysis facet
  68. calendar = 1 << 16, ///< Generate boundary analysis facet
  69. information = 1 << 17, ///< Generate general locale information facet
  70. };
  71. typedef BOOST_DEPRECATED("Use category_t") category_t locale_category_type;
  72. /// First facet specific for character
  73. constexpr category_t per_character_facet_first = category_t::convert;
  74. /// Last facet specific for character
  75. constexpr category_t per_character_facet_last = category_t::boundary;
  76. /// First character independent facet
  77. constexpr category_t non_character_facet_first = category_t::calendar;
  78. /// Last character independent facet
  79. constexpr category_t non_character_facet_last = category_t::information;
  80. /// First category facet
  81. constexpr category_t category_first = category_t::convert;
  82. /// Last category facet
  83. constexpr category_t category_last = category_t::information;
  84. /// Generate all of them
  85. constexpr category_t all_categories = category_t(0xFFFFFFFFu);
  86. /// \brief the major class used for locale generation
  87. ///
  88. /// This class is used for specification of all parameters required for locale generation and
  89. /// caching. This class const member functions are thread safe if locale class implementation is thread safe.
  90. class BOOST_LOCALE_DECL generator {
  91. public:
  92. /// Create new generator using global localization_backend_manager
  93. generator();
  94. /// Create new generator using specific localization_backend_manager
  95. generator(const localization_backend_manager&);
  96. ~generator();
  97. /// Set types of facets that should be generated, default all
  98. void categories(category_t cats);
  99. /// Get types of facets that should be generated, default all
  100. category_t categories() const;
  101. /// Set the characters type for which the facets should be generated, default all supported
  102. void characters(char_facet_t chars);
  103. /// Get the characters type for which the facets should be generated, default all supported
  104. char_facet_t characters() const;
  105. /// Add a new domain of messages that would be generated. It should be set in order to enable
  106. /// messages support.
  107. ///
  108. /// Messages domain has following format: "name" or "name/encoding"
  109. /// where name is the base name of the "mo" file where the catalog is stored
  110. /// without ".mo" extension. For example for file \c /usr/share/locale/he/LC_MESSAGES/blog.mo
  111. /// it would be \c blog.
  112. ///
  113. /// You can optionally specify the encoding of the keys in the sources by adding "/encoding_name"
  114. /// For example blog/cp1255.
  115. ///
  116. /// If not defined all keys are assumed to be UTF-8 encoded.
  117. ///
  118. /// \note When you select a domain for the program using dgettext or message API, you
  119. /// do not specify the encoding part. So for example if the provided
  120. /// domain name was "blog/windows-1255" then for translation
  121. /// you should use dgettext("blog","Hello")
  122. void add_messages_domain(const std::string& domain);
  123. /// Set default message domain. If this member was not called, the first added messages domain is used.
  124. /// If the domain \a domain is not added yet it is added.
  125. void set_default_messages_domain(const std::string& domain);
  126. /// Remove all added domains from the list
  127. void clear_domains();
  128. /// Add a search path where dictionaries are looked in.
  129. ///
  130. /// \note
  131. ///
  132. /// - Under the Windows platform the path is treated as a path in the locale's encoding so
  133. /// if you create locale "en_US.windows-1251" then path would be treated as cp1255,
  134. /// and if it is en_US.UTF-8 it is treated as UTF-8. File name is always opened with
  135. /// a wide file name as wide file names are the native file name on Windows.
  136. ///
  137. /// - Under POSIX platforms all paths passed as-is regardless of encoding as narrow
  138. /// encodings are the native encodings for POSIX platforms.
  139. ///
  140. void add_messages_path(const std::string& path);
  141. /// Remove all added paths
  142. void clear_paths();
  143. /// Remove all cached locales
  144. void clear_cache();
  145. /// Turn locale caching ON
  146. void locale_cache_enabled(bool on);
  147. /// Get locale cache option
  148. bool locale_cache_enabled() const;
  149. /// Check if by default ANSI encoding is selected or UTF-8 onces. The default is false.
  150. bool use_ansi_encoding() const;
  151. /// Select ANSI encodings as default system encoding rather then UTF-8 by default
  152. /// under Windows.
  153. ///
  154. /// The default is the most portable and most powerful encoding, UTF-8, but the user
  155. /// can select "system" one if dealing with legacy applications
  156. void use_ansi_encoding(bool enc);
  157. /// Generate a locale with id \a id
  158. std::locale generate(const std::string& id) const;
  159. /// Generate a locale with id \a id. Use \a base as a locale to which all facets are added,
  160. /// instead of std::locale::classic().
  161. std::locale generate(const std::locale& base, const std::string& id) const;
  162. /// Shortcut to generate(id)
  163. std::locale operator()(const std::string& id) const { return generate(id); }
  164. private:
  165. void set_all_options(localization_backend& backend, const std::string& id) const;
  166. generator(const generator&);
  167. void operator=(const generator&);
  168. struct data;
  169. hold_ptr<data> d;
  170. };
  171. constexpr char_facet_t operator|(const char_facet_t lhs, const char_facet_t rhs)
  172. {
  173. return char_facet_t(static_cast<uint32_t>(lhs) | static_cast<uint32_t>(rhs));
  174. }
  175. constexpr char_facet_t operator^(const char_facet_t lhs, const char_facet_t rhs)
  176. {
  177. return char_facet_t(static_cast<uint32_t>(lhs) ^ static_cast<uint32_t>(rhs));
  178. }
  179. constexpr bool operator&(const char_facet_t lhs, const char_facet_t rhs)
  180. {
  181. return (static_cast<uint32_t>(lhs) & static_cast<uint32_t>(rhs)) != 0u;
  182. }
  183. // Prefix increment: Return the next value
  184. BOOST_CXX14_CONSTEXPR inline char_facet_t& operator++(char_facet_t& v)
  185. {
  186. return v = char_facet_t(static_cast<uint32_t>(v) ? static_cast<uint32_t>(v) << 1 : 1);
  187. }
  188. constexpr category_t operator|(const category_t lhs, const category_t rhs)
  189. {
  190. return category_t(static_cast<uint32_t>(lhs) | static_cast<uint32_t>(rhs));
  191. }
  192. constexpr category_t operator^(const category_t lhs, const category_t rhs)
  193. {
  194. return category_t(static_cast<uint32_t>(lhs) ^ static_cast<uint32_t>(rhs));
  195. }
  196. constexpr bool operator&(const category_t lhs, const category_t rhs)
  197. {
  198. return (static_cast<uint32_t>(lhs) & static_cast<uint32_t>(rhs)) != 0u;
  199. }
  200. // Prefix increment: Return the next value
  201. BOOST_CXX14_CONSTEXPR inline category_t& operator++(category_t& v)
  202. {
  203. return v = category_t(static_cast<uint32_t>(v) << 1);
  204. }
  205. } // namespace locale
  206. } // namespace boost
  207. #ifdef BOOST_MSVC
  208. # pragma warning(pop)
  209. #endif
  210. #endif