From ba0750ff48bcf97ef99602fe27322fa706a93b6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Mon, 22 Aug 2022 01:05:05 +0200 Subject: Add Buf class. --- c/util.h | 45 +++++++++++++++++++++++++++++---------------- c/win.cpp | 45 +++++++++++++++++++++++---------------------- c/win.h | 4 ++-- 3 files changed, 54 insertions(+), 40 deletions(-) (limited to 'c') diff --git a/c/util.h b/c/util.h index 1d8d222..698ce10 100644 --- a/c/util.h +++ b/c/util.h @@ -4,42 +4,51 @@ #include #include #include +#include #include #include -#define CAT(a, b) a##b -#define APPLY(a, ...) a(__VA_ARGS__) -#define UNUSED APPLY(CAT, unused_, __COUNTER__) +/* Buf is a span-like structure of a buffer and its size. */ +template +struct Buf +{ + T* data; + size_t size; + Buf(T* data, size_t size) : data(data), size(size) {} + Buf(std::basic_string& s) : data(s.data()), size(s.capacity()) {} + template Buf(T (&data)[N]) : data(data), size(N) {} + operator T*() { return data; } + T& operator *() { return *data; } + T& operator [](size_t i) { return data[i]; } +}; -/* Format static wide string. */ -template -inline int Swprintf(wchar_t (&buf)[N], const wchar_t* const fmt, T... xs) +/* Format wide string. */ +template +inline int Swprintf(Buf buf, const wchar_t* const fmt, T... xs) { - return _snwprintf_s(buf, N, _TRUNCATE, fmt, xs...); + return _snwprintf_s(buf, buf.size, _TRUNCATE, fmt, xs...); } /* Format static narrow string. */ -template -inline int Sprintf(char (&buf)[N], const char* const fmt, T... xs) +template +inline int Sprintf(Buf buf, const char* const fmt, T... xs) { - return _snprintf_s(buf, N, _TRUNCATE, fmt, xs...); + return _snprintf_s(buf, buf.size, _TRUNCATE, fmt, xs...); } /* Copy to static wide string buffer. */ -template -inline wchar_t* Wcscpy(wchar_t (&dst)[N], const wchar_t* const src) +inline wchar_t* Wcscpy(Buf dst, const wchar_t* const src) { - const size_t len = std::min(N, wcslen(src)+1); + const size_t len = std::min(dst.size, wcslen(src)+1); memcpy(dst, src, len*sizeof(wchar_t)); dst[len-1] = 0; return dst; } /* Copy to static narrow string buffer. */ -template -inline char* Strcpy(char (&dst)[N], const char* const src) +inline char* Strcpy(Buf dst, const char* const src) { - const size_t len = std::min(N, strlen(src)+1); + const size_t len = std::min(dst.size, strlen(src)+1); memcpy(dst, src, len); dst[len-1] = 0; return dst; @@ -52,4 +61,8 @@ inline int Cmp(const int a, const int b) return -1; } +#define CAT(a, b) a##b +#define APPLY(a, ...) a(__VA_ARGS__) +#define UNUSED APPLY(CAT, unused_, __COUNTER__) + #endif diff --git a/c/win.cpp b/c/win.cpp index ea47fb0..c7ce7ec 100644 --- a/c/win.cpp +++ b/c/win.cpp @@ -1,4 +1,5 @@ #include +#include #include #include @@ -7,28 +8,6 @@ #include "win.h" #include "wcharptr.h" -void ShowException(const wchar_t* const fmt, const wchar_t* const title, const UINT uType) noexcept -{ - const wchar_t* what = L"an exception"; - WcharPtr why; - - try { - std::rethrow_exception(std::current_exception()); - } catch (const term_t& t) { - what = L"a Prolog exception"; - try { why = PlString(t); } catch (...) {} - } catch (const Win32Error& e) { - what = L"a Windows error"; - try { why = WcharPtr::Copy(e.What()); } catch (...) {} - } catch (const std::exception& e) { - try { why = WcharPtr::FromNarrow(e.what()); } catch (...) {} - } catch (...) {} - - wchar_t msg[512]; - Swprintf(msg, fmt, what, static_cast(why)); - EBMessageBox(msg, title, uType); -} - void WithNextWindow(void (*proc)(HWND)) { /* WithNextWindow uses a CBT hook to call an arbitrary @@ -103,6 +82,28 @@ int EBMessageBox(const std::wstring_view text, const std::wstring_view caption, return MessageBox(g_hWnd, text.data(), caption.data(), uType); } +void ShowException(const wchar_t* const fmt, const wchar_t* const title, const UINT uType) noexcept +{ + const wchar_t* what = L"an exception"; + WcharPtr why; + + try { + std::rethrow_exception(std::current_exception()); + } catch (const term_t& t) { + what = L"a Prolog exception"; + try { why = PlString(t); } catch (...) {} + } catch (const Win32Error& e) { + what = L"a Windows error"; + try { why = WcharPtr::Copy(e.What()); } catch (...) {} + } catch (const std::exception& e) { + try { why = WcharPtr::FromNarrow(e.what()); } catch (...) {} + } catch (...) {} + + std::wstring msg(wcslen(fmt)+wcslen(what)+wcslen(why), 0); + Swprintf(msg, fmt, what, static_cast(why)); + EBMessageBox(msg, title, uType); +} + int GetRelativeCursorPos(const HWND hWnd, POINT* const pt) noexcept { RECT rc; diff --git a/c/win.h b/c/win.h index 3bb0d6e..ab5cf05 100644 --- a/c/win.h +++ b/c/win.h @@ -5,12 +5,12 @@ #include #include -/* Show message box for current exception. */ -void ShowException(const wchar_t* fmt, const wchar_t* title, UINT uType = MB_ICONWARNING) noexcept; /* Run given procedure at creation of next window. */ void WithNextWindow(void (*proc)(HWND)); /* Display message box centered in main window. */ int EBMessageBox(std::wstring_view text, std::wstring_view data, UINT uType); +/* Show message box for current exception. */ +void ShowException(const wchar_t* fmt, const wchar_t* title, UINT uType = MB_ICONWARNING) noexcept; /* Retrieve mouse position relative to given window's client area. */ int GetRelativeCursorPos(HWND hWnd, POINT* pt) noexcept; /* Cached values from GetSystemMetrics. */ -- cgit v1.2.3