Path: blob/jdk8u272-b10-aarch32-20201026/jdk/src/share/native/common/unicode/char16ptr.h
48711 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 <cstddef>10#include "unicode/utypes.h"1112/**13* \file14* \brief C++ API: char16_t pointer wrappers with15* implicit conversion from bit-compatible raw pointer types.16* Also conversion functions from char16_t * to UChar * and OldUChar *.17*/1819U_NAMESPACE_BEGIN2021/**22* \def U_ALIASING_BARRIER23* Barrier for pointer anti-aliasing optimizations even across function boundaries.24* @internal25*/26#ifdef U_ALIASING_BARRIER27// Use the predefined value.28#elif (defined(__clang__) || defined(__GNUC__)) && U_PLATFORM != U_PF_BROWSER_NATIVE_CLIENT29# define U_ALIASING_BARRIER(ptr) asm volatile("" : : "rm"(ptr) : "memory")30#elif defined(U_IN_DOXYGEN)31# define U_ALIASING_BARRIER(ptr)32#endif3334/**35* char16_t * wrapper with implicit conversion from distinct but bit-compatible pointer types.36* @stable ICU 5937*/38class U_COMMON_API Char16Ptr U_FINAL {39public:40/**41* Copies the pointer.42* @param p pointer43* @stable ICU 5944*/45inline Char16Ptr(char16_t *p);46#if !U_CHAR16_IS_TYPEDEF47/**48* Converts the pointer to char16_t *.49* @param p pointer to be converted50* @stable ICU 5951*/52inline Char16Ptr(uint16_t *p);53#endif54#if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN)55/**56* Converts the pointer to char16_t *.57* (Only defined if U_SIZEOF_WCHAR_T==2.)58* @param p pointer to be converted59* @stable ICU 5960*/61inline Char16Ptr(wchar_t *p);62#endif63/**64* nullptr constructor.65* @param p nullptr66* @stable ICU 5967*/68inline Char16Ptr(std::nullptr_t p);69/**70* Destructor.71* @stable ICU 5972*/73inline ~Char16Ptr();7475/**76* Pointer access.77* @return the wrapped pointer78* @stable ICU 5979*/80inline char16_t *get() const;81/**82* char16_t pointer access via type conversion (e.g., static_cast).83* @return the wrapped pointer84* @stable ICU 5985*/86inline operator char16_t *() const { return get(); }8788private:89Char16Ptr() = delete;9091#ifdef U_ALIASING_BARRIER92template<typename T> static char16_t *cast(T *t) {93U_ALIASING_BARRIER(t);94return reinterpret_cast<char16_t *>(t);95}9697char16_t *p_;98#else99union {100char16_t *cp;101uint16_t *up;102wchar_t *wp;103} u_;104#endif105};106107/// \cond108#ifdef U_ALIASING_BARRIER109110Char16Ptr::Char16Ptr(char16_t *p) : p_(p) {}111#if !U_CHAR16_IS_TYPEDEF112Char16Ptr::Char16Ptr(uint16_t *p) : p_(cast(p)) {}113#endif114#if U_SIZEOF_WCHAR_T==2115Char16Ptr::Char16Ptr(wchar_t *p) : p_(cast(p)) {}116#endif117Char16Ptr::Char16Ptr(std::nullptr_t p) : p_(p) {}118Char16Ptr::~Char16Ptr() {119U_ALIASING_BARRIER(p_);120}121122char16_t *Char16Ptr::get() const { return p_; }123124#else125126Char16Ptr::Char16Ptr(char16_t *p) { u_.cp = p; }127#if !U_CHAR16_IS_TYPEDEF128Char16Ptr::Char16Ptr(uint16_t *p) { u_.up = p; }129#endif130#if U_SIZEOF_WCHAR_T==2131Char16Ptr::Char16Ptr(wchar_t *p) { u_.wp = p; }132#endif133Char16Ptr::Char16Ptr(std::nullptr_t p) { u_.cp = p; }134Char16Ptr::~Char16Ptr() {}135136char16_t *Char16Ptr::get() const { return u_.cp; }137138#endif139/// \endcond140141/**142* const char16_t * wrapper with implicit conversion from distinct but bit-compatible pointer types.143* @stable ICU 59144*/145class U_COMMON_API ConstChar16Ptr U_FINAL {146public:147/**148* Copies the pointer.149* @param p pointer150* @stable ICU 59151*/152inline ConstChar16Ptr(const char16_t *p);153#if !U_CHAR16_IS_TYPEDEF154/**155* Converts the pointer to char16_t *.156* @param p pointer to be converted157* @stable ICU 59158*/159inline ConstChar16Ptr(const uint16_t *p);160#endif161#if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN)162/**163* Converts the pointer to char16_t *.164* (Only defined if U_SIZEOF_WCHAR_T==2.)165* @param p pointer to be converted166* @stable ICU 59167*/168inline ConstChar16Ptr(const wchar_t *p);169#endif170/**171* nullptr constructor.172* @param p nullptr173* @stable ICU 59174*/175inline ConstChar16Ptr(const std::nullptr_t p);176177/**178* Destructor.179* @stable ICU 59180*/181inline ~ConstChar16Ptr();182183/**184* Pointer access.185* @return the wrapped pointer186* @stable ICU 59187*/188inline const char16_t *get() const;189/**190* char16_t pointer access via type conversion (e.g., static_cast).191* @return the wrapped pointer192* @stable ICU 59193*/194inline operator const char16_t *() const { return get(); }195196private:197ConstChar16Ptr() = delete;198199#ifdef U_ALIASING_BARRIER200template<typename T> static const char16_t *cast(const T *t) {201U_ALIASING_BARRIER(t);202return reinterpret_cast<const char16_t *>(t);203}204205const char16_t *p_;206#else207union {208const char16_t *cp;209const uint16_t *up;210const wchar_t *wp;211} u_;212#endif213};214215/// \cond216#ifdef U_ALIASING_BARRIER217218ConstChar16Ptr::ConstChar16Ptr(const char16_t *p) : p_(p) {}219#if !U_CHAR16_IS_TYPEDEF220ConstChar16Ptr::ConstChar16Ptr(const uint16_t *p) : p_(cast(p)) {}221#endif222#if U_SIZEOF_WCHAR_T==2223ConstChar16Ptr::ConstChar16Ptr(const wchar_t *p) : p_(cast(p)) {}224#endif225ConstChar16Ptr::ConstChar16Ptr(const std::nullptr_t p) : p_(p) {}226ConstChar16Ptr::~ConstChar16Ptr() {227U_ALIASING_BARRIER(p_);228}229230const char16_t *ConstChar16Ptr::get() const { return p_; }231232#else233234ConstChar16Ptr::ConstChar16Ptr(const char16_t *p) { u_.cp = p; }235#if !U_CHAR16_IS_TYPEDEF236ConstChar16Ptr::ConstChar16Ptr(const uint16_t *p) { u_.up = p; }237#endif238#if U_SIZEOF_WCHAR_T==2239ConstChar16Ptr::ConstChar16Ptr(const wchar_t *p) { u_.wp = p; }240#endif241ConstChar16Ptr::ConstChar16Ptr(const std::nullptr_t p) { u_.cp = p; }242ConstChar16Ptr::~ConstChar16Ptr() {}243244const char16_t *ConstChar16Ptr::get() const { return u_.cp; }245246#endif247/// \endcond248249/**250* Converts from const char16_t * to const UChar *.251* Includes an aliasing barrier if available.252* @param p pointer253* @return p as const UChar *254* @stable ICU 59255*/256inline const UChar *toUCharPtr(const char16_t *p) {257#ifdef U_ALIASING_BARRIER258U_ALIASING_BARRIER(p);259#endif260return reinterpret_cast<const UChar *>(p);261}262263/**264* Converts from char16_t * to UChar *.265* Includes an aliasing barrier if available.266* @param p pointer267* @return p as UChar *268* @stable ICU 59269*/270inline UChar *toUCharPtr(char16_t *p) {271#ifdef U_ALIASING_BARRIER272U_ALIASING_BARRIER(p);273#endif274return reinterpret_cast<UChar *>(p);275}276277/**278* Converts from const char16_t * to const OldUChar *.279* Includes an aliasing barrier if available.280* @param p pointer281* @return p as const OldUChar *282* @stable ICU 59283*/284inline const OldUChar *toOldUCharPtr(const char16_t *p) {285#ifdef U_ALIASING_BARRIER286U_ALIASING_BARRIER(p);287#endif288return reinterpret_cast<const OldUChar *>(p);289}290291/**292* Converts from char16_t * to OldUChar *.293* Includes an aliasing barrier if available.294* @param p pointer295* @return p as OldUChar *296* @stable ICU 59297*/298inline OldUChar *toOldUCharPtr(char16_t *p) {299#ifdef U_ALIASING_BARRIER300U_ALIASING_BARRIER(p);301#endif302return reinterpret_cast<OldUChar *>(p);303}304305U_NAMESPACE_END306307#endif // __CHAR16PTR_H__308309310