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 #elif defined(U_IN_DOXYGEN)
00032 # define U_ALIASING_BARRIER(ptr)
00033 #endif
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
00109 #ifdef U_ALIASING_BARRIER
00110
00111 Char16Ptr::Char16Ptr(char16_t *p) : p_(p) {}
00112 #if !U_CHAR16_IS_TYPEDEF
00113 Char16Ptr::Char16Ptr(uint16_t *p) : p_(cast(p)) {}
00114 #endif
00115 #if U_SIZEOF_WCHAR_T==2
00116 Char16Ptr::Char16Ptr(wchar_t *p) : p_(cast(p)) {}
00117 #endif
00118 Char16Ptr::Char16Ptr(std::nullptr_t p) : p_(p) {}
00119 Char16Ptr::~Char16Ptr() {
00120 U_ALIASING_BARRIER(p_);
00121 }
00122
00123 char16_t *Char16Ptr::get() const { return p_; }
00124
00125 #else
00126
00127 Char16Ptr::Char16Ptr(char16_t *p) { u_.cp = p; }
00128 #if !U_CHAR16_IS_TYPEDEF
00129 Char16Ptr::Char16Ptr(uint16_t *p) { u_.up = p; }
00130 #endif
00131 #if U_SIZEOF_WCHAR_T==2
00132 Char16Ptr::Char16Ptr(wchar_t *p) { u_.wp = p; }
00133 #endif
00134 Char16Ptr::Char16Ptr(std::nullptr_t p) { u_.cp = p; }
00135 Char16Ptr::~Char16Ptr() {}
00136
00137 char16_t *Char16Ptr::get() const { return u_.cp; }
00138
00139 #endif
00141
00142
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
00217 #ifdef U_ALIASING_BARRIER
00218
00219 ConstChar16Ptr::ConstChar16Ptr(const char16_t *p) : p_(p) {}
00220 #if !U_CHAR16_IS_TYPEDEF
00221 ConstChar16Ptr::ConstChar16Ptr(const uint16_t *p) : p_(cast(p)) {}
00222 #endif
00223 #if U_SIZEOF_WCHAR_T==2
00224 ConstChar16Ptr::ConstChar16Ptr(const wchar_t *p) : p_(cast(p)) {}
00225 #endif
00226 ConstChar16Ptr::ConstChar16Ptr(const std::nullptr_t p) : p_(p) {}
00227 ConstChar16Ptr::~ConstChar16Ptr() {
00228 U_ALIASING_BARRIER(p_);
00229 }
00230
00231 const char16_t *ConstChar16Ptr::get() const { return p_; }
00232
00233 #else
00234
00235 ConstChar16Ptr::ConstChar16Ptr(const char16_t *p) { u_.cp = p; }
00236 #if !U_CHAR16_IS_TYPEDEF
00237 ConstChar16Ptr::ConstChar16Ptr(const uint16_t *p) { u_.up = p; }
00238 #endif
00239 #if U_SIZEOF_WCHAR_T==2
00240 ConstChar16Ptr::ConstChar16Ptr(const wchar_t *p) { u_.wp = p; }
00241 #endif
00242 ConstChar16Ptr::ConstChar16Ptr(const std::nullptr_t p) { u_.cp = p; }
00243 ConstChar16Ptr::~ConstChar16Ptr() {}
00244
00245 const char16_t *ConstChar16Ptr::get() const { return u_.cp; }
00246
00247 #endif
00249
00250
00257 inline const UChar *toUCharPtr(const char16_t *p) {
00258 #ifdef U_ALIASING_BARRIER
00259 U_ALIASING_BARRIER(p);
00260 #endif
00261 return reinterpret_cast<const UChar *>(p);
00262 }
00263
00271 inline UChar *toUCharPtr(char16_t *p) {
00272 #ifdef U_ALIASING_BARRIER
00273 U_ALIASING_BARRIER(p);
00274 #endif
00275 return reinterpret_cast<UChar *>(p);
00276 }
00277
00285 inline const OldUChar *toOldUCharPtr(const char16_t *p) {
00286 #ifdef U_ALIASING_BARRIER
00287 U_ALIASING_BARRIER(p);
00288 #endif
00289 return reinterpret_cast<const OldUChar *>(p);
00290 }
00291
00299 inline OldUChar *toOldUCharPtr(char16_t *p) {
00300 #ifdef U_ALIASING_BARRIER
00301 U_ALIASING_BARRIER(p);
00302 #endif
00303 return reinterpret_cast<OldUChar *>(p);
00304 }
00305
00306 U_NAMESPACE_END
00307
00308 #endif // __CHAR16PTR_H__