#include #include #include "wcharptr.h" #include "win.h" WcharPtr::WcharPtr() noexcept {} WcharPtr::WcharPtr(wchar_t* const s) noexcept : m_p(s) {} WcharPtr& WcharPtr::operator=(wchar_t* const s) noexcept { if (m_p != s) { delete m_p; m_p = s; } return *this; } WcharPtr::WcharPtr(WcharPtr&& other) noexcept : m_p(std::exchange(other.m_p, nullptr)) {} WcharPtr& WcharPtr::operator=(WcharPtr&& other) noexcept { std::swap(m_p, other.m_p); return *this; } WcharPtr::operator wchar_t*() noexcept { return m_p; } wchar_t* WcharPtr::Release() noexcept { wchar_t* p2 = m_p; m_p = nullptr; return p2; } WcharPtr::~WcharPtr() noexcept { delete m_p; } WcharPtr WcharPtr::FromNarrow(const char* const src, const int cp) { int cbMultiByte = strlen(src)+1; int cchWideChar = MultiByteToWideChar(cp, 0, src, cbMultiByte, nullptr, 0); wchar_t* dst = new wchar_t[cchWideChar]; if (!MultiByteToWideChar(cp, 0, src, cbMultiByte, dst, cchWideChar)) { delete dst; throw Win32Error(); } return dst; } WcharPtr WcharPtr::Copy(const wchar_t* const src) { const int cb = wcslen(src)+1; wchar_t* dst = new wchar_t[cb]; memcpy(dst, src, cb*sizeof(wchar_t)); return dst; }