ICU 73.2  73.2
char16ptr.h
Go to the documentation of this file.
00001 // © 2017 and later: Unicode, Inc. and others.
00002 // License & terms of use: http://www.unicode.org/copyright.html
00003 
00004 // char16ptr.h
00005 // created: 2017feb28 Markus W. Scherer
00006 
00007 #ifndef __CHAR16PTR_H__
00008 #define __CHAR16PTR_H__
00009 
00010 #include "unicode/utypes.h"
00011 
00012 #if U_SHOW_CPLUSPLUS_API
00013 
00014 #include <cstddef>
00015 
00023 U_NAMESPACE_BEGIN
00024 
00030 #ifdef U_ALIASING_BARRIER
00031     // Use the predefined value.
00032 #elif (defined(__clang__) || defined(__GNUC__)) && U_PLATFORM != U_PF_BROWSER_NATIVE_CLIENT
00033 #   define U_ALIASING_BARRIER(ptr) asm volatile("" : : "rm"(ptr) : "memory")
00034 #elif defined(U_IN_DOXYGEN)
00035 #   define U_ALIASING_BARRIER(ptr)
00036 #endif
00037 
00042 class U_COMMON_API Char16Ptr final {
00043 public:
00049     inline Char16Ptr(char16_t *p);
00050 #if !U_CHAR16_IS_TYPEDEF
00051 
00056     inline Char16Ptr(uint16_t *p);
00057 #endif
00058 #if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN)
00059 
00065     inline Char16Ptr(wchar_t *p);
00066 #endif
00067 
00072     inline Char16Ptr(std::nullptr_t p);
00077     inline ~Char16Ptr();
00078 
00084     inline char16_t *get() const;
00090     inline operator char16_t *() const { return get(); }
00091 
00092 private:
00093     Char16Ptr() = delete;
00094 
00095 #ifdef U_ALIASING_BARRIER
00096     template<typename T> static char16_t *cast(T *t) {
00097         U_ALIASING_BARRIER(t);
00098         return reinterpret_cast<char16_t *>(t);
00099     }
00100 
00101     char16_t *p_;
00102 #else
00103     union {
00104         char16_t *cp;
00105         uint16_t *up;
00106         wchar_t *wp;
00107     } u_;
00108 #endif
00109 };
00110 
00112 #ifdef U_ALIASING_BARRIER
00113 
00114 Char16Ptr::Char16Ptr(char16_t *p) : p_(p) {}
00115 #if !U_CHAR16_IS_TYPEDEF
00116 Char16Ptr::Char16Ptr(uint16_t *p) : p_(cast(p)) {}
00117 #endif
00118 #if U_SIZEOF_WCHAR_T==2
00119 Char16Ptr::Char16Ptr(wchar_t *p) : p_(cast(p)) {}
00120 #endif
00121 Char16Ptr::Char16Ptr(std::nullptr_t p) : p_(p) {}
00122 Char16Ptr::~Char16Ptr() {
00123     U_ALIASING_BARRIER(p_);
00124 }
00125 
00126 char16_t *Char16Ptr::get() const { return p_; }
00127 
00128 #else
00129 
00130 Char16Ptr::Char16Ptr(char16_t *p) { u_.cp = p; }
00131 #if !U_CHAR16_IS_TYPEDEF
00132 Char16Ptr::Char16Ptr(uint16_t *p) { u_.up = p; }
00133 #endif
00134 #if U_SIZEOF_WCHAR_T==2
00135 Char16Ptr::Char16Ptr(wchar_t *p) { u_.wp = p; }
00136 #endif
00137 Char16Ptr::Char16Ptr(std::nullptr_t p) { u_.cp = p; }
00138 Char16Ptr::~Char16Ptr() {}
00139 
00140 char16_t *Char16Ptr::get() const { return u_.cp; }
00141 
00142 #endif
00143 
00144 
00149 class U_COMMON_API ConstChar16Ptr final {
00150 public:
00156     inline ConstChar16Ptr(const char16_t *p);
00157 #if !U_CHAR16_IS_TYPEDEF
00158 
00163     inline ConstChar16Ptr(const uint16_t *p);
00164 #endif
00165 #if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN)
00166 
00172     inline ConstChar16Ptr(const wchar_t *p);
00173 #endif
00174 
00179     inline ConstChar16Ptr(const std::nullptr_t p);
00180 
00185     inline ~ConstChar16Ptr();
00186 
00192     inline const char16_t *get() const;
00198     inline operator const char16_t *() const { return get(); }
00199 
00200 private:
00201     ConstChar16Ptr() = delete;
00202 
00203 #ifdef U_ALIASING_BARRIER
00204     template<typename T> static const char16_t *cast(const T *t) {
00205         U_ALIASING_BARRIER(t);
00206         return reinterpret_cast<const char16_t *>(t);
00207     }
00208 
00209     const char16_t *p_;
00210 #else
00211     union {
00212         const char16_t *cp;
00213         const uint16_t *up;
00214         const wchar_t *wp;
00215     } u_;
00216 #endif
00217 };
00218 
00220 #ifdef U_ALIASING_BARRIER
00221 
00222 ConstChar16Ptr::ConstChar16Ptr(const char16_t *p) : p_(p) {}
00223 #if !U_CHAR16_IS_TYPEDEF
00224 ConstChar16Ptr::ConstChar16Ptr(const uint16_t *p) : p_(cast(p)) {}
00225 #endif
00226 #if U_SIZEOF_WCHAR_T==2
00227 ConstChar16Ptr::ConstChar16Ptr(const wchar_t *p) : p_(cast(p)) {}
00228 #endif
00229 ConstChar16Ptr::ConstChar16Ptr(const std::nullptr_t p) : p_(p) {}
00230 ConstChar16Ptr::~ConstChar16Ptr() {
00231     U_ALIASING_BARRIER(p_);
00232 }
00233 
00234 const char16_t *ConstChar16Ptr::get() const { return p_; }
00235 
00236 #else
00237 
00238 ConstChar16Ptr::ConstChar16Ptr(const char16_t *p) { u_.cp = p; }
00239 #if !U_CHAR16_IS_TYPEDEF
00240 ConstChar16Ptr::ConstChar16Ptr(const uint16_t *p) { u_.up = p; }
00241 #endif
00242 #if U_SIZEOF_WCHAR_T==2
00243 ConstChar16Ptr::ConstChar16Ptr(const wchar_t *p) { u_.wp = p; }
00244 #endif
00245 ConstChar16Ptr::ConstChar16Ptr(const std::nullptr_t p) { u_.cp = p; }
00246 ConstChar16Ptr::~ConstChar16Ptr() {}
00247 
00248 const char16_t *ConstChar16Ptr::get() const { return u_.cp; }
00249 
00250 #endif
00251 
00252 
00260 inline const UChar *toUCharPtr(const char16_t *p) {
00261 #ifdef U_ALIASING_BARRIER
00262     U_ALIASING_BARRIER(p);
00263 #endif
00264     return reinterpret_cast<const UChar *>(p);
00265 }
00266 
00274 inline UChar *toUCharPtr(char16_t *p) {
00275 #ifdef U_ALIASING_BARRIER
00276     U_ALIASING_BARRIER(p);
00277 #endif
00278     return reinterpret_cast<UChar *>(p);
00279 }
00280 
00288 inline const OldUChar *toOldUCharPtr(const char16_t *p) {
00289 #ifdef U_ALIASING_BARRIER
00290     U_ALIASING_BARRIER(p);
00291 #endif
00292     return reinterpret_cast<const OldUChar *>(p);
00293 }
00294 
00302 inline OldUChar *toOldUCharPtr(char16_t *p) {
00303 #ifdef U_ALIASING_BARRIER
00304     U_ALIASING_BARRIER(p);
00305 #endif
00306     return reinterpret_cast<OldUChar *>(p);
00307 }
00308 
00309 U_NAMESPACE_END
00310 
00311 #endif /* U_SHOW_CPLUSPLUS_API */
00312 
00313 #endif  // __CHAR16PTR_H__