ICU 62.1 62.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
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#endif
32
37class U_COMMON_API Char16Ptr U_FINAL {
38public:
44 inline Char16Ptr(char16_t *p);
45#if !U_CHAR16_IS_TYPEDEF
51 inline Char16Ptr(uint16_t *p);
52#endif
53#if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN)
60 inline Char16Ptr(wchar_t *p);
61#endif
67 inline Char16Ptr(std::nullptr_t p);
72 inline ~Char16Ptr();
73
79 inline char16_t *get() const;
85 inline operator char16_t *() const { return get(); }
86
87private:
88 Char16Ptr() = delete;
89
90#ifdef U_ALIASING_BARRIER
91 template<typename T> static char16_t *cast(T *t) {
92 U_ALIASING_BARRIER(t);
93 return reinterpret_cast<char16_t *>(t);
94 }
95
96 char16_t *p_;
97#else
98 union {
99 char16_t *cp;
100 uint16_t *up;
101 wchar_t *wp;
102 } u_;
103#endif
104};
105
106#ifdef U_ALIASING_BARRIER
107
108Char16Ptr::Char16Ptr(char16_t *p) : p_(p) {}
109#if !U_CHAR16_IS_TYPEDEF
110Char16Ptr::Char16Ptr(uint16_t *p) : p_(cast(p)) {}
111#endif
112#if U_SIZEOF_WCHAR_T==2
113Char16Ptr::Char16Ptr(wchar_t *p) : p_(cast(p)) {}
114#endif
115Char16Ptr::Char16Ptr(std::nullptr_t p) : p_(p) {}
117 U_ALIASING_BARRIER(p_);
118}
119
120char16_t *Char16Ptr::get() const { return p_; }
121
122#else
123
124Char16Ptr::Char16Ptr(char16_t *p) { u_.cp = p; }
125#if !U_CHAR16_IS_TYPEDEF
126Char16Ptr::Char16Ptr(uint16_t *p) { u_.up = p; }
127#endif
128#if U_SIZEOF_WCHAR_T==2
129Char16Ptr::Char16Ptr(wchar_t *p) { u_.wp = p; }
130#endif
131Char16Ptr::Char16Ptr(std::nullptr_t p) { u_.cp = p; }
133
134char16_t *Char16Ptr::get() const { return u_.cp; }
135
136#endif
137
143public:
149 inline ConstChar16Ptr(const char16_t *p);
150#if !U_CHAR16_IS_TYPEDEF
156 inline ConstChar16Ptr(const uint16_t *p);
157#endif
158#if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN)
165 inline ConstChar16Ptr(const wchar_t *p);
166#endif
172 inline ConstChar16Ptr(const std::nullptr_t p);
173
178 inline ~ConstChar16Ptr();
179
185 inline const char16_t *get() const;
191 inline operator const char16_t *() const { return get(); }
192
193private:
194 ConstChar16Ptr() = delete;
195
196#ifdef U_ALIASING_BARRIER
197 template<typename T> static const char16_t *cast(const T *t) {
198 U_ALIASING_BARRIER(t);
199 return reinterpret_cast<const char16_t *>(t);
200 }
201
202 const char16_t *p_;
203#else
204 union {
205 const char16_t *cp;
206 const uint16_t *up;
207 const wchar_t *wp;
208 } u_;
209#endif
210};
211
212#ifdef U_ALIASING_BARRIER
213
214ConstChar16Ptr::ConstChar16Ptr(const char16_t *p) : p_(p) {}
215#if !U_CHAR16_IS_TYPEDEF
216ConstChar16Ptr::ConstChar16Ptr(const uint16_t *p) : p_(cast(p)) {}
217#endif
218#if U_SIZEOF_WCHAR_T==2
219ConstChar16Ptr::ConstChar16Ptr(const wchar_t *p) : p_(cast(p)) {}
220#endif
221ConstChar16Ptr::ConstChar16Ptr(const std::nullptr_t p) : p_(p) {}
223 U_ALIASING_BARRIER(p_);
224}
225
226const char16_t *ConstChar16Ptr::get() const { return p_; }
227
228#else
229
230ConstChar16Ptr::ConstChar16Ptr(const char16_t *p) { u_.cp = p; }
231#if !U_CHAR16_IS_TYPEDEF
232ConstChar16Ptr::ConstChar16Ptr(const uint16_t *p) { u_.up = p; }
233#endif
234#if U_SIZEOF_WCHAR_T==2
235ConstChar16Ptr::ConstChar16Ptr(const wchar_t *p) { u_.wp = p; }
236#endif
237ConstChar16Ptr::ConstChar16Ptr(const std::nullptr_t p) { u_.cp = p; }
239
240const char16_t *ConstChar16Ptr::get() const { return u_.cp; }
241
242#endif
243
251inline const UChar *toUCharPtr(const char16_t *p) {
252#ifdef U_ALIASING_BARRIER
253 U_ALIASING_BARRIER(p);
254#endif
255 return reinterpret_cast<const UChar *>(p);
256}
257
265inline UChar *toUCharPtr(char16_t *p) {
266#ifdef U_ALIASING_BARRIER
267 U_ALIASING_BARRIER(p);
268#endif
269 return reinterpret_cast<UChar *>(p);
270}
271
279inline const OldUChar *toOldUCharPtr(const char16_t *p) {
280#ifdef U_ALIASING_BARRIER
281 U_ALIASING_BARRIER(p);
282#endif
283 return reinterpret_cast<const OldUChar *>(p);
284}
285
293inline OldUChar *toOldUCharPtr(char16_t *p) {
294#ifdef U_ALIASING_BARRIER
295 U_ALIASING_BARRIER(p);
296#endif
297 return reinterpret_cast<OldUChar *>(p);
298}
299
301
302#endif // __CHAR16PTR_H__
char16_t * wrapper with implicit conversion from distinct but bit-compatible pointer types.
Definition char16ptr.h:37
~Char16Ptr()
Destructor.
Definition char16ptr.h:132
Char16Ptr(wchar_t *p)
Converts the pointer to char16_t *.
char16_t * get() const
Pointer access.
Definition char16ptr.h:134
const char16_t * wrapper with implicit conversion from distinct but bit-compatible pointer types.
Definition char16ptr.h:142
~ConstChar16Ptr()
Destructor.
Definition char16ptr.h:238
const char16_t * get() const
Pointer access.
Definition char16ptr.h:240
ConstChar16Ptr(const wchar_t *p)
Converts the pointer to char16_t *.
const OldUChar * toOldUCharPtr(const char16_t *p)
Converts from const char16_t * to const OldUChar *.
Definition char16ptr.h:279
const UChar * toUCharPtr(const char16_t *p)
Converts from const char16_t * to const UChar *.
Definition char16ptr.h:251
uint16_t UChar
The base type for UTF-16 code units and pointers.
Definition umachine.h:353
uint16_t OldUChar
Default ICU 58 definition of UChar.
Definition umachine.h:380
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:359
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition uversion.h:138
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition uversion.h:137