Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-18 08:53:39

0001 /* Proposed SG14 status_code
0002 (C) 2018-2024 Niall Douglas <http://www.nedproductions.biz/> (5 commits)
0003 File Created: Feb 2018
0004 
0005 
0006 Boost Software License - Version 1.0 - August 17th, 2003
0007 
0008 Permission is hereby granted, free of charge, to any person or organization
0009 obtaining a copy of the software and accompanying documentation covered by
0010 this license (the "Software") to use, reproduce, display, distribute,
0011 execute, and transmit the Software, and to prepare derivative works of the
0012 Software, and to permit third-parties to whom the Software is furnished to
0013 do so, all subject to the following:
0014 
0015 The copyright notices in the Software and this entire statement, including
0016 the above license grant, this restriction and the following disclaimer,
0017 must be included in all copies of the Software, in whole or in part, and
0018 all derivative works of the Software, unless such copies or derivative
0019 works are solely in the form of machine-executable object code generated by
0020 a source language processor.
0021 
0022 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0023 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0024 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
0025 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
0026 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
0027 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
0028 DEALINGS IN THE SOFTWARE.
0029 */
0030 
0031 #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_WIN32_CODE_HPP
0032 #define BOOST_OUTCOME_SYSTEM_ERROR2_WIN32_CODE_HPP
0033 
0034 #if !defined(_WIN32) && !defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE)
0035 #error This file should only be included on Windows
0036 #endif
0037 
0038 #include "quick_status_code_from_enum.hpp"
0039 
0040 #if defined(_MSC_VER) && !defined(__clang__)
0041 #pragma warning(push)
0042 #pragma warning(disable : 6326)  // constant comparison
0043 #endif
0044 
0045 BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_BEGIN
0046 
0047 //! \exclude
0048 namespace win32
0049 {
0050 #ifdef __MINGW32__
0051   extern "C"
0052   {
0053 #endif
0054     // A Win32 DWORD
0055     using DWORD = unsigned long;
0056     // Used to retrieve the current Win32 error code
0057     extern DWORD __stdcall GetLastError();
0058     // Used to retrieve a locale-specific message string for some error code
0059     extern DWORD __stdcall FormatMessageW(DWORD dwFlags, const void *lpSource, DWORD dwMessageId, DWORD dwLanguageId, wchar_t *lpBuffer, DWORD nSize,
0060                                           void /*va_list*/ *Arguments);
0061     // Converts UTF-16 message string to UTF-8
0062     extern int __stdcall WideCharToMultiByte(unsigned int CodePage, DWORD dwFlags, const wchar_t *lpWideCharStr, int cchWideChar, char *lpMultiByteStr,
0063                                              int cbMultiByte, const char *lpDefaultChar, int *lpUsedDefaultChar);
0064 #ifdef __MINGW32__
0065   }
0066 #else
0067 #pragma comment(lib, "kernel32.lib")
0068 #if(defined(__x86_64__) || defined(_M_X64)) || (defined(__aarch64__) || defined(_M_ARM64))
0069 #pragma comment(linker, "/alternatename:?GetLastError@win32@system_error2@@YAKXZ=GetLastError")
0070 #pragma comment(linker, "/alternatename:?FormatMessageW@win32@system_error2@@YAKKPEBXKKPEA_WKPEAX@Z=FormatMessageW")
0071 #pragma comment(linker, "/alternatename:?WideCharToMultiByte@win32@system_error2@@YAHIKPEB_WHPEADHPEBDPEAH@Z=WideCharToMultiByte")
0072 #elif defined(__x86__) || defined(_M_IX86) || defined(__i386__)
0073 #pragma comment(linker, "/alternatename:?GetLastError@win32@system_error2@@YGKXZ=_GetLastError@0")
0074 #pragma comment(linker, "/alternatename:?FormatMessageW@win32@system_error2@@YGKKPBXKKPA_WKPAX@Z=_FormatMessageW@28")
0075 #pragma comment(linker, "/alternatename:?WideCharToMultiByte@win32@system_error2@@YGHIKPB_WHPADHPBDPAH@Z=_WideCharToMultiByte@32")
0076 #elif defined(__arm__) || defined(_M_ARM)
0077 #pragma comment(linker, "/alternatename:?GetLastError@win32@system_error2@@YAKXZ=GetLastError")
0078 #pragma comment(linker, "/alternatename:?FormatMessageW@win32@system_error2@@YAKKPBXKKPA_WKPAX@Z=FormatMessageW")
0079 #pragma comment(linker, "/alternatename:?WideCharToMultiByte@win32@system_error2@@YAHIKPB_WHPADHPBDPAH@Z=WideCharToMultiByte")
0080 #else
0081 #error Unknown architecture
0082 #endif
0083 #endif
0084 }  // namespace win32
0085 
0086 class _win32_code_domain;
0087 class _com_code_domain;
0088 //! (Windows only) A Win32 error code, those returned by `GetLastError()`.
0089 using win32_code = status_code<_win32_code_domain>;
0090 //! (Windows only) A specialisation of `status_error` for the Win32 error code domain.
0091 using win32_error = status_error<_win32_code_domain>;
0092 
0093 namespace mixins
0094 {
0095   template <class Base> struct mixin<Base, _win32_code_domain> : public Base
0096   {
0097     using Base::Base;
0098 
0099     //! Returns a `win32_code` for the current value of `GetLastError()`.
0100     static inline win32_code current() noexcept;
0101   };
0102 }  // namespace mixins
0103 
0104 /*! (Windows only) The implementation of the domain for Win32 error codes, those returned by `GetLastError()`.
0105  */
0106 class _win32_code_domain : public status_code_domain
0107 {
0108   template <class DomainType> friend class status_code;
0109   friend class _com_code_domain;
0110   using _base = status_code_domain;
0111   static int _win32_code_to_errno(win32::DWORD c)
0112   {
0113     switch(c)
0114     {
0115     case 0:
0116       return 0;
0117 #include "detail/win32_code_to_generic_code.ipp"
0118     }
0119     return -1;
0120   }
0121   //! Construct from a Win32 error code
0122   static _base::string_ref _make_string_ref(win32::DWORD c) noexcept
0123   {
0124     wchar_t buffer[32768];
0125     win32::DWORD wlen =
0126     win32::FormatMessageW(0x00001000 /*FORMAT_MESSAGE_FROM_SYSTEM*/ | 0x00000200 /*FORMAT_MESSAGE_IGNORE_INSERTS*/, nullptr, c, 0, buffer, 32768, nullptr);
0127     size_t allocation = wlen + (wlen >> 1);
0128     win32::DWORD bytes;
0129     if(wlen == 0)
0130     {
0131       return _base::string_ref("failed to get message from system");
0132     }
0133     for(;;)
0134     {
0135       auto *p = static_cast<char *>(malloc(allocation));  // NOLINT
0136       if(p == nullptr)
0137       {
0138         return _base::string_ref("failed to get message from system");
0139       }
0140       bytes = win32::WideCharToMultiByte(65001 /*CP_UTF8*/, 0, buffer, (int) (wlen + 1), p, (int) allocation, nullptr, nullptr);
0141       if(bytes != 0)
0142       {
0143         char *end = strchr(p, 0);
0144         while(end[-1] == 10 || end[-1] == 13)
0145         {
0146           --end;
0147         }
0148         *end = 0;  // NOLINT
0149         return _base::atomic_refcounted_string_ref(p, end - p);
0150       }
0151       free(p);  // NOLINT
0152       if(win32::GetLastError() == 0x7a /*ERROR_INSUFFICIENT_BUFFER*/)
0153       {
0154         allocation += allocation >> 2;
0155         continue;
0156       }
0157       return _base::string_ref("failed to get message from system");
0158     }
0159   }
0160 
0161 public:
0162   //! The value type of the win32 code, which is a `win32::DWORD`
0163   using value_type = win32::DWORD;
0164   using _base::string_ref;
0165 
0166 public:
0167   //! Default constructor
0168   constexpr explicit _win32_code_domain(typename _base::unique_id_type id = 0x8cd18ee72d680f1b) noexcept
0169       : _base(id)
0170   {
0171   }
0172   _win32_code_domain(const _win32_code_domain &) = default;
0173   _win32_code_domain(_win32_code_domain &&) = default;
0174   _win32_code_domain &operator=(const _win32_code_domain &) = default;
0175   _win32_code_domain &operator=(_win32_code_domain &&) = default;
0176   ~_win32_code_domain() = default;
0177 
0178   //! Constexpr singleton getter. Returns the constexpr win32_code_domain variable.
0179   static inline constexpr const _win32_code_domain &get();
0180 
0181   virtual string_ref name() const noexcept override { return string_ref("win32 domain"); }  // NOLINT
0182 
0183   virtual payload_info_t payload_info() const noexcept override
0184   {
0185     return {sizeof(value_type), sizeof(status_code_domain *) + sizeof(value_type),
0186             (alignof(value_type) > alignof(status_code_domain *)) ? alignof(value_type) : alignof(status_code_domain *)};
0187   }
0188 
0189 protected:
0190   virtual bool _do_failure(const status_code<void> &code) const noexcept override  // NOLINT
0191   {
0192     assert(code.domain() == *this);
0193     return static_cast<const win32_code &>(code).value() != 0;  // NOLINT
0194   }
0195   virtual bool _do_equivalent(const status_code<void> &code1, const status_code<void> &code2) const noexcept override  // NOLINT
0196   {
0197     assert(code1.domain() == *this);
0198     const auto &c1 = static_cast<const win32_code &>(code1);  // NOLINT
0199     if(code2.domain() == *this)
0200     {
0201       const auto &c2 = static_cast<const win32_code &>(code2);  // NOLINT
0202       return c1.value() == c2.value();
0203     }
0204     if(code2.domain() == generic_code_domain)
0205     {
0206       const auto &c2 = static_cast<const generic_code &>(code2);  // NOLINT
0207       if(static_cast<int>(c2.value()) == _win32_code_to_errno(c1.value()))
0208       {
0209         return true;
0210       }
0211     }
0212     return false;
0213   }
0214   virtual generic_code _generic_code(const status_code<void> &code) const noexcept override  // NOLINT
0215   {
0216     assert(code.domain() == *this);
0217     const auto &c = static_cast<const win32_code &>(code);  // NOLINT
0218     return generic_code(static_cast<errc>(_win32_code_to_errno(c.value())));
0219   }
0220   virtual string_ref _do_message(const status_code<void> &code) const noexcept override  // NOLINT
0221   {
0222     assert(code.domain() == *this);
0223     const auto &c = static_cast<const win32_code &>(code);  // NOLINT
0224     return _make_string_ref(c.value());
0225   }
0226 #if defined(_CPPUNWIND) || defined(__EXCEPTIONS) || defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE)
0227   BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN virtual void _do_throw_exception(const status_code<void> &code) const override  // NOLINT
0228   {
0229     assert(code.domain() == *this);
0230     const auto &c = static_cast<const win32_code &>(code);  // NOLINT
0231     throw status_error<_win32_code_domain>(c);
0232   }
0233 #endif
0234 };
0235 //! (Windows only) A constexpr source variable for the win32 code domain, which is that of `GetLastError()` (Windows). Returned by
0236 //! `_win32_code_domain::get()`.
0237 constexpr _win32_code_domain win32_code_domain;
0238 inline constexpr const _win32_code_domain &_win32_code_domain::get()
0239 {
0240   return win32_code_domain;
0241 }
0242 
0243 namespace mixins
0244 {
0245   template <class Base> inline win32_code mixin<Base, _win32_code_domain>::current() noexcept
0246   {
0247     return win32_code(win32::GetLastError());
0248   }
0249 }  // namespace mixins
0250 
0251 BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END
0252 
0253 #if defined(_MSC_VER) && !defined(__clang__)
0254 #pragma warning(pop)
0255 #endif
0256 
0257 #endif