From ebabd34385feb629b759216c1f7d85edc20bf2fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Sat, 30 Jul 2022 03:14:13 +0200 Subject: Add wstring_owner, replacing std::wstring. std::basic_string is nice, but it is not very ergonomic if everything you really need is to automatically free C strings at end of scope. I suppose I could have used std::unique_ptr for this, but I suspect the ergonomics would be worse. --- c/common.h | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'c/common.h') diff --git a/c/common.h b/c/common.h index 8b88635..7b7e996 100644 --- a/c/common.h +++ b/c/common.h @@ -11,7 +11,22 @@ #define WA "A" #endif -std::wstring WsFromSz(const char* sz, int iCp = CP_UTF8); +struct wstring_owner +{ + wstring_owner(); + wstring_owner(wchar_t* wsz); + wstring_owner& operator=(wchar_t* wsz); + wstring_owner(wstring_owner& wso) = delete; + wstring_owner& operator=(wstring_owner& wso) = delete; + wstring_owner(wstring_owner&& wso) noexcept; + wstring_owner& operator=(wstring_owner&& wso) noexcept; + wchar_t *release(); + operator bool() const; + ~wstring_owner(); + wchar_t* p = nullptr; +}; + +wstring_owner WsoFromSz(const char* sz, int iCp = CP_UTF8); int EBMessageBox(const wchar_t* wszText, const wchar_t* wszCaption, unsigned uType); struct Win32Error : public std::exception -- cgit v1.2.3