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")
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
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
);
109 #ifdef U_ALIASING_BARRIER
111 Char16Ptr::Char16Ptr(char16_t *p
) : p(p
) {}
112 #if !U_CHAR16_IS_TYPEDEF
113 Char16Ptr::Char16Ptr(uint16_t *p
) : p(cast(p
)) {}
115 #if U_SIZEOF_WCHAR_T==2
116 Char16Ptr::Char16Ptr(wchar_t *p
) : p(cast(p
)) {}
118 Char16Ptr::Char16Ptr(std::nullptr_t p
) : p(p
) {}
119 Char16Ptr::~Char16Ptr() {
120 U_ALIASING_BARRIER(p
);
123 char16_t *Char16Ptr::get() const { return p
; }
127 Char16Ptr::Char16Ptr(char16_t *p
) { u
.cp
= p
; }
128 #if !U_CHAR16_IS_TYPEDEF
129 Char16Ptr::Char16Ptr(uint16_t *p
) { u
.up
= p
; }
131 #if U_SIZEOF_WCHAR_T==2
132 Char16Ptr::Char16Ptr(wchar_t *p
) { u
.wp
= p
; }
134 Char16Ptr::Char16Ptr(std::nullptr_t p
) { u
.cp
= p
; }
135 Char16Ptr::~Char16Ptr() {}
137 char16_t *Char16Ptr::get() const { return u
.cp
; }
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
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
);
217 #ifdef U_ALIASING_BARRIER
219 ConstChar16Ptr::ConstChar16Ptr(const char16_t *p
) : p(p
) {}
220 #if !U_CHAR16_IS_TYPEDEF
221 ConstChar16Ptr::ConstChar16Ptr(const uint16_t *p
) : p(cast(p
)) {}
223 #if U_SIZEOF_WCHAR_T==2
224 ConstChar16Ptr::ConstChar16Ptr(const wchar_t *p
) : p(cast(p
)) {}
226 ConstChar16Ptr::ConstChar16Ptr(const std::nullptr_t p
) : p(p
) {}
227 ConstChar16Ptr::~ConstChar16Ptr() {
228 U_ALIASING_BARRIER(p
);
231 const char16_t *ConstChar16Ptr::get() const { return p
; }
235 ConstChar16Ptr::ConstChar16Ptr(const char16_t *p
) { u
.cp
= p
; }
236 #if !U_CHAR16_IS_TYPEDEF
237 ConstChar16Ptr::ConstChar16Ptr(const uint16_t *p
) { u
.up
= p
; }
239 #if U_SIZEOF_WCHAR_T==2
240 ConstChar16Ptr::ConstChar16Ptr(const wchar_t *p
) { u
.wp
= p
; }
242 ConstChar16Ptr::ConstChar16Ptr(const std::nullptr_t p
) { u
.cp
= p
; }
243 ConstChar16Ptr::~ConstChar16Ptr() {}
245 const char16_t *ConstChar16Ptr::get() const { return u
.cp
; }
250 * Converts from const char16_t * to const UChar *.
251 * Includes an aliasing barrier if available.
253 * @return p as const UChar *
256 inline const UChar
*toUCharPtr(const char16_t *p
) {
257 #ifdef U_ALIASING_BARRIER
258 U_ALIASING_BARRIER(p
);
260 return reinterpret_cast<const UChar
*>(p
);
264 * Converts from char16_t * to UChar *.
265 * Includes an aliasing barrier if available.
267 * @return p as UChar *
270 inline UChar
*toUCharPtr(char16_t *p
) {
271 #ifdef U_ALIASING_BARRIER
272 U_ALIASING_BARRIER(p
);
274 return reinterpret_cast<UChar
*>(p
);
278 * Converts from const char16_t * to const OldUChar *.
279 * Includes an aliasing barrier if available.
281 * @return p as const OldUChar *
284 inline const OldUChar
*toOldUCharPtr(const char16_t *p
) {
285 #ifdef U_ALIASING_BARRIER
286 U_ALIASING_BARRIER(p
);
288 return reinterpret_cast<const OldUChar
*>(p
);
292 * Converts from char16_t * to OldUChar *.
293 * Includes an aliasing barrier if available.
295 * @return p as OldUChar *
298 inline OldUChar
*toOldUCharPtr(char16_t *p
) {
299 #ifdef U_ALIASING_BARRIER
300 U_ALIASING_BARRIER(p
);
302 return reinterpret_cast<OldUChar
*>(p
);
306 #endif // U_SHOW_CPLUSPLUS_API
308 #endif // __CHAR16PTR_H__