aboutsummaryrefslogtreecommitdiff
path: root/c/wcharptr.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'c/wcharptr.cpp')
-rw-r--r--c/wcharptr.cpp64
1 files changed, 64 insertions, 0 deletions
diff --git a/c/wcharptr.cpp b/c/wcharptr.cpp
new file mode 100644
index 0000000..e406ed1
--- /dev/null
+++ b/c/wcharptr.cpp
@@ -0,0 +1,64 @@
+#include <utility>
+#include <windows.h>
+
+#include "wcharptr.h"
+#include "win.h"
+
+wchar_ptr::wchar_ptr() noexcept {}
+
+wchar_ptr::wchar_ptr(wchar_t* const s) noexcept : m_p(s) {}
+
+wchar_ptr& wchar_ptr::operator=(wchar_t* const s) noexcept
+{
+ if (m_p != s) {
+ delete m_p;
+ m_p = s;
+ }
+ return *this;
+}
+
+wchar_ptr::wchar_ptr(wchar_ptr&& other) noexcept
+ : m_p(std::exchange(other.m_p, nullptr)) {}
+
+wchar_ptr& wchar_ptr::operator=(wchar_ptr&& other) noexcept
+{
+ std::swap(m_p, other.m_p);
+ return *this;
+}
+
+wchar_ptr::operator wchar_t*() noexcept
+{
+ return m_p;
+}
+
+wchar_t* wchar_ptr::release() noexcept
+{
+ wchar_t* p2 = m_p;
+ m_p = nullptr;
+ return p2;
+}
+
+wchar_ptr::~wchar_ptr() noexcept
+{
+ delete m_p;
+}
+
+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);
+ wchar_t* dst = new wchar_t[cchWideChar];
+ if (!MultiByteToWideChar(cp, 0, src, cbMultiByte, dst, cchWideChar)) {
+ delete dst;
+ throw Win32Error();
+ }
+ return dst;
+}
+
+wchar_ptr wchar_ptr::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;
+}