]> git.saurik.com Git - apple/icu.git/blame - icuSources/common/unicode/char16ptr.h
ICU-59117.0.1.tar.gz
[apple/icu.git] / icuSources / common / unicode / char16ptr.h
CommitLineData
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
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
21U_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#endif
33
34// Do not use #ifndef U_HIDE_DRAFT_API for the following class, it
35// is now used in place of UChar* in several stable C++ methods
36/**
37 * char16_t * wrapper with implicit conversion from distinct but bit-compatible pointer types.
38 * @draft ICU 59
39 */
40class U_COMMON_API Char16Ptr U_FINAL {
41public:
42 /**
43 * Copies the pointer.
44 * @param p pointer
45 * @draft 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 * @draft 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 * @draft ICU 59
62 */
63 inline Char16Ptr(wchar_t *p);
64#endif
65 /**
66 * nullptr constructor.
67 * @param p nullptr
68 * @draft ICU 59
69 */
70 inline Char16Ptr(std::nullptr_t p);
71 /**
72 * Destructor.
73 * @draft ICU 59
74 */
75 inline ~Char16Ptr();
76
77 /**
78 * Pointer access.
79 * @return the wrapped pointer
80 * @draft 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 * @draft ICU 59
87 */
88 inline operator char16_t *() const { return get(); }
89
90private:
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#ifdef U_ALIASING_BARRIER
110
111Char16Ptr::Char16Ptr(char16_t *p) : p(p) {}
112#if !U_CHAR16_IS_TYPEDEF
113Char16Ptr::Char16Ptr(uint16_t *p) : p(cast(p)) {}
114#endif
115#if U_SIZEOF_WCHAR_T==2
116Char16Ptr::Char16Ptr(wchar_t *p) : p(cast(p)) {}
117#endif
118Char16Ptr::Char16Ptr(std::nullptr_t p) : p(p) {}
119Char16Ptr::~Char16Ptr() {
120 U_ALIASING_BARRIER(p);
121}
122
123char16_t *Char16Ptr::get() const { return p; }
124
125#else
126
127Char16Ptr::Char16Ptr(char16_t *p) { u.cp = p; }
128#if !U_CHAR16_IS_TYPEDEF
129Char16Ptr::Char16Ptr(uint16_t *p) { u.up = p; }
130#endif
131#if U_SIZEOF_WCHAR_T==2
132Char16Ptr::Char16Ptr(wchar_t *p) { u.wp = p; }
133#endif
134Char16Ptr::Char16Ptr(std::nullptr_t p) { u.cp = p; }
135Char16Ptr::~Char16Ptr() {}
136
137char16_t *Char16Ptr::get() const { return u.cp; }
138
139#endif
140
141// Do not use #ifndef U_HIDE_DRAFT_API for the following class, it is
142// now used in place of const UChar* in several stable C++ methods
143/**
144 * const char16_t * wrapper with implicit conversion from distinct but bit-compatible pointer types.
145 * @draft ICU 59
146 */
147class U_COMMON_API ConstChar16Ptr U_FINAL {
148public:
149 /**
150 * Copies the pointer.
151 * @param p pointer
152 * @draft 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 * @draft 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 * @draft ICU 59
169 */
170 inline ConstChar16Ptr(const wchar_t *p);
171#endif
172 /**
173 * nullptr constructor.
174 * @param p nullptr
175 * @draft ICU 59
176 */
177 inline ConstChar16Ptr(const std::nullptr_t p);
178
179 /**
180 * Destructor.
181 * @draft ICU 59
182 */
183 inline ~ConstChar16Ptr();
184
185 /**
186 * Pointer access.
187 * @return the wrapped pointer
188 * @draft 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 * @draft ICU 59
195 */
196 inline operator const char16_t *() const { return get(); }
197
198private:
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#ifdef U_ALIASING_BARRIER
218
219ConstChar16Ptr::ConstChar16Ptr(const char16_t *p) : p(p) {}
220#if !U_CHAR16_IS_TYPEDEF
221ConstChar16Ptr::ConstChar16Ptr(const uint16_t *p) : p(cast(p)) {}
222#endif
223#if U_SIZEOF_WCHAR_T==2
224ConstChar16Ptr::ConstChar16Ptr(const wchar_t *p) : p(cast(p)) {}
225#endif
226ConstChar16Ptr::ConstChar16Ptr(const std::nullptr_t p) : p(p) {}
227ConstChar16Ptr::~ConstChar16Ptr() {
228 U_ALIASING_BARRIER(p);
229}
230
231const char16_t *ConstChar16Ptr::get() const { return p; }
232
233#else
234
235ConstChar16Ptr::ConstChar16Ptr(const char16_t *p) { u.cp = p; }
236#if !U_CHAR16_IS_TYPEDEF
237ConstChar16Ptr::ConstChar16Ptr(const uint16_t *p) { u.up = p; }
238#endif
239#if U_SIZEOF_WCHAR_T==2
240ConstChar16Ptr::ConstChar16Ptr(const wchar_t *p) { u.wp = p; }
241#endif
242ConstChar16Ptr::ConstChar16Ptr(const std::nullptr_t p) { u.cp = p; }
243ConstChar16Ptr::~ConstChar16Ptr() {}
244
245const char16_t *ConstChar16Ptr::get() const { return u.cp; }
246
247#endif
248
249/**
250 * Converts from const char16_t * to const UChar *.
251 * Includes an aliasing barrier if available.
252 * @param p pointer
253 * @return p as const UChar *
254 * @draft ICU 59
255 */
256inline const UChar *toUCharPtr(const char16_t *p) {
257#ifdef U_ALIASING_BARRIER
258 U_ALIASING_BARRIER(p);
259#endif
260 return reinterpret_cast<const UChar *>(p);
261}
262
263/**
264 * Converts from char16_t * to UChar *.
265 * Includes an aliasing barrier if available.
266 * @param p pointer
267 * @return p as UChar *
268 * @draft ICU 59
269 */
270inline UChar *toUCharPtr(char16_t *p) {
271#ifdef U_ALIASING_BARRIER
272 U_ALIASING_BARRIER(p);
273#endif
274 return reinterpret_cast<UChar *>(p);
275}
276
277/**
278 * Converts from const char16_t * to const OldUChar *.
279 * Includes an aliasing barrier if available.
280 * @param p pointer
281 * @return p as const OldUChar *
282 * @draft ICU 59
283 */
284inline const OldUChar *toOldUCharPtr(const char16_t *p) {
285#ifdef U_ALIASING_BARRIER
286 U_ALIASING_BARRIER(p);
287#endif
288 return reinterpret_cast<const OldUChar *>(p);
289}
290
291/**
292 * Converts from char16_t * to OldUChar *.
293 * Includes an aliasing barrier if available.
294 * @param p pointer
295 * @return p as OldUChar *
296 * @draft ICU 59
297 */
298inline OldUChar *toOldUCharPtr(char16_t *p) {
299#ifdef U_ALIASING_BARRIER
300 U_ALIASING_BARRIER(p);
301#endif
302 return reinterpret_cast<OldUChar *>(p);
303}
304
305U_NAMESPACE_END
306#endif // U_SHOW_CPLUSPLUS_API
307
308#endif // __CHAR16PTR_H__