summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorMike Kaganski <mike.kaganski@collabora.com>2017-10-05 06:19:56 +0300
committerMike Kaganski <mike.kaganski@collabora.com>2017-10-05 16:02:52 +0200
commit1944e3ddc0b2247de3138d2a441cd6999e21fd9a (patch)
treeb59f213e245e151ee792ca424fd06b5a11c88857 /include
parent81d404803f477eb71b74eb9c7a67bba6b1af95d1 (diff)
Rename and move SAL_U/W to o3tl::toU/W
Previosly (since commit 9ac98e6e3488e434bf4864ecfb13a121784f640b) it was expected to gradually remove SAL_U/W usage in Windows code by replacing with reinterpret_cast or changing to some bettertypes. But as it's useful to make use of fact that LibreOffice and Windows use compatible representation of strings, this commit puts these functions to a better-suited o3tl, and recommends that the functions be consistently used throughout Windows-specific code to reflect the compatibility and keep the casts safe. Change-Id: I2f7c65606d0e2d0c01a00f08812bb4ab7659c5f6 Reviewed-on: https://gerrit.libreoffice.org/43150 Tested-by: Jenkins <ci@libreoffice.org> Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
Diffstat (limited to 'include')
-rw-r--r--include/comphelper/windowserrorstring.hxx3
-rw-r--r--include/o3tl/char16_t2wchar_t.hxx59
-rw-r--r--include/sal/types.h15
3 files changed, 61 insertions, 16 deletions
diff --git a/include/comphelper/windowserrorstring.hxx b/include/comphelper/windowserrorstring.hxx
index 59f27711a5f8..b8cac3f8dbe9 100644
--- a/include/comphelper/windowserrorstring.hxx
+++ b/include/comphelper/windowserrorstring.hxx
@@ -13,6 +13,7 @@
#include <prewin.h>
#include <postwin.h>
#include <rtl/ustring.hxx>
+#include <o3tl/char16_t2wchar_t.hxx>
namespace {
@@ -29,7 +30,7 @@ inline OUString WindowsErrorString(DWORD nErrorCode)
nullptr) == 0)
return OUString::number(nErrorCode, 16);
- OUString result(SAL_U(pMsgBuf));
+ OUString result(o3tl::toU(pMsgBuf));
result.endsWith("\r\n", &result);
LocalFree(pMsgBuf);
diff --git a/include/o3tl/char16_t2wchar_t.hxx b/include/o3tl/char16_t2wchar_t.hxx
new file mode 100644
index 000000000000..6ffab02e78c9
--- /dev/null
+++ b/include/o3tl/char16_t2wchar_t.hxx
@@ -0,0 +1,59 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#ifndef INCLUDED_O3TL_CHAR16_T2WCHAR_T_HXX
+#define INCLUDED_O3TL_CHAR16_T2WCHAR_T_HXX
+
+#include <sal/config.h>
+
+namespace o3tl {
+
+#if defined _WIN32
+// Helpers for safe conversion between wchar_t and char16_t in MSVC
+
+static_assert(sizeof(char16_t) == sizeof(wchar_t),
+ "These helper functions are only applicable to implementations with 16-bit wchar_t");
+
+// While other implementations define wchar_t as 32-bit integral value, and mostly use
+// char-based UTF-8 string APIs, in MSVC wchar_t is (non-conformant) 16-bit, and Unicode
+// support is implemented by Unicode-aware WinAPI functions taking UTF-16 LE strings,
+// and also stdlib functions taking those strings.
+//
+// In LibreOffice, internal string representation is also UTF-16 with system endianness
+// (sal_Unicode that is typedef for char16_t); so it is an important implementation concept
+// to treat internal strings as directly usable by WinAPI/stdlib functions and vice versa.
+// Also, it's important to use safe conversion between unrelated underlying C++ types
+// used for MSVC and LibreOffice string storage without plain reinterpret_cast that brings
+// risks of masking errors like casting char buffers to wchar_t/char16_t.
+//
+// Use these helpers for wchar_t (WSTR, WCHAR, OLESTR etc) to char16_t (sal_Unicode) string
+// conversions instead of reinterpret-cast in Windows-specific code.
+inline wchar_t * toW(char16_t * p)
+{
+ return reinterpret_cast<wchar_t *>(p);
+}
+inline wchar_t const * toW(char16_t const * p)
+{
+ return reinterpret_cast<wchar_t const *>(p);
+}
+inline char16_t * toU(wchar_t * p)
+{
+ return reinterpret_cast<char16_t *>(p);
+}
+inline char16_t const * toU(wchar_t const * p)
+{
+ return reinterpret_cast<char16_t const *>(p);
+}
+#endif
+
+}
+
+#endif
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
diff --git a/include/sal/types.h b/include/sal/types.h
index a138a90858df..14c8e90d293d 100644
--- a/include/sal/types.h
+++ b/include/sal/types.h
@@ -682,21 +682,6 @@ template< typename T1, typename T2 > inline T1 static_int_cast(T2 n) {
/// @endcond
-#if defined LIBO_INTERNAL_ONLY && defined __cplusplus && defined SAL_W32
-/// @cond INTERNAL
-// Temporary scaffolding for the MSVC sal_Unicode wchar_t -> char16_t change; to
-// be removed again:
-inline wchar_t * SAL_W(char16_t * p)
-{ return reinterpret_cast<wchar_t *>(p); }
-inline wchar_t const * SAL_W(char16_t const * p)
-{ return reinterpret_cast<wchar_t const *>(p); }
-inline char16_t * SAL_U(wchar_t * p)
-{ return reinterpret_cast<char16_t *>(p); }
-inline char16_t const * SAL_U(wchar_t const * p)
-{ return reinterpret_cast<char16_t const *>(p); }
-/// @endcond
-#endif
-
/// @cond INTERNAL
/** Annotate pointer-returning functions to indicate that such a pointer
is never nullptr.