From 32bb4696396e48521614d00e5b8f6e6586822f31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Wed, 3 Aug 2022 20:51:23 +0200 Subject: Minor improvements. --- c/common.h | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) (limited to 'c/common.h') diff --git a/c/common.h b/c/common.h index 9d322b3..70c2c32 100644 --- a/c/common.h +++ b/c/common.h @@ -8,37 +8,48 @@ int EBMessageBox(const wchar_t* wszText, const wchar_t* wszCaption, unsigned uType); int GetRelativeCursorPos(HWND hWnd, POINT* pt); +/* wchar_ptr: Simple wrapper for wide C strings. */ struct wchar_ptr { + /* Named copy constructors (expensive). */ static wchar_ptr from_narrow(const char* buf, int cp = CP_UTF8); static wchar_ptr copy(const wchar_t* s); - wchar_ptr(); - wchar_ptr(wchar_t* s); - wchar_ptr& operator=(wchar_t* s); + + /* Non-explicit copies are disabled. */ wchar_ptr(wchar_ptr& other) = delete; wchar_ptr& operator=(wchar_ptr& other) = delete; + + wchar_ptr() noexcept; + ~wchar_ptr(); + operator wchar_t*() noexcept; + + wchar_ptr(wchar_t* s) noexcept; + wchar_ptr& operator=(wchar_t* s) noexcept; + wchar_ptr(wchar_ptr&& other) noexcept; wchar_ptr& operator=(wchar_ptr&& other) noexcept; - operator wchar_t*() noexcept; - wchar_t *release(); - ~wchar_ptr(); + + /* Return pointer, releasing ownership. */ + wchar_t *release() noexcept; private: wchar_t* m_p = nullptr; }; +/* Win32Error: Exception for Windows API errors. */ struct Win32Error : public std::exception { Win32Error(); Win32Error(DWORD code); ~Win32Error(); - const char* what() const noexcept; - const wchar_t* WhatW() const noexcept; + const char* what() const noexcept override; + const wchar_t* What() const noexcept; DWORD code; private: char* m_szMsg = NULL; wchar_t* m_wszMsg = NULL; }; +/* Library: Wrapper for loading and freeing dynamically linked libraries. */ struct Library { static std::optional Maybe(const wchar_t* lib); @@ -57,7 +68,7 @@ T* Library::GetProcAddress(const char* const szProc) } template -inline int wszf(wchar_t (&buf)[N], const wchar_t* const fmt, T... xs) +inline int Swprintf(wchar_t (&buf)[N], const wchar_t* const fmt, T... xs) { return _snwprintf_s(buf, N, _TRUNCATE, fmt, xs...); } @@ -79,7 +90,7 @@ template inline T prefer(const T x) { if (!x) { - EBMessageBox(Win32Error().WhatW(), L"System Error", MB_ICONWARNING); + EBMessageBox(Win32Error().What(), L"System Error", MB_ICONWARNING); return (T)NULL; } return x; -- cgit v1.2.3