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__ 12 #if U_SHOW_CPLUSPLUS_API || U_SHOW_CPLUSPLUS_HEADER_API 15 #include <string_view> 16 #include <type_traits> 32 #ifdef U_ALIASING_BARRIER 33 // Use the predefined value. 34 #elif (defined(__clang__) || defined(__GNUC__)) && U_PLATFORM != U_PF_BROWSER_NATIVE_CLIENT 35 # define U_ALIASING_BARRIER(ptr) asm volatile("" : : "rm"(ptr) : "memory")
36 #elif defined(U_IN_DOXYGEN) 37 # define U_ALIASING_BARRIER(ptr) 41 #if U_SHOW_CPLUSPLUS_API 57 #if !U_CHAR16_IS_TYPEDEF 65 #if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN) 91 inline char16_t *
get()
const;
97 inlineoperator char16_t *()
const {
return get(); }
102 #ifdef U_ALIASING_BARRIER 103 template<
typename T>
static char16_t *cast(T *t) {
105 returnreinterpret_cast<char16_t *
>(t);
119 #ifdef U_ALIASING_BARRIER 121 Char16Ptr::Char16Ptr(char16_t *p) : p_(p) {}
122 #if !U_CHAR16_IS_TYPEDEF 123 Char16Ptr::Char16Ptr(uint16_t *p) : p_(cast(p)) {}
125 #if U_SIZEOF_WCHAR_T==2 126 Char16Ptr::Char16Ptr(
wchar_t *p) : p_(cast(p)) {}
128 Char16Ptr::Char16Ptr(std::nullptr_t p) : p_(p) {}
137 Char16Ptr::Char16Ptr(char16_t *p) { u_.cp = p; }
138 #if !U_CHAR16_IS_TYPEDEF 139 Char16Ptr::Char16Ptr(uint16_t *p) { u_.up = p; }
141 #if U_SIZEOF_WCHAR_T==2 142 Char16Ptr::Char16Ptr(
wchar_t *p) { u_.wp = p; }
144 Char16Ptr::Char16Ptr(std::nullptr_t p) { u_.cp = p; }
164 #if !U_CHAR16_IS_TYPEDEF 172 #if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN) 199 inlineconst char16_t *
get()
const;
205 inlineoperatorconst char16_t *()
const {
return get(); }
210 #ifdef U_ALIASING_BARRIER 211 template<
typename T>
staticconst char16_t *cast(
const T *t) {
213 returnreinterpret_cast<constchar16_t *
>(t);
227 #ifdef U_ALIASING_BARRIER 229 ConstChar16Ptr::ConstChar16Ptr(
const char16_t *p) : p_(p) {}
230 #if !U_CHAR16_IS_TYPEDEF 231 ConstChar16Ptr::ConstChar16Ptr(
const uint16_t *p) : p_(cast(p)) {}
233 #if U_SIZEOF_WCHAR_T==2 234 ConstChar16Ptr::ConstChar16Ptr(
constwchar_t *p) : p_(cast(p)) {}
236 ConstChar16Ptr::ConstChar16Ptr(
const std::nullptr_t p) : p_(p) {}
245 ConstChar16Ptr::ConstChar16Ptr(
const char16_t *p) { u_.cp = p; }
246 #if !U_CHAR16_IS_TYPEDEF 247 ConstChar16Ptr::ConstChar16Ptr(
const uint16_t *p) { u_.up = p; }
249 #if U_SIZEOF_WCHAR_T==2 250 ConstChar16Ptr::ConstChar16Ptr(
constwchar_t *p) { u_.wp = p; }
252 ConstChar16Ptr::ConstChar16Ptr(
const std::nullptr_t p) { u_.cp = p; }
262 #endif// U_SHOW_CPLUSPLUS_API 264 // Usable in header-only definitions 265 #if U_SHOW_CPLUSPLUS_API || U_SHOW_CPLUSPLUS_HEADER_API 267 namespaceU_ICU_NAMESPACE_OR_INTERNAL {
269 #ifndef U_FORCE_HIDE_INTERNAL_API 271 template<
typename T,
typename = std::enable_if_t<std::is_same_v<T, UChar>>>
273 if constexpr (std::is_same_v<UChar, char16_t>) {
276 #if U_SHOW_CPLUSPLUS_API 277 return ConstChar16Ptr(p).get();
279 #ifdef U_ALIASING_BARRIER 282 returnreinterpret_cast<constchar16_t *
>(p);
286 #if !U_CHAR16_IS_TYPEDEF && (!defined(_LIBCPP_VERSION) || _LIBCPP_VERSION < 180000) 289 #if U_SHOW_CPLUSPLUS_API 290 return ConstChar16Ptr(p).get();
292 #ifdef U_ALIASING_BARRIER 295 returnreinterpret_cast<constchar16_t *
>(p);
299 #if U_SIZEOF_WCHAR_T==2 301 inlineconst char16_t *uprv_char16PtrFromWchar(
constwchar_t *p) {
302 #if U_SHOW_CPLUSPLUS_API 303 return ConstChar16Ptr(p).get();
305 #ifdef U_ALIASING_BARRIER 308 returnreinterpret_cast<constchar16_t *
>(p);
322 #ifdef U_ALIASING_BARRIER 325 returnreinterpret_cast<constUChar *
>(p);
336 #ifdef U_ALIASING_BARRIER 339 returnreinterpret_cast<UChar *
>(p);
350 #ifdef U_ALIASING_BARRIER 353 returnreinterpret_cast<constOldUChar *
>(p);
364 #ifdef U_ALIASING_BARRIER 367 returnreinterpret_cast<OldUChar *
>(p);
370 }
// U_ICU_NAMESPACE_OR_INTERNAL 372 #endif// U_SHOW_CPLUSPLUS_API || U_SHOW_CPLUSPLUS_HEADER_API 375 #if U_SHOW_CPLUSPLUS_API 379 #ifndef U_FORCE_HIDE_INTERNAL_API 386 std::is_convertible_v<T, std::u16string_view>
387 #if !U_CHAR16_IS_TYPEDEF && (!defined(_LIBCPP_VERSION) || _LIBCPP_VERSION < 180000) 388 || std::is_convertible_v<T, std::basic_string_view<uint16_t>>
390 #if U_SIZEOF_WCHAR_T==2 391 || std::is_convertible_v<T, std::wstring_view>
402 #if !U_CHAR16_IS_TYPEDEF && (!defined(_LIBCPP_VERSION) || _LIBCPP_VERSION < 180000) 413 #if U_SIZEOF_WCHAR_T==2 419 inline std::u16string_view toU16StringView(std::wstring_view sv) {
428 template <
typename T,
429 typename =
typename std::enable_if_t<!std::is_pointer_v<std::remove_reference_t<T>>>>
438 template <
typename T,
439 typename =
typename std::enable_if_t<std::is_pointer_v<std::remove_reference_t<T>>>,
442 if (text ==
nullptr)
return {};
// For backward compatibility. 447 #endif// U_FORCE_HIDE_INTERNAL_API 451 #endif// U_SHOW_CPLUSPLUS_API 453 #endif// __CHAR16PTR_H__ const char16_t * uprv_char16PtrFromUint16(const uint16_t *p)
std::u16string_view toU16StringView(std::u16string_view sv)
Pass-through overload.
const UChar * toUCharPtr(const char16_t *p)
Converts from const char16_t * to const UChar *.
const char16_t * uprv_char16PtrFromUChar(const T *p)
std::u16string_view toU16StringViewNullable(const T &text)
Pass-through overload.
#define U_ALIASING_BARRIER(ptr)
Barrier for pointer anti-aliasing optimizations even across function boundaries.
const OldUChar * toOldUCharPtr(const char16_t *p)
Converts from const char16_t * to const OldUChar *.
char16_t * wrapper with implicit conversion from distinct but bit-compatible pointer types.
Char16Ptr(std::nullptr_t p)
nullptr constructor.
Char16Ptr(uint16_t *p)
Converts the pointer to char16_t *.
Char16Ptr(wchar_t *p)
Converts the pointer to char16_t *.
char16_t * get() const
Pointer access.
Char16Ptr(char16_t *p)
Copies the pointer.
const char16_t * wrapper with implicit conversion from distinct but bit-compatible pointer types.
ConstChar16Ptr(const char16_t *p)
Copies the pointer.
const char16_t * get() const
Pointer access.
ConstChar16Ptr(const uint16_t *p)
Converts the pointer to char16_t *.
~ConstChar16Ptr()
Destructor.
ConstChar16Ptr(const wchar_t *p)
Converts the pointer to char16_t *.
ConstChar16Ptr(const std::nullptr_t p)
nullptr constructor.
constexpr bool ConvertibleToU16StringView
Is T convertible to a std::u16string_view or some other 16-bit string view?
char16_t UChar
The base type for UTF-16 code units and pointers.
uint16_t OldUChar
Default ICU 58 definition of UChar.
Basic definitions for ICU, for both C and C++ APIs.
#define U_COMMON_API
Set to export library symbols from inside the common library, and to import them from outside.