LibreOffice Module onlineupdate (master) 1
Char16.h
Go to the documentation of this file.
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/. */
6
7/* Implements a UTF-16 character type. */
8
9#ifndef mozilla_Char16_h
10#define mozilla_Char16_h
11
12#ifdef __cplusplus
13
14/*
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".
18 */
19
20#ifdef _WIN32
21# define MOZ_USE_CHAR16_WRAPPER
22# include <cstdint>
34class char16ptr_t
35{
36private:
37 const char16_t* mPtr;
38 static_assert(sizeof(char16_t) == sizeof(wchar_t),
39 "char16_t and wchar_t sizes differ");
40
41public:
42 char16ptr_t(const char16_t* aPtr) : mPtr(aPtr) {}
43 char16ptr_t(const wchar_t* aPtr) :
44 mPtr(reinterpret_cast<const char16_t*>(aPtr))
45 {}
46
47 /* Without this, nullptr assignment would be ambiguous. */
48 constexpr char16ptr_t(decltype(nullptr)) : mPtr(nullptr) {}
49
50 operator const char16_t*() const
51 {
52 return mPtr;
53 }
54 operator const wchar_t*() const
55 {
56 return reinterpret_cast<const wchar_t*>(mPtr);
57 }
58 operator const void*() const
59 {
60 return mPtr;
61 }
62 operator bool() const
63 {
64 return mPtr != nullptr;
65 }
66
67 /* Explicit cast operators to allow things like (char16_t*)str. */
68 explicit operator char16_t*() const
69 {
70 return const_cast<char16_t*>(mPtr);
71 }
72 explicit operator wchar_t*() const
73 {
74 return const_cast<wchar_t*>(static_cast<const wchar_t*>(*this));
75 }
76 explicit operator int() const
77 {
78 return reinterpret_cast<intptr_t>(mPtr);
79 }
80 explicit operator unsigned int() const
81 {
82 return reinterpret_cast<uintptr_t>(mPtr);
83 }
84 explicit operator long() const
85 {
86 return reinterpret_cast<intptr_t>(mPtr);
87 }
88 explicit operator unsigned long() const
89 {
90 return reinterpret_cast<uintptr_t>(mPtr);
91 }
92 explicit operator long long() const
93 {
94 return reinterpret_cast<intptr_t>(mPtr);
95 }
96 explicit operator unsigned long long() const
97 {
98 return reinterpret_cast<uintptr_t>(mPtr);
99 }
100
106 explicit operator const char*() const
107 {
108 return reinterpret_cast<const char*>(mPtr);
109 }
110 explicit operator const unsigned char*() const
111 {
112 return reinterpret_cast<const unsigned char*>(mPtr);
113 }
114 explicit operator unsigned char*() const
115 {
116 return
117 const_cast<unsigned char*>(reinterpret_cast<const unsigned char*>(mPtr));
118 }
119 explicit operator void*() const
120 {
121 return const_cast<char16_t*>(mPtr);
122 }
123
124 /* Some operators used on pointers. */
125 char16_t operator[](size_t aIndex) const
126 {
127 return mPtr[aIndex];
128 }
129 bool operator==(const char16ptr_t& aOther) const
130 {
131 return mPtr == aOther.mPtr;
132 }
133 bool operator==(decltype(nullptr)) const
134 {
135 return mPtr == nullptr;
136 }
137 bool operator!=(const char16ptr_t& aOther) const
138 {
139 return mPtr != aOther.mPtr;
140 }
141 bool operator!=(decltype(nullptr)) const
142 {
143 return mPtr != nullptr;
144 }
145 char16ptr_t operator+(int aValue) const
146 {
147 return char16ptr_t(mPtr + aValue);
148 }
149 char16ptr_t operator+(unsigned int aValue) const
150 {
151 return char16ptr_t(mPtr + aValue);
152 }
153 char16ptr_t operator+(long aValue) const
154 {
155 return char16ptr_t(mPtr + aValue);
156 }
157 char16ptr_t operator+(unsigned long aValue) const
158 {
159 return char16ptr_t(mPtr + aValue);
160 }
161 char16ptr_t operator+(long long aValue) const
162 {
163 return char16ptr_t(mPtr + aValue);
164 }
165 char16ptr_t operator+(unsigned long long aValue) const
166 {
167 return char16ptr_t(mPtr + aValue);
168 }
169 ptrdiff_t operator-(const char16ptr_t& aOther) const
170 {
171 return mPtr - aOther.mPtr;
172 }
173};
174
175inline decltype((char*)0-(char*)0)
176operator-(const char16_t* aX, const char16ptr_t aY)
177{
178 return aX - static_cast<const char16_t*>(aY);
179}
180
181#else
182
183typedef const char16_t* char16ptr_t;
184
185#endif
186
187static_assert(sizeof(char16_t) == 2, "Is char16_t type 16 bits?");
188static_assert(char16_t(-1) > char16_t(0), "Is char16_t type unsigned?");
189static_assert(sizeof(u'A') == 2, "Is unicode char literal 16 bits?");
190static_assert(sizeof(u""[0]) == 2, "Is unicode string char 16 bits?");
191
192#endif
193
194#endif /* mozilla_Char16_h */
float u
std::deque< AttacherIndex_Impl > aIndex
std::enable_if< std::is_signed< T >::value, SwNodeOffset >::type operator-(SwNodeOffset a, T n)
std::enable_if< std::is_signed< T >::value, SwNodeOffset >::type operator+(SwNodeOffset a, T n)
const wchar_t *typedef int(__stdcall *DllNativeUnregProc)(int
bool operator!=(const XclExpString &rLeft, const XclExpString &rRight)
bool operator==(const XclFontData &rLeft, const XclFontData &rRight)