Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/unicode/char16ptr.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // © 2017 and later: Unicode, Inc. and others.
0002 // License & terms of use: http://www.unicode.org/copyright.html
0003 
0004 // char16ptr.h
0005 // created: 2017feb28 Markus W. Scherer
0006 
0007 #ifndef __CHAR16PTR_H__
0008 #define __CHAR16PTR_H__
0009 
0010 #include "unicode/utypes.h"
0011 
0012 #if U_SHOW_CPLUSPLUS_API
0013 
0014 #include <cstddef>
0015 
0016 /**
0017  * \file
0018  * \brief C++ API: char16_t pointer wrappers with
0019  *        implicit conversion from bit-compatible raw pointer types.
0020  *        Also conversion functions from char16_t * to UChar * and OldUChar *.
0021  */
0022 
0023 U_NAMESPACE_BEGIN
0024 
0025 /**
0026  * \def U_ALIASING_BARRIER
0027  * Barrier for pointer anti-aliasing optimizations even across function boundaries.
0028  * @internal
0029  */
0030 #ifdef U_ALIASING_BARRIER
0031     // Use the predefined value.
0032 #elif (defined(__clang__) || defined(__GNUC__)) && U_PLATFORM != U_PF_BROWSER_NATIVE_CLIENT
0033 #   define U_ALIASING_BARRIER(ptr) asm volatile("" : : "rm"(ptr) : "memory")
0034 #elif defined(U_IN_DOXYGEN)
0035 #   define U_ALIASING_BARRIER(ptr)
0036 #endif
0037 
0038 /**
0039  * char16_t * wrapper with implicit conversion from distinct but bit-compatible pointer types.
0040  * @stable ICU 59
0041  */
0042 class U_COMMON_API Char16Ptr final {
0043 public:
0044     /**
0045      * Copies the pointer.
0046      * @param p pointer
0047      * @stable ICU 59
0048      */
0049     inline Char16Ptr(char16_t *p);
0050 #if !U_CHAR16_IS_TYPEDEF
0051     /**
0052      * Converts the pointer to char16_t *.
0053      * @param p pointer to be converted
0054      * @stable ICU 59
0055      */
0056     inline Char16Ptr(uint16_t *p);
0057 #endif
0058 #if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN)
0059     /**
0060      * Converts the pointer to char16_t *.
0061      * (Only defined if U_SIZEOF_WCHAR_T==2.)
0062      * @param p pointer to be converted
0063      * @stable ICU 59
0064      */
0065     inline Char16Ptr(wchar_t *p);
0066 #endif
0067     /**
0068      * nullptr constructor.
0069      * @param p nullptr
0070      * @stable ICU 59
0071      */
0072     inline Char16Ptr(std::nullptr_t p);
0073     /**
0074      * Destructor.
0075      * @stable ICU 59
0076      */
0077     inline ~Char16Ptr();
0078 
0079     /**
0080      * Pointer access.
0081      * @return the wrapped pointer
0082      * @stable ICU 59
0083      */
0084     inline char16_t *get() const;
0085     /**
0086      * char16_t pointer access via type conversion (e.g., static_cast).
0087      * @return the wrapped pointer
0088      * @stable ICU 59
0089      */
0090     inline operator char16_t *() const { return get(); }
0091 
0092 private:
0093     Char16Ptr() = delete;
0094 
0095 #ifdef U_ALIASING_BARRIER
0096     template<typename T> static char16_t *cast(T *t) {
0097         U_ALIASING_BARRIER(t);
0098         return reinterpret_cast<char16_t *>(t);
0099     }
0100 
0101     char16_t *p_;
0102 #else
0103     union {
0104         char16_t *cp;
0105         uint16_t *up;
0106         wchar_t *wp;
0107     } u_;
0108 #endif
0109 };
0110 
0111 /// \cond
0112 #ifdef U_ALIASING_BARRIER
0113 
0114 Char16Ptr::Char16Ptr(char16_t *p) : p_(p) {}
0115 #if !U_CHAR16_IS_TYPEDEF
0116 Char16Ptr::Char16Ptr(uint16_t *p) : p_(cast(p)) {}
0117 #endif
0118 #if U_SIZEOF_WCHAR_T==2
0119 Char16Ptr::Char16Ptr(wchar_t *p) : p_(cast(p)) {}
0120 #endif
0121 Char16Ptr::Char16Ptr(std::nullptr_t p) : p_(p) {}
0122 Char16Ptr::~Char16Ptr() {
0123     U_ALIASING_BARRIER(p_);
0124 }
0125 
0126 char16_t *Char16Ptr::get() const { return p_; }
0127 
0128 #else
0129 
0130 Char16Ptr::Char16Ptr(char16_t *p) { u_.cp = p; }
0131 #if !U_CHAR16_IS_TYPEDEF
0132 Char16Ptr::Char16Ptr(uint16_t *p) { u_.up = p; }
0133 #endif
0134 #if U_SIZEOF_WCHAR_T==2
0135 Char16Ptr::Char16Ptr(wchar_t *p) { u_.wp = p; }
0136 #endif
0137 Char16Ptr::Char16Ptr(std::nullptr_t p) { u_.cp = p; }
0138 Char16Ptr::~Char16Ptr() {}
0139 
0140 char16_t *Char16Ptr::get() const { return u_.cp; }
0141 
0142 #endif
0143 /// \endcond
0144 
0145 /**
0146  * const char16_t * wrapper with implicit conversion from distinct but bit-compatible pointer types.
0147  * @stable ICU 59
0148  */
0149 class U_COMMON_API ConstChar16Ptr final {
0150 public:
0151     /**
0152      * Copies the pointer.
0153      * @param p pointer
0154      * @stable ICU 59
0155      */
0156     inline ConstChar16Ptr(const char16_t *p);
0157 #if !U_CHAR16_IS_TYPEDEF
0158     /**
0159      * Converts the pointer to char16_t *.
0160      * @param p pointer to be converted
0161      * @stable ICU 59
0162      */
0163     inline ConstChar16Ptr(const uint16_t *p);
0164 #endif
0165 #if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN)
0166     /**
0167      * Converts the pointer to char16_t *.
0168      * (Only defined if U_SIZEOF_WCHAR_T==2.)
0169      * @param p pointer to be converted
0170      * @stable ICU 59
0171      */
0172     inline ConstChar16Ptr(const wchar_t *p);
0173 #endif
0174     /**
0175      * nullptr constructor.
0176      * @param p nullptr
0177      * @stable ICU 59
0178      */
0179     inline ConstChar16Ptr(const std::nullptr_t p);
0180 
0181     /**
0182      * Destructor.
0183      * @stable ICU 59
0184      */
0185     inline ~ConstChar16Ptr();
0186 
0187     /**
0188      * Pointer access.
0189      * @return the wrapped pointer
0190      * @stable ICU 59
0191      */
0192     inline const char16_t *get() const;
0193     /**
0194      * char16_t pointer access via type conversion (e.g., static_cast).
0195      * @return the wrapped pointer
0196      * @stable ICU 59
0197      */
0198     inline operator const char16_t *() const { return get(); }
0199 
0200 private:
0201     ConstChar16Ptr() = delete;
0202 
0203 #ifdef U_ALIASING_BARRIER
0204     template<typename T> static const char16_t *cast(const T *t) {
0205         U_ALIASING_BARRIER(t);
0206         return reinterpret_cast<const char16_t *>(t);
0207     }
0208 
0209     const char16_t *p_;
0210 #else
0211     union {
0212         const char16_t *cp;
0213         const uint16_t *up;
0214         const wchar_t *wp;
0215     } u_;
0216 #endif
0217 };
0218 
0219 /// \cond
0220 #ifdef U_ALIASING_BARRIER
0221 
0222 ConstChar16Ptr::ConstChar16Ptr(const char16_t *p) : p_(p) {}
0223 #if !U_CHAR16_IS_TYPEDEF
0224 ConstChar16Ptr::ConstChar16Ptr(const uint16_t *p) : p_(cast(p)) {}
0225 #endif
0226 #if U_SIZEOF_WCHAR_T==2
0227 ConstChar16Ptr::ConstChar16Ptr(const wchar_t *p) : p_(cast(p)) {}
0228 #endif
0229 ConstChar16Ptr::ConstChar16Ptr(const std::nullptr_t p) : p_(p) {}
0230 ConstChar16Ptr::~ConstChar16Ptr() {
0231     U_ALIASING_BARRIER(p_);
0232 }
0233 
0234 const char16_t *ConstChar16Ptr::get() const { return p_; }
0235 
0236 #else
0237 
0238 ConstChar16Ptr::ConstChar16Ptr(const char16_t *p) { u_.cp = p; }
0239 #if !U_CHAR16_IS_TYPEDEF
0240 ConstChar16Ptr::ConstChar16Ptr(const uint16_t *p) { u_.up = p; }
0241 #endif
0242 #if U_SIZEOF_WCHAR_T==2
0243 ConstChar16Ptr::ConstChar16Ptr(const wchar_t *p) { u_.wp = p; }
0244 #endif
0245 ConstChar16Ptr::ConstChar16Ptr(const std::nullptr_t p) { u_.cp = p; }
0246 ConstChar16Ptr::~ConstChar16Ptr() {}
0247 
0248 const char16_t *ConstChar16Ptr::get() const { return u_.cp; }
0249 
0250 #endif
0251 /// \endcond
0252 
0253 /**
0254  * Converts from const char16_t * to const UChar *.
0255  * Includes an aliasing barrier if available.
0256  * @param p pointer
0257  * @return p as const UChar *
0258  * @stable ICU 59
0259  */
0260 inline const UChar *toUCharPtr(const char16_t *p) {
0261 #ifdef U_ALIASING_BARRIER
0262     U_ALIASING_BARRIER(p);
0263 #endif
0264     return reinterpret_cast<const UChar *>(p);
0265 }
0266 
0267 /**
0268  * Converts from char16_t * to UChar *.
0269  * Includes an aliasing barrier if available.
0270  * @param p pointer
0271  * @return p as UChar *
0272  * @stable ICU 59
0273  */
0274 inline UChar *toUCharPtr(char16_t *p) {
0275 #ifdef U_ALIASING_BARRIER
0276     U_ALIASING_BARRIER(p);
0277 #endif
0278     return reinterpret_cast<UChar *>(p);
0279 }
0280 
0281 /**
0282  * Converts from const char16_t * to const OldUChar *.
0283  * Includes an aliasing barrier if available.
0284  * @param p pointer
0285  * @return p as const OldUChar *
0286  * @stable ICU 59
0287  */
0288 inline const OldUChar *toOldUCharPtr(const char16_t *p) {
0289 #ifdef U_ALIASING_BARRIER
0290     U_ALIASING_BARRIER(p);
0291 #endif
0292     return reinterpret_cast<const OldUChar *>(p);
0293 }
0294 
0295 /**
0296  * Converts from char16_t * to OldUChar *.
0297  * Includes an aliasing barrier if available.
0298  * @param p pointer
0299  * @return p as OldUChar *
0300  * @stable ICU 59
0301  */
0302 inline OldUChar *toOldUCharPtr(char16_t *p) {
0303 #ifdef U_ALIASING_BARRIER
0304     U_ALIASING_BARRIER(p);
0305 #endif
0306     return reinterpret_cast<OldUChar *>(p);
0307 }
0308 
0309 U_NAMESPACE_END
0310 
0311 #endif /* U_SHOW_CPLUSPLUS_API */
0312 
0313 #endif  // __CHAR16PTR_H__