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#pragma once
11
12#include <sal/config.h>
13
14#include <string_view>
15
16namespace o3tl
17{
18#if defined _WIN32
19// Helpers for safe conversion between wchar_t and char16_t in MSVC
20
21static_assert(sizeof(char16_t) == sizeof(wchar_t),
22 "These helper functions are only applicable to implementations with 16-bit wchar_t");
23
24// While other implementations define wchar_t as 32-bit integral value, and mostly use
25// char-based UTF-8 string APIs, in MSVC wchar_t is (non-conformant) 16-bit, and Unicode
26// support is implemented by Unicode-aware WinAPI functions taking UTF-16 LE strings,
27// and also stdlib functions taking those strings.
28//
29// In LibreOffice, internal string representation is also UTF-16 with system endianness
30// (sal_Unicode that is typedef for char16_t); so it is an important implementation concept
31// to treat internal strings as directly usable by WinAPI/stdlib functions and vice versa.
32// Also, it's important to use safe conversion between unrelated underlying C++ types
33// used for MSVC and LibreOffice string storage without plain reinterpret_cast that brings
34// risks of masking errors like casting char buffers to wchar_t/char16_t.
35//
36// Use these helpers for wchar_t (WSTR, WCHAR, OLESTR etc) to char16_t (sal_Unicode) string
37// conversions instead of reinterpret-cast in Windows-specific code.
38inline wchar_t* toW(char16_t* p) { return reinterpret_cast<wchar_t*>(p); }
39inline wchar_t const* toW(char16_t const* p) { return reinterpret_cast<wchar_t const*>(p); }
40inline char16_t* toU(wchar_t* p) { return reinterpret_cast<char16_t*>(p); }
41inline char16_t const* toU(wchar_t const* p) { return reinterpret_cast<char16_t const*>(p); }
42
43inline std::u16string_view toU(std::wstring_view v) { return { toU(v.data()), v.size() }; }
44#endif
45}
46
47/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
float v
void * p