1 // © 2017 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
5 // created: 2017feb28 Markus W. Scherer
7 #ifndef __CHAR16PTR_H__
8 #define __CHAR16PTR_H__
11 #include "unicode/utypes.h"
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 *.
20 #if U_SHOW_CPLUSPLUS_API
24 * \def U_ALIASING_BARRIER
25 * Barrier for pointer anti-aliasing optimizations even across function boundaries.
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)
37 * char16_t * wrapper with implicit conversion from distinct but bit-compatible pointer types.
40 class U_COMMON_API Char16Ptr U_FINAL
{
47 inline Char16Ptr(char16_t *p
);
48 #if !U_CHAR16_IS_TYPEDEF
50 * Converts the pointer to char16_t *.
51 * @param p pointer to be converted
54 inline Char16Ptr(uint16_t *p
);
56 #if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN)
58 * Converts the pointer to char16_t *.
59 * (Only defined if U_SIZEOF_WCHAR_T==2.)
60 * @param p pointer to be converted
63 inline Char16Ptr(wchar_t *p
);
66 * nullptr constructor.
70 inline Char16Ptr(std::nullptr_t p
);
79 * @return the wrapped pointer
82 inline char16_t *get() const;
84 * char16_t pointer access via type conversion (e.g., static_cast).
85 * @return the wrapped pointer
88 inline operator char16_t *() const { return get(); }
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
);
110 #ifdef U_ALIASING_BARRIER
112 Char16Ptr::Char16Ptr(char16_t *p
) : p_(p
) {}
113 #if !U_CHAR16_IS_TYPEDEF
114 Char16Ptr::Char16Ptr(uint16_t *p
) : p_(cast(p
)) {}
116 #if U_SIZEOF_WCHAR_T==2
117 Char16Ptr::Char16Ptr(wchar_t *p
) : p_(cast(p
)) {}
119 Char16Ptr::Char16Ptr(std::nullptr_t p
) : p_(p
) {}
120 Char16Ptr::~Char16Ptr() {
121 U_ALIASING_BARRIER(p_
);
124 char16_t *Char16Ptr::get() const { return p_
; }
128 Char16Ptr::Char16Ptr(char16_t *p
) { u_
.cp
= p
; }
129 #if !U_CHAR16_IS_TYPEDEF
130 Char16Ptr::Char16Ptr(uint16_t *p
) { u_
.up
= p
; }
132 #if U_SIZEOF_WCHAR_T==2
133 Char16Ptr::Char16Ptr(wchar_t *p
) { u_
.wp
= p
; }
135 Char16Ptr::Char16Ptr(std::nullptr_t p
) { u_
.cp
= p
; }
136 Char16Ptr::~Char16Ptr() {}
138 char16_t *Char16Ptr::get() const { return u_
.cp
; }
144 * const char16_t * wrapper with implicit conversion from distinct but bit-compatible pointer types.
147 class U_COMMON_API ConstChar16Ptr U_FINAL
{
150 * Copies the pointer.
154 inline ConstChar16Ptr(const char16_t *p
);
155 #if !U_CHAR16_IS_TYPEDEF
157 * Converts the pointer to char16_t *.
158 * @param p pointer to be converted
161 inline ConstChar16Ptr(const uint16_t *p
);
163 #if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN)
165 * Converts the pointer to char16_t *.
166 * (Only defined if U_SIZEOF_WCHAR_T==2.)
167 * @param p pointer to be converted
170 inline ConstChar16Ptr(const wchar_t *p
);
173 * nullptr constructor.
177 inline ConstChar16Ptr(const std::nullptr_t p
);
183 inline ~ConstChar16Ptr();
187 * @return the wrapped pointer
190 inline const char16_t *get() const;
192 * char16_t pointer access via type conversion (e.g., static_cast).
193 * @return the wrapped pointer
196 inline operator const char16_t *() const { return get(); }
199 ConstChar16Ptr() = delete;
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
);
218 #ifdef U_ALIASING_BARRIER
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
)) {}
224 #if U_SIZEOF_WCHAR_T==2
225 ConstChar16Ptr::ConstChar16Ptr(const wchar_t *p
) : p_(cast(p
)) {}
227 ConstChar16Ptr::ConstChar16Ptr(const std::nullptr_t p
) : p_(p
) {}
228 ConstChar16Ptr::~ConstChar16Ptr() {
229 U_ALIASING_BARRIER(p_
);
232 const char16_t *ConstChar16Ptr::get() const { return p_
; }
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
; }
240 #if U_SIZEOF_WCHAR_T==2
241 ConstChar16Ptr::ConstChar16Ptr(const wchar_t *p
) { u_
.wp
= p
; }
243 ConstChar16Ptr::ConstChar16Ptr(const std::nullptr_t p
) { u_
.cp
= p
; }
244 ConstChar16Ptr::~ConstChar16Ptr() {}
246 const char16_t *ConstChar16Ptr::get() const { return u_
.cp
; }
252 * Converts from const char16_t * to const UChar *.
253 * Includes an aliasing barrier if available.
255 * @return p as const UChar *
258 inline const UChar
*toUCharPtr(const char16_t *p
) {
259 #ifdef U_ALIASING_BARRIER
260 U_ALIASING_BARRIER(p
);
262 return reinterpret_cast<const UChar
*>(p
);
266 * Converts from char16_t * to UChar *.
267 * Includes an aliasing barrier if available.
269 * @return p as UChar *
272 inline UChar
*toUCharPtr(char16_t *p
) {
273 #ifdef U_ALIASING_BARRIER
274 U_ALIASING_BARRIER(p
);
276 return reinterpret_cast<UChar
*>(p
);
280 * Converts from const char16_t * to const OldUChar *.
281 * Includes an aliasing barrier if available.
283 * @return p as const OldUChar *
286 inline const OldUChar
*toOldUCharPtr(const char16_t *p
) {
287 #ifdef U_ALIASING_BARRIER
288 U_ALIASING_BARRIER(p
);
290 return reinterpret_cast<const OldUChar
*>(p
);
294 * Converts from char16_t * to OldUChar *.
295 * Includes an aliasing barrier if available.
297 * @return p as OldUChar *
300 inline OldUChar
*toOldUCharPtr(char16_t *p
) {
301 #ifdef U_ALIASING_BARRIER
302 U_ALIASING_BARRIER(p
);
304 return reinterpret_cast<OldUChar
*>(p
);
308 #endif // U_SHOW_CPLUSPLUS_API
310 #endif // __CHAR16PTR_H__