]>
Commit | Line | Data |
---|---|---|
f3c0d7a5 A |
1 | // © 2017 and later: Unicode, Inc. and others. |
2 | // License & terms of use: http://www.unicode.org/copyright.html | |
3 | ||
4 | // char16ptr.h | |
5 | // created: 2017feb28 Markus W. Scherer | |
6 | ||
7 | #ifndef __CHAR16PTR_H__ | |
8 | #define __CHAR16PTR_H__ | |
9 | ||
f3c0d7a5 A |
10 | #include "unicode/utypes.h" |
11 | ||
340931cb A |
12 | #if U_SHOW_CPLUSPLUS_API |
13 | ||
14 | #include <cstddef> | |
15 | ||
f3c0d7a5 A |
16 | /** |
17 | * \file | |
18 | * \brief C++ API: char16_t pointer wrappers with | |
19 | * implicit conversion from bit-compatible raw pointer types. | |
20 | * Also conversion functions from char16_t * to UChar * and OldUChar *. | |
21 | */ | |
22 | ||
f3c0d7a5 A |
23 | U_NAMESPACE_BEGIN |
24 | ||
25 | /** | |
26 | * \def U_ALIASING_BARRIER | |
27 | * Barrier for pointer anti-aliasing optimizations even across function boundaries. | |
28 | * @internal | |
29 | */ | |
30 | #ifdef U_ALIASING_BARRIER | |
31 | // Use the predefined value. | |
32 | #elif (defined(__clang__) || defined(__GNUC__)) && U_PLATFORM != U_PF_BROWSER_NATIVE_CLIENT | |
33 | # define U_ALIASING_BARRIER(ptr) asm volatile("" : : "rm"(ptr) : "memory") | |
3d1f044b A |
34 | #elif defined(U_IN_DOXYGEN) |
35 | # define U_ALIASING_BARRIER(ptr) | |
f3c0d7a5 A |
36 | #endif |
37 | ||
f3c0d7a5 A |
38 | /** |
39 | * char16_t * wrapper with implicit conversion from distinct but bit-compatible pointer types. | |
0f5d89e8 | 40 | * @stable ICU 59 |
f3c0d7a5 A |
41 | */ |
42 | class U_COMMON_API Char16Ptr U_FINAL { | |
43 | public: | |
44 | /** | |
45 | * Copies the pointer. | |
46 | * @param p pointer | |
0f5d89e8 | 47 | * @stable ICU 59 |
f3c0d7a5 A |
48 | */ |
49 | inline Char16Ptr(char16_t *p); | |
50 | #if !U_CHAR16_IS_TYPEDEF | |
51 | /** | |
52 | * Converts the pointer to char16_t *. | |
53 | * @param p pointer to be converted | |
0f5d89e8 | 54 | * @stable ICU 59 |
f3c0d7a5 A |
55 | */ |
56 | inline Char16Ptr(uint16_t *p); | |
57 | #endif | |
58 | #if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN) | |
59 | /** | |
60 | * Converts the pointer to char16_t *. | |
61 | * (Only defined if U_SIZEOF_WCHAR_T==2.) | |
62 | * @param p pointer to be converted | |
0f5d89e8 | 63 | * @stable ICU 59 |
f3c0d7a5 A |
64 | */ |
65 | inline Char16Ptr(wchar_t *p); | |
66 | #endif | |
67 | /** | |
68 | * nullptr constructor. | |
69 | * @param p nullptr | |
0f5d89e8 | 70 | * @stable ICU 59 |
f3c0d7a5 A |
71 | */ |
72 | inline Char16Ptr(std::nullptr_t p); | |
73 | /** | |
74 | * Destructor. | |
0f5d89e8 | 75 | * @stable ICU 59 |
f3c0d7a5 A |
76 | */ |
77 | inline ~Char16Ptr(); | |
78 | ||
79 | /** | |
80 | * Pointer access. | |
81 | * @return the wrapped pointer | |
0f5d89e8 | 82 | * @stable ICU 59 |
f3c0d7a5 A |
83 | */ |
84 | inline char16_t *get() const; | |
85 | /** | |
86 | * char16_t pointer access via type conversion (e.g., static_cast). | |
87 | * @return the wrapped pointer | |
0f5d89e8 | 88 | * @stable ICU 59 |
f3c0d7a5 A |
89 | */ |
90 | inline operator char16_t *() const { return get(); } | |
91 | ||
92 | private: | |
93 | Char16Ptr() = delete; | |
94 | ||
95 | #ifdef U_ALIASING_BARRIER | |
96 | template<typename T> static char16_t *cast(T *t) { | |
97 | U_ALIASING_BARRIER(t); | |
98 | return reinterpret_cast<char16_t *>(t); | |
99 | } | |
100 | ||
0f5d89e8 | 101 | char16_t *p_; |
f3c0d7a5 A |
102 | #else |
103 | union { | |
104 | char16_t *cp; | |
105 | uint16_t *up; | |
106 | wchar_t *wp; | |
0f5d89e8 | 107 | } u_; |
f3c0d7a5 A |
108 | #endif |
109 | }; | |
110 | ||
3d1f044b | 111 | /// \cond |
f3c0d7a5 A |
112 | #ifdef U_ALIASING_BARRIER |
113 | ||
0f5d89e8 | 114 | Char16Ptr::Char16Ptr(char16_t *p) : p_(p) {} |
f3c0d7a5 | 115 | #if !U_CHAR16_IS_TYPEDEF |
0f5d89e8 | 116 | Char16Ptr::Char16Ptr(uint16_t *p) : p_(cast(p)) {} |
f3c0d7a5 A |
117 | #endif |
118 | #if U_SIZEOF_WCHAR_T==2 | |
0f5d89e8 | 119 | Char16Ptr::Char16Ptr(wchar_t *p) : p_(cast(p)) {} |
f3c0d7a5 | 120 | #endif |
0f5d89e8 | 121 | Char16Ptr::Char16Ptr(std::nullptr_t p) : p_(p) {} |
f3c0d7a5 | 122 | Char16Ptr::~Char16Ptr() { |
0f5d89e8 | 123 | U_ALIASING_BARRIER(p_); |
f3c0d7a5 A |
124 | } |
125 | ||
0f5d89e8 | 126 | char16_t *Char16Ptr::get() const { return p_; } |
f3c0d7a5 A |
127 | |
128 | #else | |
129 | ||
0f5d89e8 | 130 | Char16Ptr::Char16Ptr(char16_t *p) { u_.cp = p; } |
f3c0d7a5 | 131 | #if !U_CHAR16_IS_TYPEDEF |
0f5d89e8 | 132 | Char16Ptr::Char16Ptr(uint16_t *p) { u_.up = p; } |
f3c0d7a5 A |
133 | #endif |
134 | #if U_SIZEOF_WCHAR_T==2 | |
0f5d89e8 | 135 | Char16Ptr::Char16Ptr(wchar_t *p) { u_.wp = p; } |
f3c0d7a5 | 136 | #endif |
0f5d89e8 | 137 | Char16Ptr::Char16Ptr(std::nullptr_t p) { u_.cp = p; } |
f3c0d7a5 A |
138 | Char16Ptr::~Char16Ptr() {} |
139 | ||
0f5d89e8 | 140 | char16_t *Char16Ptr::get() const { return u_.cp; } |
f3c0d7a5 A |
141 | |
142 | #endif | |
3d1f044b | 143 | /// \endcond |
f3c0d7a5 | 144 | |
f3c0d7a5 A |
145 | /** |
146 | * const char16_t * wrapper with implicit conversion from distinct but bit-compatible pointer types. | |
0f5d89e8 | 147 | * @stable ICU 59 |
f3c0d7a5 A |
148 | */ |
149 | class U_COMMON_API ConstChar16Ptr U_FINAL { | |
150 | public: | |
151 | /** | |
152 | * Copies the pointer. | |
153 | * @param p pointer | |
0f5d89e8 | 154 | * @stable ICU 59 |
f3c0d7a5 A |
155 | */ |
156 | inline ConstChar16Ptr(const char16_t *p); | |
157 | #if !U_CHAR16_IS_TYPEDEF | |
158 | /** | |
159 | * Converts the pointer to char16_t *. | |
160 | * @param p pointer to be converted | |
0f5d89e8 | 161 | * @stable ICU 59 |
f3c0d7a5 A |
162 | */ |
163 | inline ConstChar16Ptr(const uint16_t *p); | |
164 | #endif | |
165 | #if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN) | |
166 | /** | |
167 | * Converts the pointer to char16_t *. | |
168 | * (Only defined if U_SIZEOF_WCHAR_T==2.) | |
169 | * @param p pointer to be converted | |
0f5d89e8 | 170 | * @stable ICU 59 |
f3c0d7a5 A |
171 | */ |
172 | inline ConstChar16Ptr(const wchar_t *p); | |
173 | #endif | |
174 | /** | |
175 | * nullptr constructor. | |
176 | * @param p nullptr | |
0f5d89e8 | 177 | * @stable ICU 59 |
f3c0d7a5 A |
178 | */ |
179 | inline ConstChar16Ptr(const std::nullptr_t p); | |
180 | ||
181 | /** | |
182 | * Destructor. | |
0f5d89e8 | 183 | * @stable ICU 59 |
f3c0d7a5 A |
184 | */ |
185 | inline ~ConstChar16Ptr(); | |
186 | ||
187 | /** | |
188 | * Pointer access. | |
189 | * @return the wrapped pointer | |
0f5d89e8 | 190 | * @stable ICU 59 |
f3c0d7a5 A |
191 | */ |
192 | inline const char16_t *get() const; | |
193 | /** | |
194 | * char16_t pointer access via type conversion (e.g., static_cast). | |
195 | * @return the wrapped pointer | |
0f5d89e8 | 196 | * @stable ICU 59 |
f3c0d7a5 A |
197 | */ |
198 | inline operator const char16_t *() const { return get(); } | |
199 | ||
200 | private: | |
201 | ConstChar16Ptr() = delete; | |
202 | ||
203 | #ifdef U_ALIASING_BARRIER | |
204 | template<typename T> static const char16_t *cast(const T *t) { | |
205 | U_ALIASING_BARRIER(t); | |
206 | return reinterpret_cast<const char16_t *>(t); | |
207 | } | |
208 | ||
0f5d89e8 | 209 | const char16_t *p_; |
f3c0d7a5 A |
210 | #else |
211 | union { | |
212 | const char16_t *cp; | |
213 | const uint16_t *up; | |
214 | const wchar_t *wp; | |
0f5d89e8 | 215 | } u_; |
f3c0d7a5 A |
216 | #endif |
217 | }; | |
218 | ||
3d1f044b | 219 | /// \cond |
f3c0d7a5 A |
220 | #ifdef U_ALIASING_BARRIER |
221 | ||
0f5d89e8 | 222 | ConstChar16Ptr::ConstChar16Ptr(const char16_t *p) : p_(p) {} |
f3c0d7a5 | 223 | #if !U_CHAR16_IS_TYPEDEF |
0f5d89e8 | 224 | ConstChar16Ptr::ConstChar16Ptr(const uint16_t *p) : p_(cast(p)) {} |
f3c0d7a5 A |
225 | #endif |
226 | #if U_SIZEOF_WCHAR_T==2 | |
0f5d89e8 | 227 | ConstChar16Ptr::ConstChar16Ptr(const wchar_t *p) : p_(cast(p)) {} |
f3c0d7a5 | 228 | #endif |
0f5d89e8 | 229 | ConstChar16Ptr::ConstChar16Ptr(const std::nullptr_t p) : p_(p) {} |
f3c0d7a5 | 230 | ConstChar16Ptr::~ConstChar16Ptr() { |
0f5d89e8 | 231 | U_ALIASING_BARRIER(p_); |
f3c0d7a5 A |
232 | } |
233 | ||
0f5d89e8 | 234 | const char16_t *ConstChar16Ptr::get() const { return p_; } |
f3c0d7a5 A |
235 | |
236 | #else | |
237 | ||
0f5d89e8 | 238 | ConstChar16Ptr::ConstChar16Ptr(const char16_t *p) { u_.cp = p; } |
f3c0d7a5 | 239 | #if !U_CHAR16_IS_TYPEDEF |
0f5d89e8 | 240 | ConstChar16Ptr::ConstChar16Ptr(const uint16_t *p) { u_.up = p; } |
f3c0d7a5 A |
241 | #endif |
242 | #if U_SIZEOF_WCHAR_T==2 | |
0f5d89e8 | 243 | ConstChar16Ptr::ConstChar16Ptr(const wchar_t *p) { u_.wp = p; } |
f3c0d7a5 | 244 | #endif |
0f5d89e8 | 245 | ConstChar16Ptr::ConstChar16Ptr(const std::nullptr_t p) { u_.cp = p; } |
f3c0d7a5 A |
246 | ConstChar16Ptr::~ConstChar16Ptr() {} |
247 | ||
0f5d89e8 | 248 | const char16_t *ConstChar16Ptr::get() const { return u_.cp; } |
f3c0d7a5 A |
249 | |
250 | #endif | |
3d1f044b | 251 | /// \endcond |
f3c0d7a5 A |
252 | |
253 | /** | |
254 | * Converts from const char16_t * to const UChar *. | |
255 | * Includes an aliasing barrier if available. | |
256 | * @param p pointer | |
257 | * @return p as const UChar * | |
0f5d89e8 | 258 | * @stable ICU 59 |
f3c0d7a5 A |
259 | */ |
260 | inline const UChar *toUCharPtr(const char16_t *p) { | |
261 | #ifdef U_ALIASING_BARRIER | |
262 | U_ALIASING_BARRIER(p); | |
263 | #endif | |
264 | return reinterpret_cast<const UChar *>(p); | |
265 | } | |
266 | ||
267 | /** | |
268 | * Converts from char16_t * to UChar *. | |
269 | * Includes an aliasing barrier if available. | |
270 | * @param p pointer | |
271 | * @return p as UChar * | |
0f5d89e8 | 272 | * @stable ICU 59 |
f3c0d7a5 A |
273 | */ |
274 | inline UChar *toUCharPtr(char16_t *p) { | |
275 | #ifdef U_ALIASING_BARRIER | |
276 | U_ALIASING_BARRIER(p); | |
277 | #endif | |
278 | return reinterpret_cast<UChar *>(p); | |
279 | } | |
280 | ||
281 | /** | |
282 | * Converts from const char16_t * to const OldUChar *. | |
283 | * Includes an aliasing barrier if available. | |
284 | * @param p pointer | |
285 | * @return p as const OldUChar * | |
0f5d89e8 | 286 | * @stable ICU 59 |
f3c0d7a5 A |
287 | */ |
288 | inline const OldUChar *toOldUCharPtr(const char16_t *p) { | |
289 | #ifdef U_ALIASING_BARRIER | |
290 | U_ALIASING_BARRIER(p); | |
291 | #endif | |
292 | return reinterpret_cast<const OldUChar *>(p); | |
293 | } | |
294 | ||
295 | /** | |
296 | * Converts from char16_t * to OldUChar *. | |
297 | * Includes an aliasing barrier if available. | |
298 | * @param p pointer | |
299 | * @return p as OldUChar * | |
0f5d89e8 | 300 | * @stable ICU 59 |
f3c0d7a5 A |
301 | */ |
302 | inline OldUChar *toOldUCharPtr(char16_t *p) { | |
303 | #ifdef U_ALIASING_BARRIER | |
304 | U_ALIASING_BARRIER(p); | |
305 | #endif | |
306 | return reinterpret_cast<OldUChar *>(p); | |
307 | } | |
308 | ||
309 | U_NAMESPACE_END | |
340931cb A |
310 | |
311 | #endif /* U_SHOW_CPLUSPLUS_API */ | |
f3c0d7a5 A |
312 | |
313 | #endif // __CHAR16PTR_H__ |