diff options
Diffstat (limited to 'c/common.h')
-rw-r--r-- | c/common.h | 31 |
1 files changed, 21 insertions, 10 deletions
@@ -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<Library> Maybe(const wchar_t* lib); @@ -57,7 +68,7 @@ T* Library::GetProcAddress(const char* const szProc) } template<size_t N, typename... T> -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 <typename T> 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; |