localization_backend.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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_LOCALIZATION_BACKEND_HPP
  7. #define BOOST_LOCALE_LOCALIZATION_BACKEND_HPP
  8. #include <boost/locale/generator.hpp>
  9. #include <boost/locale/hold_ptr.hpp>
  10. #include <locale>
  11. #include <memory>
  12. #include <string>
  13. #include <vector>
  14. #ifdef BOOST_MSVC
  15. # pragma warning(push)
  16. # pragma warning(disable : 4275 4251 4231 4660)
  17. #endif
  18. namespace boost { namespace locale {
  19. /// \brief this class represents a localization backend that can be used for localizing your application.
  20. ///
  21. /// Backends are usually registered inside the localization backends manager and allow transparent support
  22. /// of different backends, so a user can switch the backend by simply linking the application to the correct one.
  23. ///
  24. /// Backends may support different tuning options, but these are the default options available to the user
  25. /// for all of them
  26. ///
  27. /// -# \c locale - the name of the locale in POSIX format like en_US.UTF-8
  28. /// -# \c use_ansi_encoding - select system locale using ANSI codepages rather then UTF-8 under Windows
  29. /// by default
  30. /// -# \c message_path - path to the location of message catalogs (vector of strings)
  31. /// -# \c message_application - the name of applications that use message catalogs (vector of strings)
  32. ///
  33. /// Each backend can be installed with a different default priority so when you work with two different backends,
  34. /// you can specify priority so this backend will be chosen according to their priority.
  35. class BOOST_LOCALE_DECL localization_backend {
  36. protected:
  37. localization_backend(const localization_backend&) = default;
  38. localization_backend& operator=(const localization_backend&) = default;
  39. public:
  40. localization_backend() = default;
  41. virtual ~localization_backend();
  42. /// Make a polymorphic copy of the backend
  43. virtual localization_backend* clone() const = 0;
  44. /// Set option for backend, for example "locale" or "encoding"
  45. virtual void set_option(const std::string& name, const std::string& value) = 0;
  46. /// Clear all options
  47. virtual void clear_options() = 0;
  48. /// Create a facet for category \a category and character type \a type
  49. virtual std::locale install(const std::locale& base, category_t category, char_facet_t type) = 0;
  50. }; // localization_backend
  51. /// \brief Localization backend manager is a class that holds various backend and allows creation
  52. /// of their combination or selection
  53. class BOOST_LOCALE_DECL localization_backend_manager {
  54. public:
  55. /// New empty localization_backend_manager
  56. localization_backend_manager();
  57. /// Copy localization_backend_manager
  58. localization_backend_manager(const localization_backend_manager&);
  59. /// Assign localization_backend_manager
  60. localization_backend_manager& operator=(const localization_backend_manager&);
  61. /// Move construct localization_backend_manager
  62. localization_backend_manager(localization_backend_manager&&) noexcept;
  63. /// Move assign localization_backend_manager
  64. localization_backend_manager& operator=(localization_backend_manager&&) noexcept;
  65. /// Destructor
  66. ~localization_backend_manager();
  67. /// Create new localization backend according to current settings. Ownership is passed to caller
  68. std::unique_ptr<localization_backend> create() const;
  69. BOOST_DEPRECATED("This function is deprecated, use 'create()' instead")
  70. std::unique_ptr<localization_backend> get() const { return create(); } // LCOV_EXCL_LINE
  71. BOOST_DEPRECATED("This function is deprecated, use 'create()' instead")
  72. std::unique_ptr<localization_backend> get_unique_ptr() const { return create(); } // LCOV_EXCL_LINE
  73. /// Add new backend to the manager, each backend should be uniquely defined by its name.
  74. ///
  75. /// This library provides: "icu", "posix", "winapi" and "std" backends.
  76. void add_backend(const std::string& name, std::unique_ptr<localization_backend> backend);
  77. // clang-format off
  78. BOOST_DEPRECATED("This function is deprecated, use 'add_backend' instead")
  79. void adopt_backend(const std::string& name, localization_backend* backend) { add_backend(name, std::unique_ptr<localization_backend>(backend)); } // LCOV_EXCL_LINE
  80. // clang-format on
  81. /// Clear backend
  82. void remove_all_backends();
  83. /// Get list of all available backends
  84. std::vector<std::string> get_all_backends() const;
  85. /// Select specific backend by name for a category \a category. It allows combining different
  86. /// backends for user preferences.
  87. void select(const std::string& backend_name, category_t category = all_categories);
  88. /// Set new global backend manager, the old one is returned.
  89. ///
  90. /// This function is thread safe
  91. static localization_backend_manager global(const localization_backend_manager&);
  92. /// Get global backend manager
  93. ///
  94. /// This function is thread safe
  95. static localization_backend_manager global();
  96. private:
  97. class impl;
  98. hold_ptr<impl> pimpl_;
  99. };
  100. }} // namespace boost::locale
  101. #ifdef BOOST_MSVC
  102. # pragma warning(pop)
  103. #endif
  104. #endif