ICU 63.1 63.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 <cstddef>
11 #include "unicode/utypes.h"
12
20 U_NAMESPACE_BEGIN
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
39 class U_COMMON_API Char16Ptr U_FINAL {
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) {
94 U_ALIASING_BARRIER(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
146 class U_COMMON_API ConstChar16Ptr U_FINAL {
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) {
202 U_ALIASING_BARRIER(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
259 U_ALIASING_BARRIER(p);
260 #endif
261 return reinterpret_cast<const UChar *>(p);
262 }
263
271 inline UChar *toUCharPtr(char16_t *p) {
272 #ifdef U_ALIASING_BARRIER
273 U_ALIASING_BARRIER(p);
274 #endif
275 return reinterpret_cast<UChar *>(p);
276 }
277
285 inline const OldUChar *toOldUCharPtr(const char16_t *p) {
286 #ifdef U_ALIASING_BARRIER
287 U_ALIASING_BARRIER(p);
288 #endif
289 return reinterpret_cast<const OldUChar *>(p);
290 }
291
299 inline OldUChar *toOldUCharPtr(char16_t *p) {
300 #ifdef U_ALIASING_BARRIER
301 U_ALIASING_BARRIER(p);
302 #endif
303 return reinterpret_cast<OldUChar *>(p);
304 }
305
306 U_NAMESPACE_END
307
308 #endif // __CHAR16PTR_H__
OldUChar
uint16_t OldUChar
Default ICU 58 definition of UChar.
Definition: umachine.h:369
icu::ConstChar16Ptr::get
const char16_t * get() const
Pointer access.
icu::toOldUCharPtr
const OldUChar * toOldUCharPtr(const char16_t *p)
Converts from const char16_t * to const OldUChar *.
Definition: char16ptr.h:285
U_ALIASING_BARRIER
#define U_ALIASING_BARRIER(ptr)
Barrier for pointer anti-aliasing optimizations even across function boundaries.
Definition: char16ptr.h:32
icu::Char16Ptr::get
char16_t * get() const
Pointer access.
U_NAMESPACE_BEGIN
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition: uversion.h:137
icu::ConstChar16Ptr::~ConstChar16Ptr
~ConstChar16Ptr()
Destructor.
icu::toUCharPtr
const UChar * toUCharPtr(const char16_t *p)
Converts from const char16_t * to const UChar *.
Definition: char16ptr.h:257
UChar
uint16_t UChar
The base type for UTF-16 code units and pointers.
Definition: umachine.h:342
U_NAMESPACE_END
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition: uversion.h:138
icu::Char16Ptr::~Char16Ptr
~Char16Ptr()
Destructor.
icu::Char16Ptr
char16_t * wrapper with implicit conversion from distinct but bit-compatible pointer types...
Definition: char16ptr.h:39
icu::ConstChar16Ptr
const char16_t * wrapper with implicit conversion from distinct but bit-compatible pointer types...
Definition: char16ptr.h:146
U_FINAL
#define U_FINAL
Defined to the C++11 "final" keyword if available.
Definition: umachine.h:140
utypes.h
Basic definitions for ICU, for both C and C++ APIs.
U_COMMON_API
#define U_COMMON_API
Set to export library symbols from inside the common library, and to import them from outside...
Definition: utypes.h:300