ICU 72.1 72.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
23U_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
43public:
49 inline Char16Ptr(char16_t *p);
50#if !U_CHAR16_IS_TYPEDEF
56 inline Char16Ptr(uint16_t *p);
57#endif
58#if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN)
65 inline Char16Ptr(wchar_t *p);
66#endif
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
92private:
93 Char16Ptr() = delete;
94
95#ifdef U_ALIASING_BARRIER
96 template<typename T> static char16_t *cast(T *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
114Char16Ptr::Char16Ptr(char16_t *p) : p_(p) {}
115#if !U_CHAR16_IS_TYPEDEF
116Char16Ptr::Char16Ptr(uint16_t *p) : p_(cast(p)) {}
117#endif
118#if U_SIZEOF_WCHAR_T==2
119Char16Ptr::Char16Ptr(wchar_t *p) : p_(cast(p)) {}
120#endif
121Char16Ptr::Char16Ptr(std::nullptr_t p) : p_(p) {}
124}
125
126char16_t *Char16Ptr::get() const { return p_; }
127
128#else
129
130Char16Ptr::Char16Ptr(char16_t *p) { u_.cp = p; }
131#if !U_CHAR16_IS_TYPEDEF
132Char16Ptr::Char16Ptr(uint16_t *p) { u_.up = p; }
133#endif
134#if U_SIZEOF_WCHAR_T==2
135Char16Ptr::Char16Ptr(wchar_t *p) { u_.wp = p; }
136#endif
137Char16Ptr::Char16Ptr(std::nullptr_t p) { u_.cp = p; }
139
140char16_t *Char16Ptr::get() const { return u_.cp; }
141
142#endif
144
150public:
156 inline ConstChar16Ptr(const char16_t *p);
157#if !U_CHAR16_IS_TYPEDEF
163 inline ConstChar16Ptr(const uint16_t *p);
164#endif
165#if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN)
172 inline ConstChar16Ptr(const wchar_t *p);
173#endif
179 inline ConstChar16Ptr(const std::nullptr_t p);
180
186
192 inline const char16_t *get() const;
198 inline operator const char16_t *() const { return get(); }
199
200private:
201 ConstChar16Ptr() = delete;
202
203#ifdef U_ALIASING_BARRIER
204 template<typename T> static const char16_t *cast(const T *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
222ConstChar16Ptr::ConstChar16Ptr(const char16_t *p) : p_(p) {}
223#if !U_CHAR16_IS_TYPEDEF
224ConstChar16Ptr::ConstChar16Ptr(const uint16_t *p) : p_(cast(p)) {}
225#endif
226#if U_SIZEOF_WCHAR_T==2
227ConstChar16Ptr::ConstChar16Ptr(const wchar_t *p) : p_(cast(p)) {}
228#endif
229ConstChar16Ptr::ConstChar16Ptr(const std::nullptr_t p) : p_(p) {}
232}
233
234const char16_t *ConstChar16Ptr::get() const { return p_; }
235
236#else
237
238ConstChar16Ptr::ConstChar16Ptr(const char16_t *p) { u_.cp = p; }
239#if !U_CHAR16_IS_TYPEDEF
240ConstChar16Ptr::ConstChar16Ptr(const uint16_t *p) { u_.up = p; }
241#endif
242#if U_SIZEOF_WCHAR_T==2
243ConstChar16Ptr::ConstChar16Ptr(const wchar_t *p) { u_.wp = p; }
244#endif
245ConstChar16Ptr::ConstChar16Ptr(const std::nullptr_t p) { u_.cp = p; }
247
248const char16_t *ConstChar16Ptr::get() const { return u_.cp; }
249
250#endif
252
260inline const UChar *toUCharPtr(const char16_t *p) {
261#ifdef U_ALIASING_BARRIER
263#endif
264 return reinterpret_cast<const UChar *>(p);
265}
266
274inline UChar *toUCharPtr(char16_t *p) {
275#ifdef U_ALIASING_BARRIER
277#endif
278 return reinterpret_cast<UChar *>(p);
279}
280
288inline const OldUChar *toOldUCharPtr(const char16_t *p) {
289#ifdef U_ALIASING_BARRIER
291#endif
292 return reinterpret_cast<const OldUChar *>(p);
293}
294
302inline OldUChar *toOldUCharPtr(char16_t *p) {
303#ifdef U_ALIASING_BARRIER
305#endif
306 return reinterpret_cast<OldUChar *>(p);
307}
308
309U_NAMESPACE_END
310
311#endif /* U_SHOW_CPLUSPLUS_API */
312
313#endif // __CHAR16PTR_H__
#define U_ALIASING_BARRIER(ptr)
Barrier for pointer anti-aliasing optimizations even across function boundaries.
Definition: char16ptr.h:35
char16_t * wrapper with implicit conversion from distinct but bit-compatible pointer types.
Definition: char16ptr.h:42
Char16Ptr(std::nullptr_t p)
nullptr constructor.
~Char16Ptr()
Destructor.
Char16Ptr(uint16_t *p)
Converts the pointer to char16_t *.
Char16Ptr(wchar_t *p)
Converts the pointer to char16_t *.
Char16Ptr(char16_t *p)
Copies the pointer.
char16_t * get() const
Pointer access.
const char16_t * wrapper with implicit conversion from distinct but bit-compatible pointer types.
Definition: char16ptr.h:149
ConstChar16Ptr(const char16_t *p)
Copies the pointer.
ConstChar16Ptr(const uint16_t *p)
Converts the pointer to char16_t *.
~ConstChar16Ptr()
Destructor.
const char16_t * get() const
Pointer access.
ConstChar16Ptr(const wchar_t *p)
Converts the pointer to char16_t *.
ConstChar16Ptr(const std::nullptr_t p)
nullptr constructor.
const OldUChar * toOldUCharPtr(const char16_t *p)
Converts from const char16_t * to const OldUChar *.
Definition: char16ptr.h:288
const UChar * toUCharPtr(const char16_t *p)
Converts from const char16_t * to const UChar *.
Definition: char16ptr.h:260
#define U_FINAL
Defined to the C++11 "final" keyword if available.
Definition: umachine.h:141
char16_t UChar
The base type for UTF-16 code units and pointers.
Definition: umachine.h:412
uint16_t OldUChar
Default ICU 58 definition of UChar.
Definition: umachine.h:441
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