HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
hresult_error_impl.hpp
1// Copyright Take Vos 2024.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "../macros.hpp"
8#include "hresult_error_intf.hpp"
9#include "winbase.hpp"
10#include <format>
11#include <string>
12
13hi_export_module(hikogui.win32 : win32_error_impl);
14
15hi_export namespace hi {
16inline namespace v1 {
17
18[[nodiscard]] inline std::string hresult_error_category::message(int code) const
19{
20 auto const code_ = static_cast<uint32_t>(code);
21
22 auto const value = code & 0xffff;
23 auto const facility = (code >> 16) & 0x7ff;
24 auto const is_message_id = ((code >> 27) & 1) != 0;
25 auto const is_ntstatus = ((code >> 28) & 1) != 0;
26 auto const is_custom = ((code >> 29) & 1) != 0;
27 auto const is_severe_failure = ((code >> 30) & 1) != 0;
28 auto const is_failure = ((code >> 31) & 1) != 0;
29
30 if (not is_ntstatus and not is_custom and not is_message_id and facility == 7) {
31 if (auto msg = win32_FormatMessage(static_cast<win32_error>(value))) {
32 return *msg;
33
34 } else {
35 throw std::system_error(msg.error());
36 }
37 }
38
39 auto const facility_str = [&] {
40 if (is_ntstatus) {
41 return std::string{"NTSTATUS"};
42 } else if (is_custom) {
43 return std::string{"Custom"};
44 } else if (is_message_id) {
45 return std::string{"Message ID"};
46 } else {
47 switch (facility) {
48 case 1:
49 return std::string{"RPC"};
50 case 2:
51 return std::string{"COM Dispatch"};
52 case 3:
53 return std::string{"OLE Storage"};
54 case 4:
55 return std::string{"COM/OLE Interface Management"};
56 case 7:
57 return std::string{"Win32"};
58 case 8:
59 return std::string{"Windows"};
60 case 9:
61 return std::string{"SSPI"};
62 case 10:
63 return std::string{"Control"};
64 case 11:
65 return std::string{"Client or Server Certificate"};
66 default:
67 return std::format("Unknown Facility {}", facility);
68 }
69 }
70 }();
71
72 auto const failure_str = [&] {
73 if (is_failure) {
74 if (is_severe_failure) {
75 return "FATAL";
76 } else {
77 return "ERROR";
78 }
79 } else {
80 return "SUCCESS";
81 }
82 }();
83
84 return std::format("HRESULT({}): {}: 0x{:08x}", facility_str, failure_str, code_);
85}
86
87} // namespace v1
88}
@ code
Text is a piece of programming-code; a variable name, a function name.
The HikoGUI namespace.
Definition array_generic.hpp:20
DOXYGEN BUG.
Definition algorithm_misc.hpp:20