|
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 _MSC_VER |
|
21 /* |
|
22 * C++11 says char16_t is a distinct builtin type, but Windows's yvals.h |
|
23 * typedefs char16_t as an unsigned short. We would like to alias char16_t |
|
24 * to Windows's 16-bit wchar_t so we can declare UTF-16 literals as constant |
|
25 * expressions (and pass char16_t pointers to Windows APIs). We #define |
|
26 * _CHAR16T here in order to prevent yvals.h from overriding our char16_t |
|
27 * typedefs, which we set to wchar_t for C++ code. |
|
28 * |
|
29 * In addition, #defining _CHAR16T will prevent yvals.h from defining a |
|
30 * char32_t type, so we have to undo that damage here and provide our own, |
|
31 * which is identical to the yvals.h type. |
|
32 */ |
|
33 # define MOZ_UTF16_HELPER(s) L##s |
|
34 # define _CHAR16T |
|
35 typedef wchar_t char16_t; |
|
36 typedef unsigned int char32_t; |
|
37 #else |
|
38 /* C++11 has a builtin char16_t type. */ |
|
39 # define MOZ_UTF16_HELPER(s) u##s |
|
40 /** |
|
41 * This macro is used to distinguish when char16_t would be a distinct |
|
42 * typedef from wchar_t. |
|
43 */ |
|
44 # define MOZ_CHAR16_IS_NOT_WCHAR |
|
45 # ifdef WIN32 |
|
46 # define MOZ_USE_CHAR16_WRAPPER |
|
47 # endif |
|
48 #endif |
|
49 |
|
50 #ifdef MOZ_USE_CHAR16_WRAPPER |
|
51 # include <string> |
|
52 /** |
|
53 * Win32 API extensively uses wchar_t, which is represented by a separated |
|
54 * builtin type than char16_t per spec. It's not the case for MSVC, but GCC |
|
55 * follows the spec. We want to mix wchar_t and char16_t on Windows builds. |
|
56 * This class is supposed to make it easier. It stores char16_t const pointer, |
|
57 * but provides implicit casts for wchar_t as well. On other platforms, we |
|
58 * simply use |typedef const char16_t* char16ptr_t|. Here, we want to make |
|
59 * the class as similar to this typedef, including providing some casts that |
|
60 * are allowed by the typedef. |
|
61 */ |
|
62 class char16ptr_t |
|
63 { |
|
64 private: |
|
65 const char16_t* ptr; |
|
66 static_assert(sizeof(char16_t) == sizeof(wchar_t), "char16_t and wchar_t sizes differ"); |
|
67 |
|
68 public: |
|
69 char16ptr_t(const char16_t* p) : ptr(p) {} |
|
70 char16ptr_t(const wchar_t* p) : ptr(reinterpret_cast<const char16_t*>(p)) {} |
|
71 |
|
72 /* Without this, nullptr assignment would be ambiguous. */ |
|
73 constexpr char16ptr_t(decltype(nullptr)) : ptr(nullptr) {} |
|
74 |
|
75 operator const char16_t*() const { |
|
76 return ptr; |
|
77 } |
|
78 operator const wchar_t*() const { |
|
79 return reinterpret_cast<const wchar_t*>(ptr); |
|
80 } |
|
81 operator const void*() const { |
|
82 return ptr; |
|
83 } |
|
84 operator bool() const { |
|
85 return ptr != nullptr; |
|
86 } |
|
87 operator std::wstring() const { |
|
88 return std::wstring(static_cast<const wchar_t*>(*this)); |
|
89 } |
|
90 |
|
91 /* Explicit cast operators to allow things like (char16_t*)str. */ |
|
92 explicit operator char16_t*() const { |
|
93 return const_cast<char16_t*>(ptr); |
|
94 } |
|
95 explicit operator wchar_t*() const { |
|
96 return const_cast<wchar_t*>(static_cast<const wchar_t*>(*this)); |
|
97 } |
|
98 explicit operator int() const { |
|
99 return reinterpret_cast<intptr_t>(ptr); |
|
100 } |
|
101 explicit operator unsigned int() const { |
|
102 return reinterpret_cast<uintptr_t>(ptr); |
|
103 } |
|
104 explicit operator long() const { |
|
105 return reinterpret_cast<intptr_t>(ptr); |
|
106 } |
|
107 explicit operator unsigned long() const { |
|
108 return reinterpret_cast<uintptr_t>(ptr); |
|
109 } |
|
110 explicit operator long long() const { |
|
111 return reinterpret_cast<intptr_t>(ptr); |
|
112 } |
|
113 explicit operator unsigned long long() const { |
|
114 return reinterpret_cast<uintptr_t>(ptr); |
|
115 } |
|
116 |
|
117 /** |
|
118 * Some Windows API calls accept BYTE* but require that data actually be WCHAR*. |
|
119 * Supporting this requires explicit operators to support the requisite explicit |
|
120 * casts. |
|
121 */ |
|
122 explicit operator const char*() const { |
|
123 return reinterpret_cast<const char*>(ptr); |
|
124 } |
|
125 explicit operator const unsigned char*() const { |
|
126 return reinterpret_cast<const unsigned char*>(ptr); |
|
127 } |
|
128 explicit operator unsigned char*() const { |
|
129 return const_cast<unsigned char*>(reinterpret_cast<const unsigned char*>(ptr)); |
|
130 } |
|
131 explicit operator void*() const { |
|
132 return const_cast<char16_t*>(ptr); |
|
133 } |
|
134 |
|
135 /* Some operators used on pointers. */ |
|
136 char16_t operator[](size_t i) const { |
|
137 return ptr[i]; |
|
138 } |
|
139 bool operator==(const char16ptr_t &x) const { |
|
140 return ptr == x.ptr; |
|
141 } |
|
142 bool operator==(decltype(nullptr)) const { |
|
143 return ptr == nullptr; |
|
144 } |
|
145 bool operator!=(const char16ptr_t &x) const { |
|
146 return ptr != x.ptr; |
|
147 } |
|
148 bool operator!=(decltype(nullptr)) const { |
|
149 return ptr != nullptr; |
|
150 } |
|
151 char16ptr_t operator+(int aValue) const { |
|
152 return char16ptr_t(ptr + aValue); |
|
153 } |
|
154 char16ptr_t operator+(unsigned int aValue) const { |
|
155 return char16ptr_t(ptr + aValue); |
|
156 } |
|
157 char16ptr_t operator+(long aValue) const { |
|
158 return char16ptr_t(ptr + aValue); |
|
159 } |
|
160 char16ptr_t operator+(unsigned long aValue) const { |
|
161 return char16ptr_t(ptr + aValue); |
|
162 } |
|
163 char16ptr_t operator+(long long aValue) const { |
|
164 return char16ptr_t(ptr + aValue); |
|
165 } |
|
166 char16ptr_t operator+(unsigned long long aValue) const { |
|
167 return char16ptr_t(ptr + aValue); |
|
168 } |
|
169 ptrdiff_t operator-(const char16ptr_t &other) const { |
|
170 return ptr - other.ptr; |
|
171 } |
|
172 }; |
|
173 |
|
174 inline decltype((char*)0-(char*)0) |
|
175 operator-(const char16_t* x, const char16ptr_t y) { |
|
176 return x - static_cast<const char16_t*>(y); |
|
177 } |
|
178 |
|
179 #else |
|
180 |
|
181 typedef const char16_t* char16ptr_t; |
|
182 |
|
183 #endif |
|
184 |
|
185 /* |
|
186 * Macro arguments used in concatenation or stringification won't be expanded. |
|
187 * Therefore, in order for |MOZ_UTF16(FOO)| to work as expected (which is to |
|
188 * expand |FOO| before doing whatever |MOZ_UTF16| needs to do to it) a helper |
|
189 * macro, |MOZ_UTF16_HELPER| needs to be inserted in between to allow the macro |
|
190 * argument to expand. See "3.10.6 Separate Expansion of Macro Arguments" of the |
|
191 * CPP manual for a more accurate and precise explanation. |
|
192 */ |
|
193 #define MOZ_UTF16(s) MOZ_UTF16_HELPER(s) |
|
194 |
|
195 static_assert(sizeof(char16_t) == 2, "Is char16_t type 16 bits?"); |
|
196 static_assert(char16_t(-1) > char16_t(0), "Is char16_t type unsigned?"); |
|
197 static_assert(sizeof(MOZ_UTF16('A')) == 2, "Is char literal 16 bits?"); |
|
198 static_assert(sizeof(MOZ_UTF16("")[0]) == 2, "Is string char 16 bits?"); |
|
199 |
|
200 #endif |
|
201 |
|
202 #endif /* mozilla_Char16_h */ |