ICU 76.1 76.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#endif
287
295inline const UChar *toUCharPtr(const char16_t *p) {
296#ifdef U_ALIASING_BARRIER
298#endif
299 return reinterpret_cast<const UChar *>(p);
300}
301
309inline UChar *toUCharPtr(char16_t *p) {
310#ifdef U_ALIASING_BARRIER
312#endif
313 return reinterpret_cast<UChar *>(p);
314}
315
323inline const OldUChar *toOldUCharPtr(const char16_t *p) {
324#ifdef U_ALIASING_BARRIER
326#endif
327 return reinterpret_cast<const OldUChar *>(p);
328}
329
337inline OldUChar *toOldUCharPtr(char16_t *p) {
338#ifdef U_ALIASING_BARRIER
340#endif
341 return reinterpret_cast<OldUChar *>(p);
342}
343
344} // U_ICU_NAMESPACE_OR_INTERNAL
345
346#endif // U_SHOW_CPLUSPLUS_API || U_SHOW_CPLUSPLUS_HEADER_API
347
348// ICU DLL-exported
349#if U_SHOW_CPLUSPLUS_API
350
351U_NAMESPACE_BEGIN
352
353#ifndef U_FORCE_HIDE_INTERNAL_API
358template<typename T>
360 std::is_convertible_v<T, std::u16string_view>
361#if !U_CHAR16_IS_TYPEDEF && (!defined(_LIBCPP_VERSION) || _LIBCPP_VERSION < 180000)
362 || std::is_convertible_v<T, std::basic_string_view<uint16_t>>
363#endif
364#if U_SIZEOF_WCHAR_T==2
365 || std::is_convertible_v<T, std::wstring_view>
366#endif
367 ;
368
369namespace internal {
374inline std::u16string_view toU16StringView(std::u16string_view sv) { return sv; }
375
376#if !U_CHAR16_IS_TYPEDEF && (!defined(_LIBCPP_VERSION) || _LIBCPP_VERSION < 180000)
382inline std::u16string_view toU16StringView(std::basic_string_view<uint16_t> sv) {
383 return { ConstChar16Ptr(sv.data()), sv.length() };
384}
385#endif
386
387#if U_SIZEOF_WCHAR_T==2
393inline std::u16string_view toU16StringView(std::wstring_view sv) {
394 return { ConstChar16Ptr(sv.data()), sv.length() };
395}
396#endif
397
402template <typename T,
403 typename = typename std::enable_if_t<!std::is_pointer_v<std::remove_reference_t<T>>>>
404inline std::u16string_view toU16StringViewNullable(const T& text) {
405 return toU16StringView(text);
406}
407
412template <typename T,
413 typename = typename std::enable_if_t<std::is_pointer_v<std::remove_reference_t<T>>>,
414 typename = void>
415inline std::u16string_view toU16StringViewNullable(const T& text) {
416 if (text == nullptr) return {}; // For backward compatibility.
417 return toU16StringView(text);
418}
419
420} // internal
421#endif // U_FORCE_HIDE_INTERNAL_API
422
423U_NAMESPACE_END
424
425#endif // U_SHOW_CPLUSPLUS_API
426
427#endif // __CHAR16PTR_H__
const OldUChar * toOldUCharPtr(const char16_t *p)
Converts from const char16_t * to const OldUChar *.
Definition char16ptr.h:323
std::u16string_view toU16StringView(std::u16string_view sv)
Pass-through overload.
Definition char16ptr.h:374
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:295
std::u16string_view toU16StringViewNullable(const T &text)
Pass-through overload.
Definition char16ptr.h:404
#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:359
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