ICU 69.1 69.1

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 "unicode/utypes.h"
11
12 #if U_SHOW_CPLUSPLUS_API
13
14 #include <cstddef>
15
23 U_NAMESPACE_BEGIN
24
30 #ifdef U_ALIASING_BARRIER
31 // Use the predefined value.
32 #elif (defined(__clang__) || defined(__GNUC__)) && U_PLATFORM != U_PF_BROWSER_NATIVE_CLIENT
33 # define U_ALIASING_BARRIER(ptr) asm volatile("" : : "rm"(ptr) : "memory")
34 #elif defined(U_IN_DOXYGEN)
35 # define U_ALIASING_BARRIER(ptr)
36 #endif
37
42 class U_COMMON_API Char16Ptr U_FINAL {
43 public:
49 inline Char16Ptr(char16_t *p);
50 #if !U_CHAR16_IS_TYPEDEF
51
56 inline Char16Ptr(uint16_t *p);
57 #endif
58 #if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN)
59
65 inline Char16Ptr(wchar_t *p);
66 #endif
67
72 inline Char16Ptr(std::nullptr_t p);
77 inline ~Char16Ptr();
78
84 inline char16_t *get() const;
90 inline operator char16_t *() const { return get(); }
91
92 private:
93 Char16Ptr() = delete;
94
95 #ifdef U_ALIASING_BARRIER
96 template<typename T> static char16_t *cast(T *t) {
97 U_ALIASING_BARRIER(t);
98 return reinterpret_cast<char16_t *>(t);
99 }
100
101 char16_t *p_;
102 #else
103 union {
104 char16_t *cp;
105 uint16_t *up;
106 wchar_t *wp;
107 } u_;
108 #endif
109 };
110
112 #ifdef U_ALIASING_BARRIER
113
114 Char16Ptr::Char16Ptr(char16_t *p) : p_(p) {}
115 #if !U_CHAR16_IS_TYPEDEF
116 Char16Ptr::Char16Ptr(uint16_t *p) : p_(cast(p)) {}
117 #endif
118 #if U_SIZEOF_WCHAR_T==2
119 Char16Ptr::Char16Ptr(wchar_t *p) : p_(cast(p)) {}
120 #endif
121 Char16Ptr::Char16Ptr(std::nullptr_t p) : p_(p) {}
122 Char16Ptr::~Char16Ptr() {
123 U_ALIASING_BARRIER(p_);
124 }
125
126 char16_t *Char16Ptr::get() const { return p_; }
127
128 #else
129
130 Char16Ptr::Char16Ptr(char16_t *p) { u_.cp = p; }
131 #if !U_CHAR16_IS_TYPEDEF
132 Char16Ptr::Char16Ptr(uint16_t *p) { u_.up = p; }
133 #endif
134 #if U_SIZEOF_WCHAR_T==2
135 Char16Ptr::Char16Ptr(wchar_t *p) { u_.wp = p; }
136 #endif
137 Char16Ptr::Char16Ptr(std::nullptr_t p) { u_.cp = p; }
138 Char16Ptr::~Char16Ptr() {}
139
140 char16_t *Char16Ptr::get() const { return u_.cp; }
141
142 #endif
143
149 class U_COMMON_API ConstChar16Ptr U_FINAL {
150 public:
156 inline ConstChar16Ptr(const char16_t *p);
157 #if !U_CHAR16_IS_TYPEDEF
158
163 inline ConstChar16Ptr(const uint16_t *p);
164 #endif
165 #if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN)
166
172 inline ConstChar16Ptr(const wchar_t *p);
173 #endif
174
179 inline ConstChar16Ptr(const std::nullptr_t p);
180
185 inline ~ConstChar16Ptr();
186
192 inline const char16_t *get() const;
198 inline operator const char16_t *() const { return get(); }
199
200 private:
201 ConstChar16Ptr() = delete;
202
203 #ifdef U_ALIASING_BARRIER
204 template<typename T> static const char16_t *cast(const T *t) {
205 U_ALIASING_BARRIER(t);
206 return reinterpret_cast<const char16_t *>(t);
207 }
208
209 const char16_t *p_;
210 #else
211 union {
212 const char16_t *cp;
213 const uint16_t *up;
214 const wchar_t *wp;
215 } u_;
216 #endif
217 };
218
220 #ifdef U_ALIASING_BARRIER
221
222 ConstChar16Ptr::ConstChar16Ptr(const char16_t *p) : p_(p) {}
223 #if !U_CHAR16_IS_TYPEDEF
224 ConstChar16Ptr::ConstChar16Ptr(const uint16_t *p) : p_(cast(p)) {}
225 #endif
226 #if U_SIZEOF_WCHAR_T==2
227 ConstChar16Ptr::ConstChar16Ptr(const wchar_t *p) : p_(cast(p)) {}
228 #endif
229 ConstChar16Ptr::ConstChar16Ptr(const std::nullptr_t p) : p_(p) {}
230 ConstChar16Ptr::~ConstChar16Ptr() {
231 U_ALIASING_BARRIER(p_);
232 }
233
234 const char16_t *ConstChar16Ptr::get() const { return p_; }
235
236 #else
237
238 ConstChar16Ptr::ConstChar16Ptr(const char16_t *p) { u_.cp = p; }
239 #if !U_CHAR16_IS_TYPEDEF
240 ConstChar16Ptr::ConstChar16Ptr(const uint16_t *p) { u_.up = p; }
241 #endif
242 #if U_SIZEOF_WCHAR_T==2
243 ConstChar16Ptr::ConstChar16Ptr(const wchar_t *p) { u_.wp = p; }
244 #endif
245 ConstChar16Ptr::ConstChar16Ptr(const std::nullptr_t p) { u_.cp = p; }
246 ConstChar16Ptr::~ConstChar16Ptr() {}
247
248 const char16_t *ConstChar16Ptr::get() const { return u_.cp; }
249
250 #endif
251
260 inline const UChar *toUCharPtr(const char16_t *p) {
261 #ifdef U_ALIASING_BARRIER
262 U_ALIASING_BARRIER(p);
263 #endif
264 return reinterpret_cast<const UChar *>(p);
265 }
266
274 inline UChar *toUCharPtr(char16_t *p) {
275 #ifdef U_ALIASING_BARRIER
276 U_ALIASING_BARRIER(p);
277 #endif
278 return reinterpret_cast<UChar *>(p);
279 }
280
288 inline const OldUChar *toOldUCharPtr(const char16_t *p) {
289 #ifdef U_ALIASING_BARRIER
290 U_ALIASING_BARRIER(p);
291 #endif
292 return reinterpret_cast<const OldUChar *>(p);
293 }
294
302 inline OldUChar *toOldUCharPtr(char16_t *p) {
303 #ifdef U_ALIASING_BARRIER
304 U_ALIASING_BARRIER(p);
305 #endif
306 return reinterpret_cast<OldUChar *>(p);
307 }
308
309 U_NAMESPACE_END
310
311 #endif /* U_SHOW_CPLUSPLUS_API */
312
313 #endif // __CHAR16PTR_H__
U_FINAL
#define U_FINAL
Definition: umachine.h:141
utypes.h
Basic definitions for ICU, for both C and C++ APIs.
icu::toUCharPtr
const UChar * toUCharPtr(const char16_t *p)
Converts from const char16_t * to const UChar *.
Definition: char16ptr.h:260
icu::Char16Ptr::~Char16Ptr
~Char16Ptr()
Destructor.
U_COMMON_API
#define U_COMMON_API
Definition: utypes.h:300
icu::Char16Ptr
char16_t * wrapper with implicit conversion from distinct but bit-compatible pointer types.
Definition: char16ptr.h:42
icu::ConstChar16Ptr::get
const char16_t * get() const
Pointer access.
icu::ConstChar16Ptr::~ConstChar16Ptr
~ConstChar16Ptr()
Destructor.
icu::toOldUCharPtr
const OldUChar * toOldUCharPtr(const char16_t *p)
Converts from const char16_t * to const OldUChar *.
Definition: char16ptr.h:288
U_ALIASING_BARRIER
#define U_ALIASING_BARRIER(ptr)
Definition: char16ptr.h:35
icu::Char16Ptr::get
char16_t * get() const
Pointer access.
UChar
char16_t UChar
Definition: umachine.h:418
OldUChar
uint16_t OldUChar
Definition: umachine.h:447
icu::ConstChar16Ptr
const char16_t * wrapper with implicit conversion from distinct but bit-compatible pointer types.
Definition: char16ptr.h:149