win32_code.hpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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_WIN32_CODE_HPP
  26. #define BOOST_OUTCOME_SYSTEM_ERROR2_WIN32_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 "quick_status_code_from_enum.hpp"
  31. BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_BEGIN
  32. //! \exclude
  33. namespace win32
  34. {
  35. // A Win32 DWORD
  36. using DWORD = unsigned long;
  37. // Used to retrieve the current Win32 error code
  38. extern DWORD __stdcall GetLastError();
  39. // Used to retrieve a locale-specific message string for some error code
  40. extern DWORD __stdcall FormatMessageW(DWORD dwFlags, const void *lpSource, DWORD dwMessageId, DWORD dwLanguageId, wchar_t *lpBuffer, DWORD nSize, void /*va_list*/ *Arguments);
  41. // Converts UTF-16 message string to UTF-8
  42. extern int __stdcall WideCharToMultiByte(unsigned int CodePage, DWORD dwFlags, const wchar_t *lpWideCharStr, int cchWideChar, char *lpMultiByteStr, int cbMultiByte, const char *lpDefaultChar, int *lpUsedDefaultChar);
  43. #pragma comment(lib, "kernel32.lib")
  44. #if(defined(__x86_64__) || defined(_M_X64)) || (defined(__aarch64__) || defined(_M_ARM64))
  45. #pragma comment(linker, "/alternatename:?GetLastError@win32@system_error2@@YAKXZ=GetLastError")
  46. #pragma comment(linker, "/alternatename:?FormatMessageW@win32@system_error2@@YAKKPEBXKKPEA_WKPEAX@Z=FormatMessageW")
  47. #pragma comment(linker, "/alternatename:?WideCharToMultiByte@win32@system_error2@@YAHIKPEB_WHPEADHPEBDPEAH@Z=WideCharToMultiByte")
  48. #elif defined(__x86__) || defined(_M_IX86) || defined(__i386__)
  49. #pragma comment(linker, "/alternatename:?GetLastError@win32@system_error2@@YGKXZ=__imp__GetLastError@0")
  50. #pragma comment(linker, "/alternatename:?FormatMessageW@win32@system_error2@@YGKKPBXKKPA_WKPAX@Z=__imp__FormatMessageW@28")
  51. #pragma comment(linker, "/alternatename:?WideCharToMultiByte@win32@system_error2@@YGHIKPB_WHPADHPBDPAH@Z=__imp__WideCharToMultiByte@32")
  52. #elif defined(__arm__) || defined(_M_ARM)
  53. #pragma comment(linker, "/alternatename:?GetLastError@win32@system_error2@@YAKXZ=GetLastError")
  54. #pragma comment(linker, "/alternatename:?FormatMessageW@win32@system_error2@@YAKKPBXKKPA_WKPAX@Z=FormatMessageW")
  55. #pragma comment(linker, "/alternatename:?WideCharToMultiByte@win32@system_error2@@YAHIKPB_WHPADHPBDPAH@Z=WideCharToMultiByte")
  56. #else
  57. #error Unknown architecture
  58. #endif
  59. } // namespace win32
  60. class _win32_code_domain;
  61. class _com_code_domain;
  62. //! (Windows only) A Win32 error code, those returned by `GetLastError()`.
  63. using win32_code = status_code<_win32_code_domain>;
  64. //! (Windows only) A specialisation of `status_error` for the Win32 error code domain.
  65. using win32_error = status_error<_win32_code_domain>;
  66. namespace mixins
  67. {
  68. template <class Base> struct mixin<Base, _win32_code_domain> : public Base
  69. {
  70. using Base::Base;
  71. //! Returns a `win32_code` for the current value of `GetLastError()`.
  72. static inline win32_code current() noexcept;
  73. };
  74. } // namespace mixins
  75. /*! (Windows only) The implementation of the domain for Win32 error codes, those returned by `GetLastError()`.
  76. */
  77. class _win32_code_domain : public status_code_domain
  78. {
  79. template <class DomainType> friend class status_code;
  80. template <class StatusCode> friend class detail::indirecting_domain;
  81. friend class _com_code_domain;
  82. using _base = status_code_domain;
  83. static int _win32_code_to_errno(win32::DWORD c)
  84. {
  85. switch(c)
  86. {
  87. case 0:
  88. return 0;
  89. #include "detail/win32_code_to_generic_code.ipp"
  90. }
  91. return -1;
  92. }
  93. //! Construct from a Win32 error code
  94. static _base::string_ref _make_string_ref(win32::DWORD c) noexcept
  95. {
  96. wchar_t buffer[32768];
  97. win32::DWORD wlen = win32::FormatMessageW(0x00001000 /*FORMAT_MESSAGE_FROM_SYSTEM*/ | 0x00000200 /*FORMAT_MESSAGE_IGNORE_INSERTS*/, nullptr, c, 0, buffer, 32768, nullptr);
  98. size_t allocation = wlen + (wlen >> 1);
  99. win32::DWORD bytes;
  100. if(wlen == 0)
  101. {
  102. return _base::string_ref("failed to get message from system");
  103. }
  104. for(;;)
  105. {
  106. auto *p = static_cast<char *>(malloc(allocation)); // NOLINT
  107. if(p == nullptr)
  108. {
  109. return _base::string_ref("failed to get message from system");
  110. }
  111. bytes = win32::WideCharToMultiByte(65001 /*CP_UTF8*/, 0, buffer, (int) (wlen + 1), p, (int) allocation, nullptr, nullptr);
  112. if(bytes != 0)
  113. {
  114. char *end = strchr(p, 0);
  115. while(end[-1] == 10 || end[-1] == 13)
  116. {
  117. --end;
  118. }
  119. *end = 0; // NOLINT
  120. return _base::atomic_refcounted_string_ref(p, end - p);
  121. }
  122. free(p); // NOLINT
  123. if(win32::GetLastError() == 0x7a /*ERROR_INSUFFICIENT_BUFFER*/)
  124. {
  125. allocation += allocation >> 2;
  126. continue;
  127. }
  128. return _base::string_ref("failed to get message from system");
  129. }
  130. }
  131. public:
  132. //! The value type of the win32 code, which is a `win32::DWORD`
  133. using value_type = win32::DWORD;
  134. using _base::string_ref;
  135. public:
  136. //! Default constructor
  137. constexpr explicit _win32_code_domain(typename _base::unique_id_type id = 0x8cd18ee72d680f1b) noexcept
  138. : _base(id)
  139. {
  140. }
  141. _win32_code_domain(const _win32_code_domain &) = default;
  142. _win32_code_domain(_win32_code_domain &&) = default;
  143. _win32_code_domain &operator=(const _win32_code_domain &) = default;
  144. _win32_code_domain &operator=(_win32_code_domain &&) = default;
  145. ~_win32_code_domain() = default;
  146. //! Constexpr singleton getter. Returns the constexpr win32_code_domain variable.
  147. static inline constexpr const _win32_code_domain &get();
  148. virtual string_ref name() const noexcept override { return string_ref("win32 domain"); } // NOLINT
  149. protected:
  150. virtual bool _do_failure(const status_code<void> &code) const noexcept override // NOLINT
  151. {
  152. assert(code.domain() == *this);
  153. return static_cast<const win32_code &>(code).value() != 0; // NOLINT
  154. }
  155. virtual bool _do_equivalent(const status_code<void> &code1, const status_code<void> &code2) const noexcept override // NOLINT
  156. {
  157. assert(code1.domain() == *this);
  158. const auto &c1 = static_cast<const win32_code &>(code1); // NOLINT
  159. if(code2.domain() == *this)
  160. {
  161. const auto &c2 = static_cast<const win32_code &>(code2); // NOLINT
  162. return c1.value() == c2.value();
  163. }
  164. if(code2.domain() == generic_code_domain)
  165. {
  166. const auto &c2 = static_cast<const generic_code &>(code2); // NOLINT
  167. if(static_cast<int>(c2.value()) == _win32_code_to_errno(c1.value()))
  168. {
  169. return true;
  170. }
  171. }
  172. return false;
  173. }
  174. virtual generic_code _generic_code(const status_code<void> &code) const noexcept override // NOLINT
  175. {
  176. assert(code.domain() == *this);
  177. const auto &c = static_cast<const win32_code &>(code); // NOLINT
  178. return generic_code(static_cast<errc>(_win32_code_to_errno(c.value())));
  179. }
  180. virtual string_ref _do_message(const status_code<void> &code) const noexcept override // NOLINT
  181. {
  182. assert(code.domain() == *this);
  183. const auto &c = static_cast<const win32_code &>(code); // NOLINT
  184. return _make_string_ref(c.value());
  185. }
  186. #if defined(_CPPUNWIND) || defined(__EXCEPTIONS) || defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE)
  187. BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN virtual void _do_throw_exception(const status_code<void> &code) const override // NOLINT
  188. {
  189. assert(code.domain() == *this);
  190. const auto &c = static_cast<const win32_code &>(code); // NOLINT
  191. throw status_error<_win32_code_domain>(c);
  192. }
  193. #endif
  194. };
  195. //! (Windows only) A constexpr source variable for the win32 code domain, which is that of `GetLastError()` (Windows). Returned by `_win32_code_domain::get()`.
  196. constexpr _win32_code_domain win32_code_domain;
  197. inline constexpr const _win32_code_domain &_win32_code_domain::get()
  198. {
  199. return win32_code_domain;
  200. }
  201. namespace mixins
  202. {
  203. template <class Base> inline win32_code mixin<Base, _win32_code_domain>::current() noexcept { return win32_code(win32::GetLastError()); }
  204. } // namespace mixins
  205. BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END
  206. #endif