ICU 77.1 77.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 || U_SHOW_CPLUSPLUS_HEADER_API
13
14#include <cstddef>
15#include <string_view>
16#include <type_traits>
17
18#endif
19
26
32#ifdef U_ALIASING_BARRIER
33 // Use the predefined value.
34#elif (defined(__clang__) || defined(__GNUC__)) && U_PLATFORM != U_PF_BROWSER_NATIVE_CLIENT
35# define U_ALIASING_BARRIER(ptr) asm volatile("" : : "rm"(ptr) : "memory")
36#elif defined(U_IN_DOXYGEN)
37# define U_ALIASING_BARRIER(ptr)
38#endif
39
40// ICU DLL-exported
41#if U_SHOW_CPLUSPLUS_API
42
43U_NAMESPACE_BEGIN
44
50public:
56 inline Char16Ptr(char16_t *p);
57#if !U_CHAR16_IS_TYPEDEF
63 inline Char16Ptr(uint16_t *p);
64#endif
65#if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN)
72 inline Char16Ptr(wchar_t *p);
73#endif
79 inline Char16Ptr(std::nullptr_t p);
84 inline ~Char16Ptr();
85
91 inline char16_t *get() const;
97 inline operator char16_t *() const { return get(); }
98
99private:
100 Char16Ptr() = delete;
101
102#ifdef U_ALIASING_BARRIER
103 template<typename T> static char16_t *cast(T *t) {
105 return reinterpret_cast<char16_t *>(t);
106 }
107
108 char16_t *p_;
109#else
110 union {
111 char16_t *cp;
112 uint16_t *up;
113 wchar_t *wp;
114 } u_;
115#endif
116};
117
119#ifdef U_ALIASING_BARRIER
120
121Char16Ptr::Char16Ptr(char16_t *p) : p_(p) {}
122#if !U_CHAR16_IS_TYPEDEF
123Char16Ptr::Char16Ptr(uint16_t *p) : p_(cast(p)) {}
124#endif
125#if U_SIZEOF_WCHAR_T==2
126Char16Ptr::Char16Ptr(wchar_t *p) : p_(cast(p)) {}
127#endif
128Char16Ptr::Char16Ptr(std::nullptr_t p) : p_(p) {}
131}
132
133char16_t *Char16Ptr::get() const { return p_; }
134
135#else
136
137Char16Ptr::Char16Ptr(char16_t *p) { u_.cp = p; }
138#if !U_CHAR16_IS_TYPEDEF
139Char16Ptr::Char16Ptr(uint16_t *p) { u_.up = p; }
140#endif
141#if U_SIZEOF_WCHAR_T==2
142Char16Ptr::Char16Ptr(wchar_t *p) { u_.wp = p; }
143#endif
144Char16Ptr::Char16Ptr(std::nullptr_t p) { u_.cp = p; }
146
147char16_t *Char16Ptr::get() const { return u_.cp; }
148
149#endif
151
157public:
163 inline ConstChar16Ptr(const char16_t *p);
164#if !U_CHAR16_IS_TYPEDEF
170 inline ConstChar16Ptr(const uint16_t *p);
171#endif
172#if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN)
179 inline ConstChar16Ptr(const wchar_t *p);
180#endif
186 inline ConstChar16Ptr(const std::nullptr_t p);
187
193
199 inline const char16_t *get() const;
205 inline operator const char16_t *() const { return get(); }
206
207private:
208 ConstChar16Ptr() = delete;
209
210#ifdef U_ALIASING_BARRIER
211 template<typename T> static const char16_t *cast(const T *t) {
213 return reinterpret_cast<const char16_t *>(t);
214 }
215
216 const char16_t *p_;
217#else
218 union {
219 const char16_t *cp;
220 const uint16_t *up;
221 const wchar_t *wp;
222 } u_;
223#endif
224};
225
227#ifdef U_ALIASING_BARRIER
228
229ConstChar16Ptr::ConstChar16Ptr(const char16_t *p) : p_(p) {}
230#if !U_CHAR16_IS_TYPEDEF
231ConstChar16Ptr::ConstChar16Ptr(const uint16_t *p) : p_(cast(p)) {}
232#endif
233#if U_SIZEOF_WCHAR_T==2
234ConstChar16Ptr::ConstChar16Ptr(const wchar_t *p) : p_(cast(p)) {}
235#endif
236ConstChar16Ptr::ConstChar16Ptr(const std::nullptr_t p) : p_(p) {}
239}
240
241const char16_t *ConstChar16Ptr::get() const { return p_; }
242
243#else
244
245ConstChar16Ptr::ConstChar16Ptr(const char16_t *p) { u_.cp = p; }
246#if !U_CHAR16_IS_TYPEDEF
247ConstChar16Ptr::ConstChar16Ptr(const uint16_t *p) { u_.up = p; }
248#endif
249#if U_SIZEOF_WCHAR_T==2
250ConstChar16Ptr::ConstChar16Ptr(const wchar_t *p) { u_.wp = p; }
251#endif
252ConstChar16Ptr::ConstChar16Ptr(const std::nullptr_t p) { u_.cp = p; }
254
255const char16_t *ConstChar16Ptr::get() const { return u_.cp; }
256
257#endif
259
260U_NAMESPACE_END
261
262#endif // U_SHOW_CPLUSPLUS_API
263
264// Usable in header-only definitions
265#if U_SHOW_CPLUSPLUS_API || U_SHOW_CPLUSPLUS_HEADER_API
266
267namespace U_ICU_NAMESPACE_OR_INTERNAL {
268
269#ifndef U_FORCE_HIDE_INTERNAL_API
271template<typename T, typename = std::enable_if_t<std::is_same_v<T, UChar>>>
272inline const char16_t *uprv_char16PtrFromUChar(const T *p) {
273 if constexpr (std::is_same_v<UChar, char16_t>) {
274 return p;
275 } else {
276#if U_SHOW_CPLUSPLUS_API
277 return ConstChar16Ptr(p).get();
278#else
279#ifdef U_ALIASING_BARRIER
281#endif
282 return reinterpret_cast<const char16_t *>(p);
283#endif
284 }
285}
286#if !U_CHAR16_IS_TYPEDEF && (!defined(_LIBCPP_VERSION) || _LIBCPP_VERSION < 180000)
288inline const char16_t *uprv_char16PtrFromUint16(const uint16_t *p) {
289#if U_SHOW_CPLUSPLUS_API
290 return ConstChar16Ptr(p).get();
291#else
292#ifdef U_ALIASING_BARRIER
294#endif
295 return reinterpret_cast<const char16_t *>(p);
296#endif
297}
298#endif
299#if U_SIZEOF_WCHAR_T==2
301inline const char16_t *uprv_char16PtrFromWchar(const wchar_t *p) {
302#if U_SHOW_CPLUSPLUS_API
303 return ConstChar16Ptr(p).get();
304#else
305#ifdef U_ALIASING_BARRIER
307#endif
308 return reinterpret_cast<const char16_t *>(p);
309#endif
310}
311#endif
312#endif
313
321inline const UChar *toUCharPtr(const char16_t *p) {
322#ifdef U_ALIASING_BARRIER
324#endif
325 return reinterpret_cast<const UChar *>(p);
326}
327
335inline UChar *toUCharPtr(char16_t *p) {
336#ifdef U_ALIASING_BARRIER
338#endif
339 return reinterpret_cast<UChar *>(p);
340}
341
349inline const OldUChar *toOldUCharPtr(const char16_t *p) {
350#ifdef U_ALIASING_BARRIER
352#endif
353 return reinterpret_cast<const OldUChar *>(p);
354}
355
363inline OldUChar *toOldUCharPtr(char16_t *p) {
364#ifdef U_ALIASING_BARRIER
366#endif
367 return reinterpret_cast<OldUChar *>(p);
368}
369
370} // U_ICU_NAMESPACE_OR_INTERNAL
371
372#endif // U_SHOW_CPLUSPLUS_API || U_SHOW_CPLUSPLUS_HEADER_API
373
374// ICU DLL-exported
375#if U_SHOW_CPLUSPLUS_API
376
377U_NAMESPACE_BEGIN
378
379#ifndef U_FORCE_HIDE_INTERNAL_API
384template<typename T>
386 std::is_convertible_v<T, std::u16string_view>
387#if !U_CHAR16_IS_TYPEDEF && (!defined(_LIBCPP_VERSION) || _LIBCPP_VERSION < 180000)
388 || std::is_convertible_v<T, std::basic_string_view<uint16_t>>
389#endif
390#if U_SIZEOF_WCHAR_T==2
391 || std::is_convertible_v<T, std::wstring_view>
392#endif
393 ;
394
395namespace internal {
400inline std::u16string_view toU16StringView(std::u16string_view sv) { return sv; }
401
402#if !U_CHAR16_IS_TYPEDEF && (!defined(_LIBCPP_VERSION) || _LIBCPP_VERSION < 180000)
408inline std::u16string_view toU16StringView(std::basic_string_view<uint16_t> sv) {
409 return { ConstChar16Ptr(sv.data()), sv.length() };
410}
411#endif
412
413#if U_SIZEOF_WCHAR_T==2
419inline std::u16string_view toU16StringView(std::wstring_view sv) {
420 return { ConstChar16Ptr(sv.data()), sv.length() };
421}
422#endif
423
428template <typename T,
429 typename = typename std::enable_if_t<!std::is_pointer_v<std::remove_reference_t<T>>>>
430inline std::u16string_view toU16StringViewNullable(const T& text) {
431 return toU16StringView(text);
432}
433
438template <typename T,
439 typename = typename std::enable_if_t<std::is_pointer_v<std::remove_reference_t<T>>>,
440 typename = void>
441inline std::u16string_view toU16StringViewNullable(const T& text) {
442 if (text == nullptr) return {}; // For backward compatibility.
443 return toU16StringView(text);
444}
445
446} // internal
447#endif // U_FORCE_HIDE_INTERNAL_API
448
449U_NAMESPACE_END
450
451#endif // U_SHOW_CPLUSPLUS_API
452
453#endif // __CHAR16PTR_H__
const OldUChar * toOldUCharPtr(const char16_t *p)
Converts from const char16_t * to const OldUChar *.
Definition char16ptr.h:349
std::u16string_view toU16StringView(std::u16string_view sv)
Pass-through overload.
Definition char16ptr.h:400
const char16_t * uprv_char16PtrFromUint16(const uint16_t *p)
Definition char16ptr.h:288
const char16_t * uprv_char16PtrFromUChar(const T *p)
Definition char16ptr.h:272
const UChar * toUCharPtr(const char16_t *p)
Converts from const char16_t * to const UChar *.
Definition char16ptr.h:321
std::u16string_view toU16StringViewNullable(const T &text)
Pass-through overload.
Definition char16ptr.h:430
#define U_ALIASING_BARRIER(ptr)
Barrier for pointer anti-aliasing optimizations even across function boundaries.
Definition char16ptr.h:37
char16_t * wrapper with implicit conversion from distinct but bit-compatible pointer types.
Definition char16ptr.h:49
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:156
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.
constexpr bool ConvertibleToU16StringView
Is T convertible to a std::u16string_view or some other 16-bit string view?
Definition char16ptr.h:385
char16_t UChar
The base type for UTF-16 code units and pointers.
Definition umachine.h:378
uint16_t OldUChar
Default ICU 58 definition of UChar.
Definition umachine.h:407
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:315