From 23dc657f0af27bdac887f8d18208d544cc9f010e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Tue, 2 Aug 2022 23:50:58 +0200 Subject: Improve wstring_owner, rename to wchar_ptr. The user-defined conversion function makes the interface a lot simpler AND safer. --- c/common.cpp | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) (limited to 'c/common.cpp') diff --git a/c/common.cpp b/c/common.cpp index 5d786da..59f0fdc 100644 --- a/c/common.cpp +++ b/c/common.cpp @@ -3,48 +3,48 @@ #include "common.h" -/* wstring_owner: Simple wrapper for wide C strings. */ +/* wchar_ptr: Simple wrapper for wide C strings. */ -wstring_owner::wstring_owner() {} +wchar_ptr::wchar_ptr() {} -wstring_owner::wstring_owner(wchar_t* const s) : p(s) {} +wchar_ptr::wchar_ptr(wchar_t* const s) : m_p(s) {} -wstring_owner& wstring_owner::operator=(wchar_t* const s) +wchar_ptr& wchar_ptr::operator=(wchar_t* const s) { - if (p != s) { - delete p; - p = s; + if (m_p != s) { + delete m_p; + m_p = s; } return *this; } -wstring_owner::wstring_owner(wstring_owner&& other) noexcept : p(std::exchange(other.p, nullptr)) {} +wchar_ptr::wchar_ptr(wchar_ptr&& other) noexcept : m_p(std::exchange(other.m_p, nullptr)) {} -wstring_owner& wstring_owner::operator=(wstring_owner&& other) noexcept +wchar_ptr& wchar_ptr::operator=(wchar_ptr&& other) noexcept { - std::swap(p, other.p); + std::swap(m_p, other.m_p); return *this; } -/* Return pointer, releasing ownership. */ -wchar_t* wstring_owner::release() +wchar_ptr::operator wchar_t*() noexcept { - wchar_t* p2 = p; - p = nullptr; - return p2; + return m_p; } -wstring_owner::operator bool() const +/* Return pointer, releasing ownership. */ +wchar_t* wchar_ptr::release() { - return p; + wchar_t* p2 = m_p; + m_p = nullptr; + return p2; } -wstring_owner::~wstring_owner() +wchar_ptr::~wchar_ptr() { - delete p; + delete m_p; } -wstring_owner wstring_owner::from_narrow(const char* const src, const int cp) +wchar_ptr wchar_ptr::from_narrow(const char* const src, const int cp) { int cbMultiByte = strlen(src)+1; int cchWideChar = MultiByteToWideChar(cp, 0, src, cbMultiByte, NULL, 0); @@ -56,7 +56,7 @@ wstring_owner wstring_owner::from_narrow(const char* const src, const int cp) return dst; } -wstring_owner wstring_owner::copy(const wchar_t* const src) +wchar_ptr wchar_ptr::copy(const wchar_t* const src) { const int cb = wcslen(src)+1; wchar_t* dst = new wchar_t[cb]; -- cgit v1.2.3