Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / onlineupdate / source / update / inc / mozilla / Char16.h
blobf07494f7f47f785837ff19781784198b25c93e76
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /* Implements a UTF-16 character type. */
9 #ifndef mozilla_Char16_h
10 #define mozilla_Char16_h
12 #ifdef __cplusplus
15 * C++11 introduces a char16_t type and support for UTF-16 string and character
16 * literals. C++11's char16_t is a distinct builtin type. Technically, char16_t
17 * is a 16-bit code unit of a Unicode code point, not a "character".
20 #if defined(_MSC_VER) && _MSC_VER < 1900
22 * C++11 says char16_t is a distinct builtin type, but Windows's yvals.h
23 * typedefs char16_t as an unsigned short prior to MSVC 2015, which
24 * implemented C++11's distinct char16_t type. We would like to alias
25 * char16_t to Windows's 16-bit wchar_t so we can declare UTF-16 literals as
26 * constant expressions (and pass char16_t pointers to Windows APIs). We
27 * #define _CHAR16T here in order to prevent yvals.h from overriding our
28 * char16_t typedefs, which we set to wchar_t for C++ code.
30 * In addition, #defining _CHAR16T will prevent yvals.h from defining a
31 * char32_t type, so we have to undo that damage here and provide our own,
32 * which is identical to the yvals.h type.
34 # define MOZ_UTF16_HELPER(s) L##s
35 # define _CHAR16T
36 typedef wchar_t char16_t;
37 typedef unsigned int char32_t;
38 #else
39 /* C++11 has a builtin char16_t type. */
40 # define MOZ_UTF16_HELPER(s) u##s
41 /**
42 * This macro is used to distinguish when char16_t would be a distinct
43 * typedef from wchar_t.
45 # define MOZ_CHAR16_IS_NOT_WCHAR
46 # ifdef WIN32
47 # define MOZ_USE_CHAR16_WRAPPER
48 # endif
49 #endif
51 #ifdef MOZ_USE_CHAR16_WRAPPER
52 # include <string>
53 /**
54 * Win32 API extensively uses wchar_t, which is represented by a separated
55 * builtin type than char16_t per spec. It's not the case for MSVC prior to
56 * MSVC 2015, but other compilers follow the spec. We want to mix wchar_t and
57 * char16_t on Windows builds. This class is supposed to make it easier. It
58 * stores char16_t const pointer, but provides implicit casts for wchar_t as
59 * well. On other platforms, we simply use
60 * |typedef const char16_t* char16ptr_t|. Here, we want to make the class as
61 * similar to this typedef, including providing some casts that are allowed
62 * by the typedef.
64 class char16ptr_t
66 private:
67 const char16_t* mPtr;
68 static_assert(sizeof(char16_t) == sizeof(wchar_t),
69 "char16_t and wchar_t sizes differ");
71 public:
72 char16ptr_t(const char16_t* aPtr) : mPtr(aPtr) {}
73 char16ptr_t(const wchar_t* aPtr) :
74 mPtr(reinterpret_cast<const char16_t*>(aPtr))
77 /* Without this, nullptr assignment would be ambiguous. */
78 constexpr char16ptr_t(decltype(nullptr)) : mPtr(nullptr) {}
80 operator const char16_t*() const
82 return mPtr;
84 operator const wchar_t*() const
86 return reinterpret_cast<const wchar_t*>(mPtr);
88 operator const void*() const
90 return mPtr;
92 operator bool() const
94 return mPtr != nullptr;
96 operator std::wstring() const
98 return std::wstring(static_cast<const wchar_t*>(*this));
101 /* Explicit cast operators to allow things like (char16_t*)str. */
102 explicit operator char16_t*() const
104 return const_cast<char16_t*>(mPtr);
106 explicit operator wchar_t*() const
108 return const_cast<wchar_t*>(static_cast<const wchar_t*>(*this));
110 explicit operator int() const
112 return reinterpret_cast<intptr_t>(mPtr);
114 explicit operator unsigned int() const
116 return reinterpret_cast<uintptr_t>(mPtr);
118 explicit operator long() const
120 return reinterpret_cast<intptr_t>(mPtr);
122 explicit operator unsigned long() const
124 return reinterpret_cast<uintptr_t>(mPtr);
126 explicit operator long long() const
128 return reinterpret_cast<intptr_t>(mPtr);
130 explicit operator unsigned long long() const
132 return reinterpret_cast<uintptr_t>(mPtr);
136 * Some Windows API calls accept BYTE* but require that data actually be
137 * WCHAR*. Supporting this requires explicit operators to support the
138 * requisite explicit casts.
140 explicit operator const char*() const
142 return reinterpret_cast<const char*>(mPtr);
144 explicit operator const unsigned char*() const
146 return reinterpret_cast<const unsigned char*>(mPtr);
148 explicit operator unsigned char*() const
150 return
151 const_cast<unsigned char*>(reinterpret_cast<const unsigned char*>(mPtr));
153 explicit operator void*() const
155 return const_cast<char16_t*>(mPtr);
158 /* Some operators used on pointers. */
159 char16_t operator[](size_t aIndex) const
161 return mPtr[aIndex];
163 bool operator==(const char16ptr_t& aOther) const
165 return mPtr == aOther.mPtr;
167 bool operator==(decltype(nullptr)) const
169 return mPtr == nullptr;
171 bool operator!=(const char16ptr_t& aOther) const
173 return mPtr != aOther.mPtr;
175 bool operator!=(decltype(nullptr)) const
177 return mPtr != nullptr;
179 char16ptr_t operator+(int aValue) const
181 return char16ptr_t(mPtr + aValue);
183 char16ptr_t operator+(unsigned int aValue) const
185 return char16ptr_t(mPtr + aValue);
187 char16ptr_t operator+(long aValue) const
189 return char16ptr_t(mPtr + aValue);
191 char16ptr_t operator+(unsigned long aValue) const
193 return char16ptr_t(mPtr + aValue);
195 char16ptr_t operator+(long long aValue) const
197 return char16ptr_t(mPtr + aValue);
199 char16ptr_t operator+(unsigned long long aValue) const
201 return char16ptr_t(mPtr + aValue);
203 ptrdiff_t operator-(const char16ptr_t& aOther) const
205 return mPtr - aOther.mPtr;
209 inline decltype((char*)0-(char*)0)
210 operator-(const char16_t* aX, const char16ptr_t aY)
212 return aX - static_cast<const char16_t*>(aY);
215 #else
217 typedef const char16_t* char16ptr_t;
219 #endif
222 * Macro arguments used in concatenation or stringification won't be expanded.
223 * Therefore, in order for |MOZ_UTF16(FOO)| to work as expected (which is to
224 * expand |FOO| before doing whatever |MOZ_UTF16| needs to do to it) a helper
225 * macro, |MOZ_UTF16_HELPER| needs to be inserted in between to allow the macro
226 * argument to expand. See "3.10.6 Separate Expansion of Macro Arguments" of the
227 * CPP manual for a more accurate and precise explanation.
229 #define MOZ_UTF16(s) MOZ_UTF16_HELPER(s)
231 static_assert(sizeof(char16_t) == 2, "Is char16_t type 16 bits?");
232 static_assert(char16_t(-1) > char16_t(0), "Is char16_t type unsigned?");
233 static_assert(sizeof(MOZ_UTF16('A')) == 2, "Is char literal 16 bits?");
234 static_assert(sizeof(MOZ_UTF16("")[0]) == 2, "Is string char 16 bits?");
236 #endif
238 #endif /* mozilla_Char16_h */