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