com_code.hpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /* Proposed SG14 status_code
  2. (C) 2018-2019 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. static _base::string_ref _make_string_ref(HRESULT c, IErrorInfo *perrinfo = nullptr) noexcept
  56. {
  57. _com_error ce(c, perrinfo);
  58. #ifdef _UNICODE
  59. win32::DWORD wlen = (win32::DWORD) wcslen(ce.ErrorMessage());
  60. size_t allocation = wlen + (wlen >> 1);
  61. win32::DWORD bytes;
  62. if(wlen == 0)
  63. {
  64. return _base::string_ref("failed to get message from system");
  65. }
  66. for(;;)
  67. {
  68. auto *p = static_cast<char *>(malloc(allocation)); // NOLINT
  69. if(p == nullptr)
  70. {
  71. return _base::string_ref("failed to get message from system");
  72. }
  73. bytes = win32::WideCharToMultiByte(65001 /*CP_UTF8*/, 0, ce.ErrorMessage(), wlen + 1, p, allocation, nullptr, nullptr);
  74. if(bytes != 0)
  75. {
  76. char *end = strchr(p, 0);
  77. while(end[-1] == 10 || end[-1] == 13)
  78. {
  79. --end;
  80. }
  81. *end = 0; // NOLINT
  82. return _base::atomic_refcounted_string_ref(p, end - p);
  83. }
  84. free(p); // NOLINT
  85. if(win32::GetLastError() == 0x7a /*ERROR_INSUFFICIENT_BUFFER*/)
  86. {
  87. allocation += allocation >> 2;
  88. continue;
  89. }
  90. return _base::string_ref("failed to get message from system");
  91. }
  92. #else
  93. auto wlen = static_cast<win32::DWORD>(strlen(ce.ErrorMessage()));
  94. auto *p = static_cast<char *>(malloc(wlen + 1)); // NOLINT
  95. if(p == nullptr)
  96. {
  97. return _base::string_ref("failed to get message from system");
  98. }
  99. memcpy(p, ce.ErrorMessage(), wlen + 1);
  100. char *end = strchr(p, 0);
  101. while(end[-1] == 10 || end[-1] == 13)
  102. {
  103. --end;
  104. }
  105. *end = 0; // NOLINT
  106. return _base::atomic_refcounted_string_ref(p, end - p);
  107. #endif
  108. }
  109. public:
  110. //! The value type of the COM code, which is a `HRESULT`
  111. using value_type = HRESULT;
  112. using _base::string_ref;
  113. public:
  114. //! Default constructor
  115. constexpr explicit _com_code_domain(typename _base::unique_id_type id = 0xdc8275428b4effac) noexcept : _base(id) {}
  116. _com_code_domain(const _com_code_domain &) = default;
  117. _com_code_domain(_com_code_domain &&) = default;
  118. _com_code_domain &operator=(const _com_code_domain &) = default;
  119. _com_code_domain &operator=(_com_code_domain &&) = default;
  120. ~_com_code_domain() = default;
  121. //! Constexpr singleton getter. Returns the constexpr com_code_domain variable.
  122. static inline constexpr const _com_code_domain &get();
  123. virtual string_ref name() const noexcept override { return string_ref("COM domain"); } // NOLINT
  124. protected:
  125. virtual bool _do_failure(const status_code<void> &code) const noexcept override // NOLINT
  126. {
  127. assert(code.domain() == *this);
  128. return static_cast<const com_code &>(code).value() < 0; // NOLINT
  129. }
  130. /*! Note semantic equivalence testing is only implemented for `FACILITY_WIN32` and `FACILITY_NT_BIT`.
  131. */
  132. virtual bool _do_equivalent(const status_code<void> &code1, const status_code<void> &code2) const noexcept override // NOLINT
  133. {
  134. assert(code1.domain() == *this);
  135. const auto &c1 = static_cast<const com_code &>(code1); // NOLINT
  136. if(code2.domain() == *this)
  137. {
  138. const auto &c2 = static_cast<const com_code &>(code2); // NOLINT
  139. return c1.value() == c2.value();
  140. }
  141. if((c1.value() & FACILITY_NT_BIT) != 0)
  142. {
  143. if(code2.domain() == nt_code_domain)
  144. {
  145. const auto &c2 = static_cast<const nt_code &>(code2); // NOLINT
  146. if(c2.value() == (c1.value() & ~FACILITY_NT_BIT))
  147. {
  148. return true;
  149. }
  150. }
  151. else if(code2.domain() == generic_code_domain)
  152. {
  153. const auto &c2 = static_cast<const generic_code &>(code2); // NOLINT
  154. if(static_cast<int>(c2.value()) == _nt_code_domain::_nt_code_to_errno(c1.value() & ~FACILITY_NT_BIT))
  155. {
  156. return true;
  157. }
  158. }
  159. }
  160. else if(HRESULT_FACILITY(c1.value()) == FACILITY_WIN32)
  161. {
  162. if(code2.domain() == win32_code_domain)
  163. {
  164. const auto &c2 = static_cast<const win32_code &>(code2); // NOLINT
  165. if(c2.value() == HRESULT_CODE(c1.value()))
  166. {
  167. return true;
  168. }
  169. }
  170. else if(code2.domain() == generic_code_domain)
  171. {
  172. const auto &c2 = static_cast<const generic_code &>(code2); // NOLINT
  173. if(static_cast<int>(c2.value()) == _win32_code_domain::_win32_code_to_errno(HRESULT_CODE(c1.value())))
  174. {
  175. return true;
  176. }
  177. }
  178. }
  179. return false;
  180. }
  181. virtual generic_code _generic_code(const status_code<void> &code) const noexcept override // NOLINT
  182. {
  183. assert(code.domain() == *this);
  184. const auto &c1 = static_cast<const com_code &>(code); // NOLINT
  185. if(c1.value() == S_OK)
  186. {
  187. return generic_code(errc::success);
  188. }
  189. if((c1.value() & FACILITY_NT_BIT) != 0)
  190. {
  191. return generic_code(static_cast<errc>(_nt_code_domain::_nt_code_to_errno(c1.value() & ~FACILITY_NT_BIT)));
  192. }
  193. if(HRESULT_FACILITY(c1.value()) == FACILITY_WIN32)
  194. {
  195. return generic_code(static_cast<errc>(_win32_code_domain::_win32_code_to_errno(HRESULT_CODE(c1.value()))));
  196. }
  197. return generic_code(errc::unknown);
  198. }
  199. virtual string_ref _do_message(const status_code<void> &code) const noexcept override // NOLINT
  200. {
  201. assert(code.domain() == *this);
  202. const auto &c = static_cast<const com_code &>(code); // NOLINT
  203. return _make_string_ref(c.value());
  204. }
  205. #if defined(_CPPUNWIND) || defined(__EXCEPTIONS) || defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE)
  206. BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN virtual void _do_throw_exception(const status_code<void> &code) const override // NOLINT
  207. {
  208. assert(code.domain() == *this);
  209. const auto &c = static_cast<const com_code &>(code); // NOLINT
  210. throw status_error<_com_code_domain>(c);
  211. }
  212. #endif
  213. };
  214. //! (Windows only) A constexpr source variable for the COM code domain. Returned by `_com_code_domain::get()`.
  215. constexpr _com_code_domain com_code_domain;
  216. inline constexpr const _com_code_domain &_com_code_domain::get()
  217. {
  218. return com_code_domain;
  219. }
  220. BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END
  221. #endif