generator.hpp 9.8 KB

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