]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/outcome/experimental/status-code/com_code.hpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / boost / outcome / experimental / status-code / com_code.hpp
1 /* Proposed SG14 status_code
2 (C) 2018-2019 Niall Douglas <http://www.nedproductions.biz/> (5 commits)
3 File Created: Feb 2018
4
5
6 Boost Software License - Version 1.0 - August 17th, 2003
7
8 Permission is hereby granted, free of charge, to any person or organization
9 obtaining a copy of the software and accompanying documentation covered by
10 this license (the "Software") to use, reproduce, display, distribute,
11 execute, and transmit the Software, and to prepare derivative works of the
12 Software, and to permit third-parties to whom the Software is furnished to
13 do so, all subject to the following:
14
15 The copyright notices in the Software and this entire statement, including
16 the above license grant, this restriction and the following disclaimer,
17 must be included in all copies of the Software, in whole or in part, and
18 all derivative works of the Software, unless such copies or derivative
19 works are solely in the form of machine-executable object code generated by
20 a source language processor.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
25 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
26 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
27 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 DEALINGS IN THE SOFTWARE.
29 */
30
31 #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_COM_CODE_HPP
32 #define BOOST_OUTCOME_SYSTEM_ERROR2_COM_CODE_HPP
33
34 #if !defined(_WIN32) && !defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE)
35 #error This file should only be included on Windows
36 #endif
37
38 #include "nt_code.hpp"
39 #include "win32_code.hpp"
40
41 #ifndef BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE
42 #include <comdef.h>
43 #endif
44
45 BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_BEGIN
46
47 class _com_code_domain;
48 /*! (Windows only) A COM error code. Note semantic equivalence testing is only implemented for `FACILITY_WIN32`
49 and `FACILITY_NT_BIT`. As you can see at [https://blogs.msdn.microsoft.com/eldar/2007/04/03/a-lot-of-hresult-codes/](https://blogs.msdn.microsoft.com/eldar/2007/04/03/a-lot-of-hresult-codes/),
50 there are an awful lot of COM error codes, and keeping mapping tables for all of them would be impractical
51 (for the Win32 and NT facilities, we actually reuse the mapping tables in `win32_code` and `nt_code`).
52 You can, of course, inherit your own COM code domain from this one and override the `_do_equivalent()` function
53 to add semantic equivalence testing for whichever extra COM codes that your application specifically needs.
54 */
55 using com_code = status_code<_com_code_domain>;
56 //! (Windows only) A specialisation of `status_error` for the COM error code domain.
57 using com_error = status_error<_com_code_domain>;
58
59 /*! (Windows only) The implementation of the domain for COM error codes and/or `IErrorInfo`.
60 */
61 class _com_code_domain : public status_code_domain
62 {
63 template <class DomainType> friend class status_code;
64 template <class StatusCode> friend class detail::indirecting_domain;
65 using _base = status_code_domain;
66
67 //! Construct from a `HRESULT` error code
68 static _base::string_ref _make_string_ref(HRESULT c, IErrorInfo *perrinfo = nullptr) noexcept
69 {
70 _com_error ce(c, perrinfo);
71 #ifdef _UNICODE
72 win32::DWORD wlen = (win32::DWORD) wcslen(ce.ErrorMessage());
73 size_t allocation = wlen + (wlen >> 1);
74 win32::DWORD bytes;
75 if(wlen == 0)
76 {
77 return _base::string_ref("failed to get message from system");
78 }
79 for(;;)
80 {
81 auto *p = static_cast<char *>(malloc(allocation)); // NOLINT
82 if(p == nullptr)
83 {
84 return _base::string_ref("failed to get message from system");
85 }
86 bytes = win32::WideCharToMultiByte(65001 /*CP_UTF8*/, 0, ce.ErrorMessage(), wlen + 1, p, allocation, nullptr, nullptr);
87 if(bytes != 0)
88 {
89 char *end = strchr(p, 0);
90 while(end[-1] == 10 || end[-1] == 13)
91 {
92 --end;
93 }
94 *end = 0; // NOLINT
95 return _base::atomic_refcounted_string_ref(p, end - p);
96 }
97 free(p); // NOLINT
98 if(win32::GetLastError() == 0x7a /*ERROR_INSUFFICIENT_BUFFER*/)
99 {
100 allocation += allocation >> 2;
101 continue;
102 }
103 return _base::string_ref("failed to get message from system");
104 }
105 #else
106 auto wlen = static_cast<win32::DWORD>(strlen(ce.ErrorMessage()));
107 auto *p = static_cast<char *>(malloc(wlen + 1)); // NOLINT
108 if(p == nullptr)
109 {
110 return _base::string_ref("failed to get message from system");
111 }
112 memcpy(p, ce.ErrorMessage(), wlen + 1);
113 char *end = strchr(p, 0);
114 while(end[-1] == 10 || end[-1] == 13)
115 {
116 --end;
117 }
118 *end = 0; // NOLINT
119 return _base::atomic_refcounted_string_ref(p, end - p);
120 #endif
121 }
122
123 public:
124 //! The value type of the COM code, which is a `HRESULT`
125 using value_type = HRESULT;
126 using _base::string_ref;
127
128 public:
129 //! Default constructor
130 constexpr explicit _com_code_domain(typename _base::unique_id_type id = 0xdc8275428b4effac) noexcept : _base(id) {}
131 _com_code_domain(const _com_code_domain &) = default;
132 _com_code_domain(_com_code_domain &&) = default;
133 _com_code_domain &operator=(const _com_code_domain &) = default;
134 _com_code_domain &operator=(_com_code_domain &&) = default;
135 ~_com_code_domain() = default;
136
137 //! Constexpr singleton getter. Returns the constexpr com_code_domain variable.
138 static inline constexpr const _com_code_domain &get();
139
140 virtual string_ref name() const noexcept override { return string_ref("COM domain"); } // NOLINT
141 protected:
142 virtual bool _do_failure(const status_code<void> &code) const noexcept override // NOLINT
143 {
144 assert(code.domain() == *this);
145 return static_cast<const com_code &>(code).value() < 0; // NOLINT
146 }
147 /*! Note semantic equivalence testing is only implemented for `FACILITY_WIN32` and `FACILITY_NT_BIT`.
148 */
149 virtual bool _do_equivalent(const status_code<void> &code1, const status_code<void> &code2) const noexcept override // NOLINT
150 {
151 assert(code1.domain() == *this);
152 const auto &c1 = static_cast<const com_code &>(code1); // NOLINT
153 if(code2.domain() == *this)
154 {
155 const auto &c2 = static_cast<const com_code &>(code2); // NOLINT
156 return c1.value() == c2.value();
157 }
158 if((c1.value() & FACILITY_NT_BIT) != 0)
159 {
160 if(code2.domain() == nt_code_domain)
161 {
162 const auto &c2 = static_cast<const nt_code &>(code2); // NOLINT
163 if(c2.value() == (c1.value() & ~FACILITY_NT_BIT))
164 {
165 return true;
166 }
167 }
168 else if(code2.domain() == generic_code_domain)
169 {
170 const auto &c2 = static_cast<const generic_code &>(code2); // NOLINT
171 if(static_cast<int>(c2.value()) == _nt_code_domain::_nt_code_to_errno(c1.value() & ~FACILITY_NT_BIT))
172 {
173 return true;
174 }
175 }
176 }
177 else if(HRESULT_FACILITY(c1.value()) == FACILITY_WIN32)
178 {
179 if(code2.domain() == win32_code_domain)
180 {
181 const auto &c2 = static_cast<const win32_code &>(code2); // NOLINT
182 if(c2.value() == HRESULT_CODE(c1.value()))
183 {
184 return true;
185 }
186 }
187 else if(code2.domain() == generic_code_domain)
188 {
189 const auto &c2 = static_cast<const generic_code &>(code2); // NOLINT
190 if(static_cast<int>(c2.value()) == _win32_code_domain::_win32_code_to_errno(HRESULT_CODE(c1.value())))
191 {
192 return true;
193 }
194 }
195 }
196 return false;
197 }
198 virtual generic_code _generic_code(const status_code<void> &code) const noexcept override // NOLINT
199 {
200 assert(code.domain() == *this);
201 const auto &c1 = static_cast<const com_code &>(code); // NOLINT
202 if(c1.value() == S_OK)
203 {
204 return generic_code(errc::success);
205 }
206 if((c1.value() & FACILITY_NT_BIT) != 0)
207 {
208 return generic_code(static_cast<errc>(_nt_code_domain::_nt_code_to_errno(c1.value() & ~FACILITY_NT_BIT)));
209 }
210 if(HRESULT_FACILITY(c1.value()) == FACILITY_WIN32)
211 {
212 return generic_code(static_cast<errc>(_win32_code_domain::_win32_code_to_errno(HRESULT_CODE(c1.value()))));
213 }
214 return generic_code(errc::unknown);
215 }
216 virtual string_ref _do_message(const status_code<void> &code) const noexcept override // NOLINT
217 {
218 assert(code.domain() == *this);
219 const auto &c = static_cast<const com_code &>(code); // NOLINT
220 return _make_string_ref(c.value());
221 }
222 #if defined(_CPPUNWIND) || defined(__EXCEPTIONS) || defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE)
223 BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN virtual void _do_throw_exception(const status_code<void> &code) const override // NOLINT
224 {
225 assert(code.domain() == *this);
226 const auto &c = static_cast<const com_code &>(code); // NOLINT
227 throw status_error<_com_code_domain>(c);
228 }
229 #endif
230 };
231 //! (Windows only) A constexpr source variable for the COM code domain. Returned by `_com_code_domain::get()`.
232 constexpr _com_code_domain com_code_domain;
233 inline constexpr const _com_code_domain &_com_code_domain::get()
234 {
235 return com_code_domain;
236 }
237
238 BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END
239
240 #endif