Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/native/common/unicode/char16ptr.h
38827 views
// © 2017 and later: Unicode, Inc. and others.1// License & terms of use: http://www.unicode.org/copyright.html23// char16ptr.h4// created: 2017feb28 Markus W. Scherer56#ifndef __CHAR16PTR_H__7#define __CHAR16PTR_H__89#include "unicode/utypes.h"1011#if U_SHOW_CPLUSPLUS_API1213#include <cstddef>1415/**16* \file17* \brief C++ API: char16_t pointer wrappers with18* implicit conversion from bit-compatible raw pointer types.19* Also conversion functions from char16_t * to UChar * and OldUChar *.20*/2122U_NAMESPACE_BEGIN2324/**25* \def U_ALIASING_BARRIER26* Barrier for pointer anti-aliasing optimizations even across function boundaries.27* @internal28*/29#ifdef U_ALIASING_BARRIER30// Use the predefined value.31#elif (defined(__clang__) || defined(__GNUC__)) && U_PLATFORM != U_PF_BROWSER_NATIVE_CLIENT32# define U_ALIASING_BARRIER(ptr) asm volatile("" : : "rm"(ptr) : "memory")33#elif defined(U_IN_DOXYGEN)34# define U_ALIASING_BARRIER(ptr)35#endif3637/**38* char16_t * wrapper with implicit conversion from distinct but bit-compatible pointer types.39* @stable ICU 5940*/41class U_COMMON_API Char16Ptr U_FINAL {42public:43/**44* Copies the pointer.45* @param p pointer46* @stable ICU 5947*/48inline Char16Ptr(char16_t *p);49#if !U_CHAR16_IS_TYPEDEF50/**51* Converts the pointer to char16_t *.52* @param p pointer to be converted53* @stable ICU 5954*/55inline Char16Ptr(uint16_t *p);56#endif57#if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN)58/**59* Converts the pointer to char16_t *.60* (Only defined if U_SIZEOF_WCHAR_T==2.)61* @param p pointer to be converted62* @stable ICU 5963*/64inline Char16Ptr(wchar_t *p);65#endif66/**67* nullptr constructor.68* @param p nullptr69* @stable ICU 5970*/71inline Char16Ptr(std::nullptr_t p);72/**73* Destructor.74* @stable ICU 5975*/76inline ~Char16Ptr();7778/**79* Pointer access.80* @return the wrapped pointer81* @stable ICU 5982*/83inline char16_t *get() const;84/**85* char16_t pointer access via type conversion (e.g., static_cast).86* @return the wrapped pointer87* @stable ICU 5988*/89inline operator char16_t *() const { return get(); }9091private:92Char16Ptr() = delete;9394#ifdef U_ALIASING_BARRIER95template<typename T> static char16_t *cast(T *t) {96U_ALIASING_BARRIER(t);97return reinterpret_cast<char16_t *>(t);98}99100char16_t *p_;101#else102union {103char16_t *cp;104uint16_t *up;105wchar_t *wp;106} u_;107#endif108};109110/// \cond111#ifdef U_ALIASING_BARRIER112113Char16Ptr::Char16Ptr(char16_t *p) : p_(p) {}114#if !U_CHAR16_IS_TYPEDEF115Char16Ptr::Char16Ptr(uint16_t *p) : p_(cast(p)) {}116#endif117#if U_SIZEOF_WCHAR_T==2118Char16Ptr::Char16Ptr(wchar_t *p) : p_(cast(p)) {}119#endif120Char16Ptr::Char16Ptr(std::nullptr_t p) : p_(p) {}121Char16Ptr::~Char16Ptr() {122U_ALIASING_BARRIER(p_);123}124125char16_t *Char16Ptr::get() const { return p_; }126127#else128129Char16Ptr::Char16Ptr(char16_t *p) { u_.cp = p; }130#if !U_CHAR16_IS_TYPEDEF131Char16Ptr::Char16Ptr(uint16_t *p) { u_.up = p; }132#endif133#if U_SIZEOF_WCHAR_T==2134Char16Ptr::Char16Ptr(wchar_t *p) { u_.wp = p; }135#endif136Char16Ptr::Char16Ptr(std::nullptr_t p) { u_.cp = p; }137Char16Ptr::~Char16Ptr() {}138139char16_t *Char16Ptr::get() const { return u_.cp; }140141#endif142/// \endcond143144/**145* const char16_t * wrapper with implicit conversion from distinct but bit-compatible pointer types.146* @stable ICU 59147*/148class U_COMMON_API ConstChar16Ptr U_FINAL {149public:150/**151* Copies the pointer.152* @param p pointer153* @stable ICU 59154*/155inline ConstChar16Ptr(const char16_t *p);156#if !U_CHAR16_IS_TYPEDEF157/**158* Converts the pointer to char16_t *.159* @param p pointer to be converted160* @stable ICU 59161*/162inline ConstChar16Ptr(const uint16_t *p);163#endif164#if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN)165/**166* Converts the pointer to char16_t *.167* (Only defined if U_SIZEOF_WCHAR_T==2.)168* @param p pointer to be converted169* @stable ICU 59170*/171inline ConstChar16Ptr(const wchar_t *p);172#endif173/**174* nullptr constructor.175* @param p nullptr176* @stable ICU 59177*/178inline ConstChar16Ptr(const std::nullptr_t p);179180/**181* Destructor.182* @stable ICU 59183*/184inline ~ConstChar16Ptr();185186/**187* Pointer access.188* @return the wrapped pointer189* @stable ICU 59190*/191inline const char16_t *get() const;192/**193* char16_t pointer access via type conversion (e.g., static_cast).194* @return the wrapped pointer195* @stable ICU 59196*/197inline operator const char16_t *() const { return get(); }198199private:200ConstChar16Ptr() = delete;201202#ifdef U_ALIASING_BARRIER203template<typename T> static const char16_t *cast(const T *t) {204U_ALIASING_BARRIER(t);205return reinterpret_cast<const char16_t *>(t);206}207208const char16_t *p_;209#else210union {211const char16_t *cp;212const uint16_t *up;213const wchar_t *wp;214} u_;215#endif216};217218/// \cond219#ifdef U_ALIASING_BARRIER220221ConstChar16Ptr::ConstChar16Ptr(const char16_t *p) : p_(p) {}222#if !U_CHAR16_IS_TYPEDEF223ConstChar16Ptr::ConstChar16Ptr(const uint16_t *p) : p_(cast(p)) {}224#endif225#if U_SIZEOF_WCHAR_T==2226ConstChar16Ptr::ConstChar16Ptr(const wchar_t *p) : p_(cast(p)) {}227#endif228ConstChar16Ptr::ConstChar16Ptr(const std::nullptr_t p) : p_(p) {}229ConstChar16Ptr::~ConstChar16Ptr() {230U_ALIASING_BARRIER(p_);231}232233const char16_t *ConstChar16Ptr::get() const { return p_; }234235#else236237ConstChar16Ptr::ConstChar16Ptr(const char16_t *p) { u_.cp = p; }238#if !U_CHAR16_IS_TYPEDEF239ConstChar16Ptr::ConstChar16Ptr(const uint16_t *p) { u_.up = p; }240#endif241#if U_SIZEOF_WCHAR_T==2242ConstChar16Ptr::ConstChar16Ptr(const wchar_t *p) { u_.wp = p; }243#endif244ConstChar16Ptr::ConstChar16Ptr(const std::nullptr_t p) { u_.cp = p; }245ConstChar16Ptr::~ConstChar16Ptr() {}246247const char16_t *ConstChar16Ptr::get() const { return u_.cp; }248249#endif250/// \endcond251252/**253* Converts from const char16_t * to const UChar *.254* Includes an aliasing barrier if available.255* @param p pointer256* @return p as const UChar *257* @stable ICU 59258*/259inline const UChar *toUCharPtr(const char16_t *p) {260#ifdef U_ALIASING_BARRIER261U_ALIASING_BARRIER(p);262#endif263return reinterpret_cast<const UChar *>(p);264}265266/**267* Converts from char16_t * to UChar *.268* Includes an aliasing barrier if available.269* @param p pointer270* @return p as UChar *271* @stable ICU 59272*/273inline UChar *toUCharPtr(char16_t *p) {274#ifdef U_ALIASING_BARRIER275U_ALIASING_BARRIER(p);276#endif277return reinterpret_cast<UChar *>(p);278}279280/**281* Converts from const char16_t * to const OldUChar *.282* Includes an aliasing barrier if available.283* @param p pointer284* @return p as const OldUChar *285* @stable ICU 59286*/287inline const OldUChar *toOldUCharPtr(const char16_t *p) {288#ifdef U_ALIASING_BARRIER289U_ALIASING_BARRIER(p);290#endif291return reinterpret_cast<const OldUChar *>(p);292}293294/**295* Converts from char16_t * to OldUChar *.296* Includes an aliasing barrier if available.297* @param p pointer298* @return p as OldUChar *299* @stable ICU 59300*/301inline OldUChar *toOldUCharPtr(char16_t *p) {302#ifdef U_ALIASING_BARRIER303U_ALIASING_BARRIER(p);304#endif305return reinterpret_cast<OldUChar *>(p);306}307308U_NAMESPACE_END309310#endif /* U_SHOW_CPLUSPLUS_API */311312#endif // __CHAR16PTR_H__313314315