com_code.hpp 9.2 KB

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