|
| 1 | +// © 2017 and later: Unicode, Inc. and others. |
| 2 | +// License & terms of use: http://www.unicode.org/copyright.html |
| 3 | +// char16ptr.h |
| 4 | +// created: 2017feb28 Markus W. Scherer |
| 5 | +#ifndef __CHAR16PTR_H__ |
| 6 | +#define __CHAR16PTR_H__ |
| 7 | +#include <cstddef> |
| 8 | +#include "unicode/utypes.h" |
| 9 | +/** |
| 10 | + * \file |
| 11 | + * \brief C++ API: char16_t pointer wrappers with |
| 12 | + * implicit conversion from bit-compatible raw pointer types. |
| 13 | + * Also conversion functions from char16_t * to UChar * and OldUChar *. |
| 14 | + */ |
| 15 | +U_NAMESPACE_BEGIN |
| 16 | +/** |
| 17 | + * \def U_ALIASING_BARRIER |
| 18 | + * Barrier for pointer anti-aliasing optimizations even across function boundaries. |
| 19 | + * @internal |
| 20 | + */ |
| 21 | +#ifdef U_ALIASING_BARRIER |
| 22 | + // Use the predefined value. |
| 23 | +#elif (defined(__clang__) || defined(__GNUC__)) && U_PLATFORM != U_PF_BROWSER_NATIVE_CLIENT |
| 24 | +# define U_ALIASING_BARRIER(ptr) asm volatile("" : : "rm"(ptr) : "memory") |
| 25 | +#endif |
| 26 | +// Do not use #ifndef U_HIDE_DRAFT_API for the following class, it |
| 27 | +// is now used in place of UChar* in several stable C++ methods |
| 28 | +/** |
| 29 | + * char16_t * wrapper with implicit conversion from distinct but bit-compatible pointer types. |
| 30 | + * @draft ICU 59 |
| 31 | + */ |
| 32 | +class U_COMMON_API Char16Ptr U_FINAL { |
| 33 | +public: |
| 34 | + /** |
| 35 | + * Copies the pointer. |
| 36 | + * @param p pointer |
| 37 | + * @draft ICU 59 |
| 38 | + */ |
| 39 | + inline Char16Ptr(char16_t *p); |
| 40 | +#if !U_CHAR16_IS_TYPEDEF |
| 41 | + /** |
| 42 | + * Converts the pointer to char16_t *. |
| 43 | + * @param p pointer to be converted |
| 44 | + * @draft ICU 59 |
| 45 | + */ |
| 46 | + inline Char16Ptr(uint16_t *p); |
| 47 | +#endif |
| 48 | +#if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN) |
| 49 | + /** |
| 50 | + * Converts the pointer to char16_t *. |
| 51 | + * (Only defined if U_SIZEOF_WCHAR_T==2.) |
| 52 | + * @param p pointer to be converted |
| 53 | + * @draft ICU 59 |
| 54 | + */ |
| 55 | + inline Char16Ptr(wchar_t *p); |
| 56 | +#endif |
| 57 | + /** |
| 58 | + * nullptr constructor. |
| 59 | + * @param p nullptr |
| 60 | + * @draft ICU 59 |
| 61 | + */ |
| 62 | + inline Char16Ptr(std::nullptr_t p); |
| 63 | + /** |
| 64 | + * Destructor. |
| 65 | + * @draft ICU 59 |
| 66 | + */ |
| 67 | + inline ~Char16Ptr(); |
| 68 | + /** |
| 69 | + * Pointer access. |
| 70 | + * @return the wrapped pointer |
| 71 | + * @draft ICU 59 |
| 72 | + */ |
| 73 | + inline UChar *get() const; |
| 74 | + /** |
| 75 | + * char16_t pointer access via type conversion (e.g., static_cast). |
| 76 | + * @return the wrapped pointer |
| 77 | + * @draft ICU 59 |
| 78 | + */ |
| 79 | + inline operator UChar *() const { return get(); } |
| 80 | +private: |
| 81 | + Char16Ptr() = delete; |
| 82 | +#ifdef U_ALIASING_BARRIER |
| 83 | + template<typename T> static char16_t *cast(T *t) { |
| 84 | + U_ALIASING_BARRIER(t); |
| 85 | + return reinterpret_cast<char16_t *>(t); |
| 86 | + } |
| 87 | + char16_t *p; |
| 88 | +#else |
| 89 | + union { |
| 90 | + char16_t *cp; |
| 91 | + uint16_t *up; |
| 92 | + wchar_t *wp; |
| 93 | + UChar *ucp; |
| 94 | + } u; |
| 95 | +#endif |
| 96 | +}; |
| 97 | +#ifdef U_ALIASING_BARRIER |
| 98 | +Char16Ptr::Char16Ptr(char16_t *p) : p(p) {} |
| 99 | +#if !U_CHAR16_IS_TYPEDEF |
| 100 | +Char16Ptr::Char16Ptr(uint16_t *p) : p(cast(p)) {} |
| 101 | +#endif |
| 102 | +#if U_SIZEOF_WCHAR_T==2 |
| 103 | +Char16Ptr::Char16Ptr(wchar_t *p) : p(cast(p)) {} |
| 104 | +#endif |
| 105 | +Char16Ptr::Char16Ptr(std::nullptr_t p) : p(p) {} |
| 106 | +Char16Ptr::~Char16Ptr() { |
| 107 | + U_ALIASING_BARRIER(p); |
| 108 | +} |
| 109 | +UChar *Char16Ptr::get() const { |
| 110 | +#ifdef U_ALIASING_BARRIER |
| 111 | + U_ALIASING_BARRIER(p); |
| 112 | +#endif |
| 113 | + return reinterpret_cast<UChar *>(p); |
| 114 | +} |
| 115 | +#else |
| 116 | +Char16Ptr::Char16Ptr(char16_t *p) { u.cp = p; } |
| 117 | +#if !U_CHAR16_IS_TYPEDEF |
| 118 | +Char16Ptr::Char16Ptr(uint16_t *p) { u.up = p; } |
| 119 | +#endif |
| 120 | +#if U_SIZEOF_WCHAR_T==2 |
| 121 | +Char16Ptr::Char16Ptr(wchar_t *p) { u.wp = p; } |
| 122 | +#endif |
| 123 | +Char16Ptr::Char16Ptr(std::nullptr_t p) { u.cp = p; } |
| 124 | +Char16Ptr::~Char16Ptr() {} |
| 125 | +UChar *Char16Ptr::get() const { return u.ucp; } |
| 126 | +#endif |
| 127 | +// Do not use #ifndef U_HIDE_DRAFT_API for the following class, it is |
| 128 | +// now used in place of const UChar* in several stable C++ methods |
| 129 | +/** |
| 130 | + * const char16_t * wrapper with implicit conversion from distinct but bit-compatible pointer types. |
| 131 | + * @draft ICU 59 |
| 132 | + */ |
| 133 | +class U_COMMON_API ConstChar16Ptr U_FINAL { |
| 134 | +public: |
| 135 | + /** |
| 136 | + * Copies the pointer. |
| 137 | + * @param p pointer |
| 138 | + * @draft ICU 59 |
| 139 | + */ |
| 140 | + inline ConstChar16Ptr(const char16_t *p); |
| 141 | +#if !U_CHAR16_IS_TYPEDEF |
| 142 | + /** |
| 143 | + * Converts the pointer to char16_t *. |
| 144 | + * @param p pointer to be converted |
| 145 | + * @draft ICU 59 |
| 146 | + */ |
| 147 | + inline ConstChar16Ptr(const uint16_t *p); |
| 148 | +#endif |
| 149 | +#if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN) |
| 150 | + /** |
| 151 | + * Converts the pointer to char16_t *. |
| 152 | + * (Only defined if U_SIZEOF_WCHAR_T==2.) |
| 153 | + * @param p pointer to be converted |
| 154 | + * @draft ICU 59 |
| 155 | + */ |
| 156 | + inline ConstChar16Ptr(const wchar_t *p); |
| 157 | +#endif |
| 158 | + /** |
| 159 | + * nullptr constructor. |
| 160 | + * @param p nullptr |
| 161 | + * @draft ICU 59 |
| 162 | + */ |
| 163 | + inline ConstChar16Ptr(const std::nullptr_t p); |
| 164 | + /** |
| 165 | + * Destructor. |
| 166 | + * @draft ICU 59 |
| 167 | + */ |
| 168 | + inline ~ConstChar16Ptr(); |
| 169 | + /** |
| 170 | + * Pointer access. |
| 171 | + * @return the wrapped pointer |
| 172 | + * @draft ICU 59 |
| 173 | + */ |
| 174 | + inline const UChar *get() const; |
| 175 | + /** |
| 176 | + * char16_t pointer access via type conversion (e.g., static_cast). |
| 177 | + * @return the wrapped pointer |
| 178 | + * @draft ICU 59 |
| 179 | + */ |
| 180 | + inline operator const UChar *() const { return get(); } |
| 181 | +private: |
| 182 | + ConstChar16Ptr() = delete; |
| 183 | +#ifdef U_ALIASING_BARRIER |
| 184 | + template<typename T> static const char16_t *cast(const T *t) { |
| 185 | + U_ALIASING_BARRIER(t); |
| 186 | + return reinterpret_cast<const char16_t *>(t); |
| 187 | + } |
| 188 | + const char16_t *p; |
| 189 | +#else |
| 190 | + union { |
| 191 | + const char16_t *cp; |
| 192 | + const uint16_t *up; |
| 193 | + const wchar_t *wp; |
| 194 | + Const UChar *ucp; |
| 195 | + } u; |
| 196 | +#endif |
| 197 | +}; |
| 198 | +#ifdef U_ALIASING_BARRIER |
| 199 | +ConstChar16Ptr::ConstChar16Ptr(const char16_t *p) : p(p) {} |
| 200 | +#if !U_CHAR16_IS_TYPEDEF |
| 201 | +ConstChar16Ptr::ConstChar16Ptr(const uint16_t *p) : p(cast(p)) {} |
| 202 | +#endif |
| 203 | +#if U_SIZEOF_WCHAR_T==2 |
| 204 | +ConstChar16Ptr::ConstChar16Ptr(const wchar_t *p) : p(cast(p)) {} |
| 205 | +#endif |
| 206 | +ConstChar16Ptr::ConstChar16Ptr(const std::nullptr_t p) : p(p) {} |
| 207 | +ConstChar16Ptr::~ConstChar16Ptr() { |
| 208 | + U_ALIASING_BARRIER(p); |
| 209 | +} |
| 210 | +const UChar *ConstChar16Ptr::get() const { |
| 211 | +#ifdef U_ALIASING_BARRIER |
| 212 | + U_ALIASING_BARRIER(p); |
| 213 | +#endif |
| 214 | + return reinterpret_cast<const UChar *>(p); |
| 215 | +} |
| 216 | +#else |
| 217 | +ConstChar16Ptr::ConstChar16Ptr(const char16_t *p) { u.cp = p; } |
| 218 | +#if !U_CHAR16_IS_TYPEDEF |
| 219 | +ConstChar16Ptr::ConstChar16Ptr(const uint16_t *p) { u.up = p; } |
| 220 | +#endif |
| 221 | +#if U_SIZEOF_WCHAR_T==2 |
| 222 | +ConstChar16Ptr::ConstChar16Ptr(const wchar_t *p) { u.wp = p; } |
| 223 | +#endif |
| 224 | +ConstChar16Ptr::ConstChar16Ptr(const std::nullptr_t p) { u.cp = p; } |
| 225 | +ConstChar16Ptr::~ConstChar16Ptr() {} |
| 226 | +const UChar *ConstChar16Ptr::get() const { return u.ucp; } |
| 227 | +#endif |
| 228 | +// Patched in from ICU 59 umachine.h |
| 229 | +/** |
| 230 | + * \var OldUChar |
| 231 | + * Default ICU 58 definition of UChar. |
| 232 | + * A base type for UTF-16 code units and pointers. |
| 233 | + * Unsigned 16-bit integer. |
| 234 | + * |
| 235 | + * Define OldUChar to be wchar_t if that is 16 bits wide. |
| 236 | + * If wchar_t is not 16 bits wide, then define UChar to be uint16_t. |
| 237 | + * |
| 238 | + * This makes the definition of OldUChar platform-dependent |
| 239 | + * but allows direct string type compatibility with platforms with |
| 240 | + * 16-bit wchar_t types. |
| 241 | + * |
| 242 | + * This is how UChar was defined in ICU 58, for transition convenience. |
| 243 | + * Exception: ICU 58 UChar was defined to UCHAR_TYPE if that macro was defined. |
| 244 | + * The current UChar responds to UCHAR_TYPE but OldUChar does not. |
| 245 | + * |
| 246 | + * @draft ICU 59 |
| 247 | + */ |
| 248 | +#if U_SIZEOF_WCHAR_T==2 |
| 249 | + typedef wchar_t OldUChar; |
| 250 | +#elif defined(__CHAR16_TYPE__) |
| 251 | + typedef __CHAR16_TYPE__ OldUChar; |
| 252 | +#else |
| 253 | + typedef uint16_t OldUChar; |
| 254 | +#endif |
| 255 | +// End of patch from ICU 59 umachine.h |
| 256 | +/** |
| 257 | + * Converts from const char16_t * to const UChar *. |
| 258 | + * Includes an aliasing barrier if available. |
| 259 | + * @param p pointer |
| 260 | + * @return p as const UChar * |
| 261 | + * @draft ICU 59 |
| 262 | + */ |
| 263 | +inline const UChar *toUCharPtr(const UChar *p) { |
| 264 | + // backport: no-op |
| 265 | + return p; |
| 266 | +} |
| 267 | +/** |
| 268 | + * Converts from char16_t * to UChar *. |
| 269 | + * Includes an aliasing barrier if available. |
| 270 | + * @param p pointer |
| 271 | + * @return p as UChar * |
| 272 | + * @draft ICU 59 |
| 273 | + */ |
| 274 | +inline UChar *toUCharPtr(UChar *p) { |
| 275 | + // backport: no-op |
| 276 | + return p; |
| 277 | +} |
| 278 | +/** |
| 279 | + * Converts from const char16_t * to const OldUChar *. |
| 280 | + * Includes an aliasing barrier if available. |
| 281 | + * @param p pointer |
| 282 | + * @return p as const OldUChar * |
| 283 | + * @draft ICU 59 |
| 284 | + */ |
| 285 | +inline const OldUChar *toOldUCharPtr(const UChar *p) { |
| 286 | + return p; |
| 287 | +} |
| 288 | +/** |
| 289 | + * Converts from char16_t * to OldUChar *. |
| 290 | + * Includes an aliasing barrier if available. |
| 291 | + * @param p pointer |
| 292 | + * @return p as OldUChar * |
| 293 | + * @draft ICU 59 |
| 294 | + */ |
| 295 | +inline OldUChar *toOldUCharPtr(UChar *p) { |
| 296 | + return p; |
| 297 | +} |
| 298 | +U_NAMESPACE_END |
| 299 | +#endif // __CHAR16PTR_H__ |
0 commit comments