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