1 | // codecvt_error_category implementation file ----------------------------------------// |
2 | |
3 | // Copyright 2009 Beman Dawes |
4 | // Copyright 2022 Andrey Semashev |
5 | |
6 | // Distributed under the Boost Software License, Version 1.0. |
7 | // See http://www.boost.org/LICENSE_1_0.txt) |
8 | |
9 | // Library home page at http://www.boost.org/libs/filesystem |
10 | |
11 | //--------------------------------------------------------------------------------------// |
12 | |
13 | #include "platform_config.hpp" |
14 | |
15 | #include <boost/config/warning_disable.hpp> |
16 | |
17 | #include <boost/filesystem/config.hpp> |
18 | #include <boost/filesystem/detail/path_traits.hpp> |
19 | #include <boost/system/error_category.hpp> |
20 | #include <locale> |
21 | #include <string> |
22 | |
23 | #include "private_config.hpp" |
24 | |
25 | #include <boost/filesystem/detail/header.hpp> // must be the last #include |
26 | |
27 | //--------------------------------------------------------------------------------------// |
28 | |
29 | namespace boost { |
30 | namespace filesystem { |
31 | |
32 | namespace { |
33 | |
34 | #if (defined(BOOST_GCC) && BOOST_GCC >= 40600) || defined(BOOST_CLANG) |
35 | #pragma GCC diagnostic push |
36 | // '(anonymous namespace)::codecvt_error_cat' has virtual functions but non-virtual destructor |
37 | // This is not a problem as instances of codecvt_error_cat are never destroyed through a pointer to base. |
38 | #pragma GCC diagnostic ignored "-Wnon-virtual-dtor" |
39 | #endif |
40 | |
41 | class codecvt_error_cat final : |
42 | public boost::system::error_category |
43 | { |
44 | public: |
45 | // clang up to version 3.8 requires a user-defined default constructor in order to be able to declare a static constant of the error category. |
46 | BOOST_SYSTEM_CONSTEXPR codecvt_error_cat() noexcept {} |
47 | const char* name() const noexcept override; |
48 | std::string message(int ev) const override; |
49 | }; |
50 | |
51 | const char* codecvt_error_cat::name() const noexcept |
52 | { |
53 | return "codecvt" ; |
54 | } |
55 | |
56 | std::string codecvt_error_cat::message(int ev) const |
57 | { |
58 | std::string str; |
59 | switch (ev) |
60 | { |
61 | case std::codecvt_base::ok: |
62 | str = "ok" ; |
63 | break; |
64 | case std::codecvt_base::partial: |
65 | str = "partial" ; |
66 | break; |
67 | case std::codecvt_base::error: |
68 | str = "error" ; |
69 | break; |
70 | case std::codecvt_base::noconv: |
71 | str = "noconv" ; |
72 | break; |
73 | default: |
74 | str = "unknown error" ; |
75 | break; |
76 | } |
77 | return str; |
78 | } |
79 | |
80 | #if (defined(BOOST_GCC) && BOOST_GCC >= 40600) || defined(BOOST_CLANG) |
81 | #pragma GCC diagnostic pop |
82 | #endif |
83 | |
84 | } // unnamed namespace |
85 | |
86 | BOOST_FILESYSTEM_DECL boost::system::error_category const& codecvt_error_category() noexcept |
87 | { |
88 | static |
89 | #if defined(BOOST_SYSTEM_HAS_CONSTEXPR) |
90 | constexpr |
91 | #else |
92 | const |
93 | #endif |
94 | codecvt_error_cat codecvt_error_cat_const; |
95 | return codecvt_error_cat_const; |
96 | } |
97 | |
98 | // Try to initialize the error category instance as early as possible to make sure it is |
99 | // available during global deinitialization stage. For MSVC, codecvt_error_category() will |
100 | // be called early by MSVC-specific initialization routine in path.cpp. |
101 | #if !defined(BOOST_SYSTEM_HAS_CONSTEXPR) && !defined(_MSC_VER) |
102 | |
103 | namespace { |
104 | |
105 | struct codecvt_error_category_initializer |
106 | { |
107 | codecvt_error_category_initializer() { boost::filesystem::codecvt_error_category(); } |
108 | }; |
109 | |
110 | BOOST_FILESYSTEM_INIT_PRIORITY(BOOST_FILESYSTEM_PATH_GLOBALS_INIT_PRIORITY) BOOST_ATTRIBUTE_UNUSED BOOST_FILESYSTEM_ATTRIBUTE_RETAIN |
111 | const codecvt_error_category_initializer g_codecvt_error_category_initializer; |
112 | |
113 | } // namespace |
114 | |
115 | #endif // !defined(BOOST_SYSTEM_HAS_CONSTEXPR) && !defined(_MSC_VER) |
116 | |
117 | } // namespace filesystem |
118 | } // namespace boost |
119 | |
120 | #include <boost/filesystem/detail/footer.hpp> |
121 | |