00001
00002
00003
00004
00005
00006
00007 #ifndef __CHAR16PTR_H__
00008 #define __CHAR16PTR_H__
00009
00010 #include <cstddef>
00011 #include "unicode/utypes.h"
00012
00020 U_NAMESPACE_BEGIN
00021
00027 #ifdef U_ALIASING_BARRIER
00028
00029 #elif (defined(__clang__) || defined(__GNUC__)) && U_PLATFORM != U_PF_BROWSER_NATIVE_CLIENT
00030 # define U_ALIASING_BARRIER(ptr) asm volatile("" : : "rm"(ptr) : "memory")
00031 #endif
00032
00033
00034
00039 class U_COMMON_API Char16Ptr U_FINAL {
00040 public:
00046 inline Char16Ptr(char16_t *p);
00047 #if !U_CHAR16_IS_TYPEDEF
00048
00053 inline Char16Ptr(uint16_t *p);
00054 #endif
00055 #if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN)
00056
00062 inline Char16Ptr(wchar_t *p);
00063 #endif
00064
00069 inline Char16Ptr(std::nullptr_t p);
00074 inline ~Char16Ptr();
00075
00081 inline char16_t *get() const;
00087 inline operator char16_t *() const { return get(); }
00088
00089 private:
00090 Char16Ptr() = delete;
00091
00092 #ifdef U_ALIASING_BARRIER
00093 template<typename T> static char16_t *cast(T *t) {
00094 U_ALIASING_BARRIER(t);
00095 return reinterpret_cast<char16_t *>(t);
00096 }
00097
00098 char16_t *p_;
00099 #else
00100 union {
00101 char16_t *cp;
00102 uint16_t *up;
00103 wchar_t *wp;
00104 } u_;
00105 #endif
00106 };
00107
00108 #ifdef U_ALIASING_BARRIER
00109
00110 Char16Ptr::Char16Ptr(char16_t *p) : p_(p) {}
00111 #if !U_CHAR16_IS_TYPEDEF
00112 Char16Ptr::Char16Ptr(uint16_t *p) : p_(cast(p)) {}
00113 #endif
00114 #if U_SIZEOF_WCHAR_T==2
00115 Char16Ptr::Char16Ptr(wchar_t *p) : p_(cast(p)) {}
00116 #endif
00117 Char16Ptr::Char16Ptr(std::nullptr_t p) : p_(p) {}
00118 Char16Ptr::~Char16Ptr() {
00119 U_ALIASING_BARRIER(p_);
00120 }
00121
00122 char16_t *Char16Ptr::get() const { return p_; }
00123
00124 #else
00125
00126 Char16Ptr::Char16Ptr(char16_t *p) { u_.cp = p; }
00127 #if !U_CHAR16_IS_TYPEDEF
00128 Char16Ptr::Char16Ptr(uint16_t *p) { u_.up = p; }
00129 #endif
00130 #if U_SIZEOF_WCHAR_T==2
00131 Char16Ptr::Char16Ptr(wchar_t *p) { u_.wp = p; }
00132 #endif
00133 Char16Ptr::Char16Ptr(std::nullptr_t p) { u_.cp = p; }
00134 Char16Ptr::~Char16Ptr() {}
00135
00136 char16_t *Char16Ptr::get() const { return u_.cp; }
00137
00138 #endif
00139
00140
00141
00146 class U_COMMON_API ConstChar16Ptr U_FINAL {
00147 public:
00153 inline ConstChar16Ptr(const char16_t *p);
00154 #if !U_CHAR16_IS_TYPEDEF
00155
00160 inline ConstChar16Ptr(const uint16_t *p);
00161 #endif
00162 #if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN)
00163
00169 inline ConstChar16Ptr(const wchar_t *p);
00170 #endif
00171
00176 inline ConstChar16Ptr(const std::nullptr_t p);
00177
00182 inline ~ConstChar16Ptr();
00183
00189 inline const char16_t *get() const;
00195 inline operator const char16_t *() const { return get(); }
00196
00197 private:
00198 ConstChar16Ptr() = delete;
00199
00200 #ifdef U_ALIASING_BARRIER
00201 template<typename T> static const char16_t *cast(const T *t) {
00202 U_ALIASING_BARRIER(t);
00203 return reinterpret_cast<const char16_t *>(t);
00204 }
00205
00206 const char16_t *p_;
00207 #else
00208 union {
00209 const char16_t *cp;
00210 const uint16_t *up;
00211 const wchar_t *wp;
00212 } u_;
00213 #endif
00214 };
00215
00216 #ifdef U_ALIASING_BARRIER
00217
00218 ConstChar16Ptr::ConstChar16Ptr(const char16_t *p) : p_(p) {}
00219 #if !U_CHAR16_IS_TYPEDEF
00220 ConstChar16Ptr::ConstChar16Ptr(const uint16_t *p) : p_(cast(p)) {}
00221 #endif
00222 #if U_SIZEOF_WCHAR_T==2
00223 ConstChar16Ptr::ConstChar16Ptr(const wchar_t *p) : p_(cast(p)) {}
00224 #endif
00225 ConstChar16Ptr::ConstChar16Ptr(const std::nullptr_t p) : p_(p) {}
00226 ConstChar16Ptr::~ConstChar16Ptr() {
00227 U_ALIASING_BARRIER(p_);
00228 }
00229
00230 const char16_t *ConstChar16Ptr::get() const { return p_; }
00231
00232 #else
00233
00234 ConstChar16Ptr::ConstChar16Ptr(const char16_t *p) { u_.cp = p; }
00235 #if !U_CHAR16_IS_TYPEDEF
00236 ConstChar16Ptr::ConstChar16Ptr(const uint16_t *p) { u_.up = p; }
00237 #endif
00238 #if U_SIZEOF_WCHAR_T==2
00239 ConstChar16Ptr::ConstChar16Ptr(const wchar_t *p) { u_.wp = p; }
00240 #endif
00241 ConstChar16Ptr::ConstChar16Ptr(const std::nullptr_t p) { u_.cp = p; }
00242 ConstChar16Ptr::~ConstChar16Ptr() {}
00243
00244 const char16_t *ConstChar16Ptr::get() const { return u_.cp; }
00245
00246 #endif
00247
00255 inline const UChar *toUCharPtr(const char16_t *p) {
00256 #ifdef U_ALIASING_BARRIER
00257 U_ALIASING_BARRIER(p);
00258 #endif
00259 return reinterpret_cast<const UChar *>(p);
00260 }
00261
00269 inline UChar *toUCharPtr(char16_t *p) {
00270 #ifdef U_ALIASING_BARRIER
00271 U_ALIASING_BARRIER(p);
00272 #endif
00273 return reinterpret_cast<UChar *>(p);
00274 }
00275
00283 inline const OldUChar *toOldUCharPtr(const char16_t *p) {
00284 #ifdef U_ALIASING_BARRIER
00285 U_ALIASING_BARRIER(p);
00286 #endif
00287 return reinterpret_cast<const OldUChar *>(p);
00288 }
00289
00297 inline OldUChar *toOldUCharPtr(char16_t *p) {
00298 #ifdef U_ALIASING_BARRIER
00299 U_ALIASING_BARRIER(p);
00300 #endif
00301 return reinterpret_cast<OldUChar *>(p);
00302 }
00303
00304 U_NAMESPACE_END
00305
00306 #endif // __CHAR16PTR_H__