LibreOffice Module o3tl (master)  1
char16_t2wchar_t.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  */
9 
10 #ifndef INCLUDED_O3TL_CHAR16_T2WCHAR_T_HXX
11 #define INCLUDED_O3TL_CHAR16_T2WCHAR_T_HXX
12 
13 #include <sal/config.h>
14 
15 namespace o3tl {
16 
17 #if defined _WIN32
18 // Helpers for safe conversion between wchar_t and char16_t in MSVC
19 
20 static_assert(sizeof(char16_t) == sizeof(wchar_t),
21  "These helper functions are only applicable to implementations with 16-bit wchar_t");
22 
23 // While other implementations define wchar_t as 32-bit integral value, and mostly use
24 // char-based UTF-8 string APIs, in MSVC wchar_t is (non-conformant) 16-bit, and Unicode
25 // support is implemented by Unicode-aware WinAPI functions taking UTF-16 LE strings,
26 // and also stdlib functions taking those strings.
27 //
28 // In LibreOffice, internal string representation is also UTF-16 with system endianness
29 // (sal_Unicode that is typedef for char16_t); so it is an important implementation concept
30 // to treat internal strings as directly usable by WinAPI/stdlib functions and vice versa.
31 // Also, it's important to use safe conversion between unrelated underlying C++ types
32 // used for MSVC and LibreOffice string storage without plain reinterpret_cast that brings
33 // risks of masking errors like casting char buffers to wchar_t/char16_t.
34 //
35 // Use these helpers for wchar_t (WSTR, WCHAR, OLESTR etc) to char16_t (sal_Unicode) string
36 // conversions instead of reinterpret-cast in Windows-specific code.
37 inline wchar_t * toW(char16_t * p)
38 {
39  return reinterpret_cast<wchar_t *>(p);
40 }
41 inline wchar_t const * toW(char16_t const * p)
42 {
43  return reinterpret_cast<wchar_t const *>(p);
44 }
45 inline char16_t * toU(wchar_t * p)
46 {
47  return reinterpret_cast<char16_t *>(p);
48 }
49 inline char16_t const * toU(wchar_t const * p)
50 {
51  return reinterpret_cast<char16_t const *>(p);
52 }
53 #endif
54 
55 }
56 
57 #endif
58 
59 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */