ICU 64.2  64.2
char16ptr.h
Go to the documentation of this file.
1 // © 2017 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 
4 // char16ptr.h
5 // created: 2017feb28 Markus W. Scherer
6 
7 #ifndef __CHAR16PTR_H__
8 #define __CHAR16PTR_H__
9 
10 #include <cstddef>
11 #include "unicode/utypes.h"
12 
21 
27 #ifdef U_ALIASING_BARRIER
28  // Use the predefined value.
29 #elif (defined(__clang__) || defined(__GNUC__)) && U_PLATFORM != U_PF_BROWSER_NATIVE_CLIENT
30 # define U_ALIASING_BARRIER(ptr) asm volatile("" : : "rm"(ptr) : "memory")
31 #elif defined(U_IN_DOXYGEN)
32 # define U_ALIASING_BARRIER(ptr)
33 #endif
34 
40 public:
46  inline Char16Ptr(char16_t *p);
47 #if !U_CHAR16_IS_TYPEDEF
48 
53  inline Char16Ptr(uint16_t *p);
54 #endif
55 #if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN)
56 
62  inline Char16Ptr(wchar_t *p);
63 #endif
64 
69  inline Char16Ptr(std::nullptr_t p);
74  inline ~Char16Ptr();
75 
81  inline char16_t *get() const;
87  inline operator char16_t *() const { return get(); }
88 
89 private:
90  Char16Ptr() = delete;
91 
92 #ifdef U_ALIASING_BARRIER
93  template<typename T> static char16_t *cast(T *t) {
95  return reinterpret_cast<char16_t *>(t);
96  }
97 
98  char16_t *p_;
99 #else
100  union {
101  char16_t *cp;
102  uint16_t *up;
103  wchar_t *wp;
104  } u_;
105 #endif
106 };
107 
109 #ifdef U_ALIASING_BARRIER
110 
111 Char16Ptr::Char16Ptr(char16_t *p) : p_(p) {}
112 #if !U_CHAR16_IS_TYPEDEF
113 Char16Ptr::Char16Ptr(uint16_t *p) : p_(cast(p)) {}
114 #endif
115 #if U_SIZEOF_WCHAR_T==2
116 Char16Ptr::Char16Ptr(wchar_t *p) : p_(cast(p)) {}
117 #endif
118 Char16Ptr::Char16Ptr(std::nullptr_t p) : p_(p) {}
119 Char16Ptr::~Char16Ptr() {
120  U_ALIASING_BARRIER(p_);
121 }
122 
123 char16_t *Char16Ptr::get() const { return p_; }
124 
125 #else
126 
127 Char16Ptr::Char16Ptr(char16_t *p) { u_.cp = p; }
128 #if !U_CHAR16_IS_TYPEDEF
129 Char16Ptr::Char16Ptr(uint16_t *p) { u_.up = p; }
130 #endif
131 #if U_SIZEOF_WCHAR_T==2
132 Char16Ptr::Char16Ptr(wchar_t *p) { u_.wp = p; }
133 #endif
134 Char16Ptr::Char16Ptr(std::nullptr_t p) { u_.cp = p; }
135 Char16Ptr::~Char16Ptr() {}
136 
137 char16_t *Char16Ptr::get() const { return u_.cp; }
138 
139 #endif
140 
147 public:
153  inline ConstChar16Ptr(const char16_t *p);
154 #if !U_CHAR16_IS_TYPEDEF
155 
160  inline ConstChar16Ptr(const uint16_t *p);
161 #endif
162 #if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN)
163 
169  inline ConstChar16Ptr(const wchar_t *p);
170 #endif
171 
176  inline ConstChar16Ptr(const std::nullptr_t p);
177 
182  inline ~ConstChar16Ptr();
183 
189  inline const char16_t *get() const;
195  inline operator const char16_t *() const { return get(); }
196 
197 private:
198  ConstChar16Ptr() = delete;
199 
200 #ifdef U_ALIASING_BARRIER
201  template<typename T> static const char16_t *cast(const T *t) {
203  return reinterpret_cast<const char16_t *>(t);
204  }
205 
206  const char16_t *p_;
207 #else
208  union {
209  const char16_t *cp;
210  const uint16_t *up;
211  const wchar_t *wp;
212  } u_;
213 #endif
214 };
215 
217 #ifdef U_ALIASING_BARRIER
218 
219 ConstChar16Ptr::ConstChar16Ptr(const char16_t *p) : p_(p) {}
220 #if !U_CHAR16_IS_TYPEDEF
221 ConstChar16Ptr::ConstChar16Ptr(const uint16_t *p) : p_(cast(p)) {}
222 #endif
223 #if U_SIZEOF_WCHAR_T==2
224 ConstChar16Ptr::ConstChar16Ptr(const wchar_t *p) : p_(cast(p)) {}
225 #endif
226 ConstChar16Ptr::ConstChar16Ptr(const std::nullptr_t p) : p_(p) {}
227 ConstChar16Ptr::~ConstChar16Ptr() {
228  U_ALIASING_BARRIER(p_);
229 }
230 
231 const char16_t *ConstChar16Ptr::get() const { return p_; }
232 
233 #else
234 
235 ConstChar16Ptr::ConstChar16Ptr(const char16_t *p) { u_.cp = p; }
236 #if !U_CHAR16_IS_TYPEDEF
237 ConstChar16Ptr::ConstChar16Ptr(const uint16_t *p) { u_.up = p; }
238 #endif
239 #if U_SIZEOF_WCHAR_T==2
240 ConstChar16Ptr::ConstChar16Ptr(const wchar_t *p) { u_.wp = p; }
241 #endif
242 ConstChar16Ptr::ConstChar16Ptr(const std::nullptr_t p) { u_.cp = p; }
243 ConstChar16Ptr::~ConstChar16Ptr() {}
244 
245 const char16_t *ConstChar16Ptr::get() const { return u_.cp; }
246 
247 #endif
248 
257 inline const UChar *toUCharPtr(const char16_t *p) {
258 #ifdef U_ALIASING_BARRIER
260 #endif
261  return reinterpret_cast<const UChar *>(p);
262 }
263 
271 inline UChar *toUCharPtr(char16_t *p) {
272 #ifdef U_ALIASING_BARRIER
274 #endif
275  return reinterpret_cast<UChar *>(p);
276 }
277 
285 inline const OldUChar *toOldUCharPtr(const char16_t *p) {
286 #ifdef U_ALIASING_BARRIER
288 #endif
289  return reinterpret_cast<const OldUChar *>(p);
290 }
291 
299 inline OldUChar *toOldUCharPtr(char16_t *p) {
300 #ifdef U_ALIASING_BARRIER
302 #endif
303  return reinterpret_cast<OldUChar *>(p);
304 }
305 
307 
308 #endif // __CHAR16PTR_H__
uint16_t OldUChar
Default ICU 58 definition of UChar.
Definition: umachine.h:369
OldUChar * toOldUCharPtr(char16_t *p)
Converts from char16_t * to OldUChar *.
Definition: char16ptr.h:299
#define U_ALIASING_BARRIER(ptr)
Barrier for pointer anti-aliasing optimizations even across function boundaries.
Definition: char16ptr.h:32
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition: uversion.h:137
UChar * toUCharPtr(char16_t *p)
Converts from char16_t * to UChar *.
Definition: char16ptr.h:271
uint16_t UChar
The base type for UTF-16 code units and pointers.
Definition: umachine.h:342
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition: uversion.h:138
char16_t * wrapper with implicit conversion from distinct but bit-compatible pointer types...
Definition: char16ptr.h:39
const char16_t * wrapper with implicit conversion from distinct but bit-compatible pointer types...
Definition: char16ptr.h:146
#define U_FINAL
Defined to the C++11 "final" keyword if available.
Definition: umachine.h:140
Basic definitions for ICU, for both C and C++ APIs.
#define U_COMMON_API
Set to export library symbols from inside the common library, and to import them from outside...
Definition: utypes.h:300