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 
37 class U_COMMON_API Char16Ptr U_FINAL {
38 public:
44  inline Char16Ptr(char16_t *p);
45 #if !U_CHAR16_IS_TYPEDEF
46 
51  inline Char16Ptr(uint16_t *p);
52 #endif
53 #if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN)
54 
60  inline Char16Ptr(wchar_t *p);
61 #endif
62 
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 
87 private:
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 
108 Char16Ptr::Char16Ptr(char16_t *p) : p_(p) {}
109 #if !U_CHAR16_IS_TYPEDEF
110 Char16Ptr::Char16Ptr(uint16_t *p) : p_(cast(p)) {}
111 #endif
112 #if U_SIZEOF_WCHAR_T==2
113 Char16Ptr::Char16Ptr(wchar_t *p) : p_(cast(p)) {}
114 #endif
115 Char16Ptr::Char16Ptr(std::nullptr_t p) : p_(p) {}
116 Char16Ptr::~Char16Ptr() {
117  U_ALIASING_BARRIER(p_);
118 }
119 
120 char16_t *Char16Ptr::get() const { return p_; }
121 
122 #else
123 
124 Char16Ptr::Char16Ptr(char16_t *p) { u_.cp = p; }
125 #if !U_CHAR16_IS_TYPEDEF
126 Char16Ptr::Char16Ptr(uint16_t *p) { u_.up = p; }
127 #endif
128 #if U_SIZEOF_WCHAR_T==2
129 Char16Ptr::Char16Ptr(wchar_t *p) { u_.wp = p; }
130 #endif
131 Char16Ptr::Char16Ptr(std::nullptr_t p) { u_.cp = p; }
132 Char16Ptr::~Char16Ptr() {}
133 
134 char16_t *Char16Ptr::get() const { return u_.cp; }
135 
136 #endif
137 
143 public:
149  inline ConstChar16Ptr(const char16_t *p);
150 #if !U_CHAR16_IS_TYPEDEF
151 
156  inline ConstChar16Ptr(const uint16_t *p);
157 #endif
158 #if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN)
159 
165  inline ConstChar16Ptr(const wchar_t *p);
166 #endif
167 
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 
193 private:
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 
214 ConstChar16Ptr::ConstChar16Ptr(const char16_t *p) : p_(p) {}
215 #if !U_CHAR16_IS_TYPEDEF
216 ConstChar16Ptr::ConstChar16Ptr(const uint16_t *p) : p_(cast(p)) {}
217 #endif
218 #if U_SIZEOF_WCHAR_T==2
219 ConstChar16Ptr::ConstChar16Ptr(const wchar_t *p) : p_(cast(p)) {}
220 #endif
221 ConstChar16Ptr::ConstChar16Ptr(const std::nullptr_t p) : p_(p) {}
222 ConstChar16Ptr::~ConstChar16Ptr() {
223  U_ALIASING_BARRIER(p_);
224 }
225 
226 const char16_t *ConstChar16Ptr::get() const { return p_; }
227 
228 #else
229 
230 ConstChar16Ptr::ConstChar16Ptr(const char16_t *p) { u_.cp = p; }
231 #if !U_CHAR16_IS_TYPEDEF
232 ConstChar16Ptr::ConstChar16Ptr(const uint16_t *p) { u_.up = p; }
233 #endif
234 #if U_SIZEOF_WCHAR_T==2
235 ConstChar16Ptr::ConstChar16Ptr(const wchar_t *p) { u_.wp = p; }
236 #endif
237 ConstChar16Ptr::ConstChar16Ptr(const std::nullptr_t p) { u_.cp = p; }
238 ConstChar16Ptr::~ConstChar16Ptr() {}
239 
240 const char16_t *ConstChar16Ptr::get() const { return u_.cp; }
241 
242 #endif
243 
251 inline 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 
265 inline 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 
279 inline 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 
293 inline 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__
uint16_t OldUChar
Default ICU 58 definition of UChar.
Definition: umachine.h:380
OldUChar * toOldUCharPtr(char16_t *p)
Converts from char16_t * to OldUChar *.
Definition: char16ptr.h:293
#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:265
uint16_t UChar
The base type for UTF-16 code units and pointers.
Definition: umachine.h:353
#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:37
const char16_t * wrapper with implicit conversion from distinct but bit-compatible pointer types...
Definition: char16ptr.h:142
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