5#include "../macros.hpp"
13hi_export_module(hikogui.win32.errhandlingapi);
15namespace hi {
inline namespace v1 {
17hi_export
enum class win32_error : uint32_t {
18 success = ERROR_SUCCESS,
19 file_not_found = ERROR_FILE_NOT_FOUND,
20 more_data = ERROR_MORE_DATA,
21 invalid_data = ERROR_INVALID_DATA,
29namespace hi {
inline namespace v1 {
32 char const *name()
const noexcept override
41 switch (
static_cast<hi::win32_error
>(code)) {
42 case hi::win32_error::file_not_found:
43 return condition == std::errc::no_such_file_or_directory;
44 case hi::win32_error::more_data:
45 return condition == std::errc::message_size;
46 case hi::win32_error::invalid_data:
47 return condition == std::errc::bad_message;
56hi_export [[nodiscard]]
inline std::error_code make_error_code(win32_error code)
noexcept
58 return {
static_cast<int>(code), global_win32_error_category};
61hi_export [[nodiscard]]
inline win32_error win32_GetLastError() noexcept
63 return static_cast<win32_error
>(::GetLastError());
73hi_export [[nodiscard]]
inline std::expected<std::string, win32_error>
win32_WideCharToMultiByte(std::wstring_view s,
unsigned int code_page = CP_UTF8, uint32_t flags = 0) noexcept
80 auto s_len =
static_cast<int>(
static_cast<unsigned int>(s.size()));
81 auto r_len = ::WideCharToMultiByte(code_page, flags, s.data(), s_len,
nullptr, 0,
nullptr,
nullptr);
86 auto r =
std::string(
static_cast<size_t>(
static_cast<std::make_signed_t<size_t>
>(r_len)),
'\0');
87 r.resize_and_overwrite(r_len, [&](
char *p,
size_t count) {
88 return ::WideCharToMultiByte(code_page, flags, s.data(), s_len, p,
static_cast<int>(count),
nullptr,
nullptr);
105hi_export [[nodiscard]]
inline std::expected<std::wstring, win32_error>
win32_MultiByteToWideChar(std::string_view s,
unsigned int code_page = CP_UTF8, uint32_t flags = 0) noexcept
112 auto s_len =
static_cast<int>(
static_cast<unsigned int>(s.size()));
113 auto r_len = ::MultiByteToWideChar(code_page, flags, s.data(), s_len,
nullptr, 0);
119 r.resize_and_overwrite(r_len, [&](
wchar_t *p,
size_t count) {
120 return ::MultiByteToWideChar(code_page, flags, s.data(), s_len, p,
static_cast<int>(count));
139hi_export [[nodiscard]]
inline std::expected<std::vector<std::string>, win32_error>
win32_MultiSZToStringVector(
wchar_t const *first,
wchar_t const *last)
noexcept
143 while (first != last) {
144 auto it_zero =
std::find(first, last,
wchar_t{0});
145 if (it_zero == last) {
150 hilet ws = std::wstring_view{first,
static_cast<std::size_t>(it_zero - first)};
169hi_export [[nodiscard]]
inline std::expected<std::string, win32_error> win32_FormatMessage(win32_error error_code)
noexcept
171 hilet error_code_ =
static_cast<DWORD
>(std::to_underlying(error_code));
175 LPWSTR buffer =
nullptr;
176 hilet result = ::FormatMessageW(
177 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
180 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
181 reinterpret_cast<LPWSTR
>(&buffer),
194hi_export
inline std::string win32_error_category::message(
int code)
const
196 if (
auto msg = win32_FormatMessage(
static_cast<win32_error
>(code))) {
200 throw std::system_error(msg.error());
Rules for working with win32 headers.
DOXYGEN BUG.
Definition algorithm.hpp:16
geometry/margins.hpp
Definition lookahead_iterator.hpp:5
The HikoGUI API version 1.
Definition lookahead_iterator.hpp:6
hi_export std::expected< std::string, win32_error > win32_WideCharToMultiByte(std::wstring_view s, unsigned int code_page=CP_UTF8, uint32_t flags=0) noexcept
Convert a win32-API compatible std::wstring to a multi-byte std::string.
Definition base.hpp:73
hi_export std::expected< std::vector< std::string >, win32_error > win32_MultiSZToStringVector(wchar_t const *first, wchar_t const *last) noexcept
Convert a win32 zero terminated list of zero terminated strings.
Definition base.hpp:139
hi_export std::expected< std::wstring, win32_error > win32_MultiByteToWideChar(std::string_view s, unsigned int code_page=CP_UTF8, uint32_t flags=0) noexcept
Convert a win32-API compatible std::wstring to a multi-byte std::string.
Definition base.hpp:105