8#include "../macros.hpp"
21hi_export_module(hikogui.win32.base);
23hi_export
namespace hi {
inline namespace v1 {
25enum class win32_error : uint32_t {
26 success = ERROR_SUCCESS,
27 file_not_found = ERROR_FILE_NOT_FOUND,
28 more_data = ERROR_MORE_DATA,
29 invalid_data = ERROR_INVALID_DATA,
30 insufficient_buffer = ERROR_INSUFFICIENT_BUFFER,
31 status_pending = STATUS_PENDING,
39namespace hi {
inline namespace v1 {
42 char const *name()
const noexcept override
51 switch (
static_cast<hi::win32_error
>(code)) {
52 case hi::win32_error::file_not_found:
53 return condition == std::errc::no_such_file_or_directory;
54 case hi::win32_error::more_data:
55 return condition == std::errc::message_size;
56 case hi::win32_error::invalid_data:
57 return condition == std::errc::bad_message;
58 case hi::win32_error::status_pending:
59 return condition == std::errc::interrupted;
60 case hi::win32_error::insufficient_buffer:
61 return condition == std::errc::no_buffer_space;
70[[nodiscard]] hi_inline
std::error_code make_error_code(win32_error code)
noexcept
72 return {
static_cast<int>(code), global_win32_error_category};
75[[nodiscard]] hi_inline win32_error win32_GetLastError() noexcept
77 return static_cast<win32_error
>(::GetLastError());
87[[nodiscard]] hi_inline std::expected<std::string, win32_error>
win32_WideCharToMultiByte(std::wstring_view s,
unsigned int code_page = CP_UTF8, uint32_t flags = 0) noexcept
94 auto s_len =
static_cast<int>(
static_cast<unsigned int>(s.size()));
95 auto r_len = ::WideCharToMultiByte(code_page, flags, s.data(), s_len,
nullptr, 0,
nullptr,
nullptr);
100 auto r =
std::string(
static_cast<size_t>(
static_cast<std::make_signed_t<size_t>
>(r_len)),
'\0');
101 r.resize_and_overwrite(r_len, [&](
char *p,
size_t count) {
102 return ::WideCharToMultiByte(code_page, flags, s.data(), s_len, p,
static_cast<int>(count),
nullptr,
nullptr);
119[[nodiscard]] hi_inline std::expected<std::wstring, win32_error>
win32_MultiByteToWideChar(std::string_view s,
unsigned int code_page = CP_UTF8, uint32_t flags = 0) noexcept
126 auto s_len =
static_cast<int>(
static_cast<unsigned int>(s.size()));
127 auto r_len = ::MultiByteToWideChar(code_page, flags, s.data(), s_len,
nullptr, 0);
133 r.resize_and_overwrite(r_len, [&](
wchar_t *p,
size_t count) {
134 return ::MultiByteToWideChar(code_page, flags, s.data(), s_len, p,
static_cast<int>(count));
153[[nodiscard]] hi_inline std::expected<std::vector<std::string>, win32_error>
win32_MultiSZToStringVector(
wchar_t const *first,
wchar_t const *last)
noexcept
157 while (first != last) {
158 auto it_zero =
std::find(first, last,
wchar_t{0});
159 if (it_zero == last) {
164 auto const ws = std::wstring_view{first,
static_cast<std::size_t>(it_zero - first)};
183[[nodiscard]] hi_inline std::expected<std::string, win32_error> win32_FormatMessage(win32_error error_code)
noexcept
185 auto const error_code_ =
static_cast<DWORD
>(std::to_underlying(error_code));
189 LPWSTR buffer =
nullptr;
190 auto const result = ::FormatMessageW(
191 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
194 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
195 reinterpret_cast<LPWSTR
>(&buffer),
208[[nodiscard]] hi_inline std::string win32_error_category::message(
int code)
const
210 if (
auto msg = win32_FormatMessage(
static_cast<win32_error
>(code))) {
214 throw std::system_error(msg.error());
228 auto i = std::bit_cast<uintptr_t>(handle);
232 return static_cast<uint32_t
>(i);
235[[nodiscard]] hi_inline HANDLE win32_int_to_HANDLE(uint32_t i)
noexcept
237 return std::bit_cast<HANDLE>(
static_cast<uintptr_t
>(i));
Rules for working with win32 headers.
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
The HikoGUI namespace.
Definition recursive_iterator.hpp:15
The HikoGUI API version 1.
Definition recursive_iterator.hpp:16
hi_inline 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:153
hi_inline 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:119
hi_inline uint32_t win32_HANDLE_to_int(HANDLE handle) noexcept
Convert a HANDLE to a 32-bit unsigned integer.
Definition base.hpp:226
hi_inline 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:87