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__
10 #include "unicode/utypes.h"
12 #if U_SHOW_CPLUSPLUS_API
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 *.
26 * \def U_ALIASING_BARRIER
27 * Barrier for pointer anti-aliasing optimizations even across function boundaries.
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")
34 #elif defined(U_IN_DOXYGEN)
35 # define U_ALIASING_BARRIER(ptr)
39 * char16_t * wrapper with implicit conversion from distinct but bit-compatible pointer types.
42 class U_COMMON_API Char16Ptr U_FINAL
{
49 inline Char16Ptr(char16_t *p
);
50 #if !U_CHAR16_IS_TYPEDEF
52 * Converts the pointer to char16_t *.
53 * @param p pointer to be converted
56 inline Char16Ptr(uint16_t *p
);
58 #if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN)
60 * Converts the pointer to char16_t *.
61 * (Only defined if U_SIZEOF_WCHAR_T==2.)
62 * @param p pointer to be converted
65 inline Char16Ptr(wchar_t *p
);
68 * nullptr constructor.
72 inline Char16Ptr(std::nullptr_t p
);
81 * @return the wrapped pointer
84 inline char16_t *get() const;
86 * char16_t pointer access via type conversion (e.g., static_cast).
87 * @return the wrapped pointer
90 inline operator char16_t *() const { return get(); }
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
);
112 #ifdef U_ALIASING_BARRIER
114 Char16Ptr::Char16Ptr(char16_t *p
) : p_(p
) {}
115 #if !U_CHAR16_IS_TYPEDEF
116 Char16Ptr::Char16Ptr(uint16_t *p
) : p_(cast(p
)) {}
118 #if U_SIZEOF_WCHAR_T==2
119 Char16Ptr::Char16Ptr(wchar_t *p
) : p_(cast(p
)) {}
121 Char16Ptr::Char16Ptr(std::nullptr_t p
) : p_(p
) {}
122 Char16Ptr::~Char16Ptr() {
123 U_ALIASING_BARRIER(p_
);
126 char16_t *Char16Ptr::get() const { return p_
; }
130 Char16Ptr::Char16Ptr(char16_t *p
) { u_
.cp
= p
; }
131 #if !U_CHAR16_IS_TYPEDEF
132 Char16Ptr::Char16Ptr(uint16_t *p
) { u_
.up
= p
; }
134 #if U_SIZEOF_WCHAR_T==2
135 Char16Ptr::Char16Ptr(wchar_t *p
) { u_
.wp
= p
; }
137 Char16Ptr::Char16Ptr(std::nullptr_t p
) { u_
.cp
= p
; }
138 Char16Ptr::~Char16Ptr() {}
140 char16_t *Char16Ptr::get() const { return u_
.cp
; }
146 * const char16_t * wrapper with implicit conversion from distinct but bit-compatible pointer types.
149 class U_COMMON_API ConstChar16Ptr U_FINAL
{
152 * Copies the pointer.
156 inline ConstChar16Ptr(const char16_t *p
);
157 #if !U_CHAR16_IS_TYPEDEF
159 * Converts the pointer to char16_t *.
160 * @param p pointer to be converted
163 inline ConstChar16Ptr(const uint16_t *p
);
165 #if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN)
167 * Converts the pointer to char16_t *.
168 * (Only defined if U_SIZEOF_WCHAR_T==2.)
169 * @param p pointer to be converted
172 inline ConstChar16Ptr(const wchar_t *p
);
175 * nullptr constructor.
179 inline ConstChar16Ptr(const std::nullptr_t p
);
185 inline ~ConstChar16Ptr();
189 * @return the wrapped pointer
192 inline const char16_t *get() const;
194 * char16_t pointer access via type conversion (e.g., static_cast).
195 * @return the wrapped pointer
198 inline operator const char16_t *() const { return get(); }
201 ConstChar16Ptr() = delete;
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
);
220 #ifdef U_ALIASING_BARRIER
222 ConstChar16Ptr::ConstChar16Ptr(const char16_t *p
) : p_(p
) {}
223 #if !U_CHAR16_IS_TYPEDEF
224 ConstChar16Ptr::ConstChar16Ptr(const uint16_t *p
) : p_(cast(p
)) {}
226 #if U_SIZEOF_WCHAR_T==2
227 ConstChar16Ptr::ConstChar16Ptr(const wchar_t *p
) : p_(cast(p
)) {}
229 ConstChar16Ptr::ConstChar16Ptr(const std::nullptr_t p
) : p_(p
) {}
230 ConstChar16Ptr::~ConstChar16Ptr() {
231 U_ALIASING_BARRIER(p_
);
234 const char16_t *ConstChar16Ptr::get() const { return p_
; }
238 ConstChar16Ptr::ConstChar16Ptr(const char16_t *p
) { u_
.cp
= p
; }
239 #if !U_CHAR16_IS_TYPEDEF
240 ConstChar16Ptr::ConstChar16Ptr(const uint16_t *p
) { u_
.up
= p
; }
242 #if U_SIZEOF_WCHAR_T==2
243 ConstChar16Ptr::ConstChar16Ptr(const wchar_t *p
) { u_
.wp
= p
; }
245 ConstChar16Ptr::ConstChar16Ptr(const std::nullptr_t p
) { u_
.cp
= p
; }
246 ConstChar16Ptr::~ConstChar16Ptr() {}
248 const char16_t *ConstChar16Ptr::get() const { return u_
.cp
; }
254 * Converts from const char16_t * to const UChar *.
255 * Includes an aliasing barrier if available.
257 * @return p as const UChar *
260 inline const UChar
*toUCharPtr(const char16_t *p
) {
261 #ifdef U_ALIASING_BARRIER
262 U_ALIASING_BARRIER(p
);
264 return reinterpret_cast<const UChar
*>(p
);
268 * Converts from char16_t * to UChar *.
269 * Includes an aliasing barrier if available.
271 * @return p as UChar *
274 inline UChar
*toUCharPtr(char16_t *p
) {
275 #ifdef U_ALIASING_BARRIER
276 U_ALIASING_BARRIER(p
);
278 return reinterpret_cast<UChar
*>(p
);
282 * Converts from const char16_t * to const OldUChar *.
283 * Includes an aliasing barrier if available.
285 * @return p as const OldUChar *
288 inline const OldUChar
*toOldUCharPtr(const char16_t *p
) {
289 #ifdef U_ALIASING_BARRIER
290 U_ALIASING_BARRIER(p
);
292 return reinterpret_cast<const OldUChar
*>(p
);
296 * Converts from char16_t * to OldUChar *.
297 * Includes an aliasing barrier if available.
299 * @return p as OldUChar *
302 inline OldUChar
*toOldUCharPtr(char16_t *p
) {
303 #ifdef U_ALIASING_BARRIER
304 U_ALIASING_BARRIER(p
);
306 return reinterpret_cast<OldUChar
*>(p
);
311 #endif /* U_SHOW_CPLUSPLUS_API */
313 #endif // __CHAR16PTR_H__