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
13
14#include <cstddef>
15#include <string_view>
16#include <type_traits>
17
24
25U_NAMESPACE_BEGIN
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
45public:
51 inline Char16Ptr(char16_t *p);
52#if !U_CHAR16_IS_TYPEDEF
58 inline Char16Ptr(uint16_t *p);
59#endif
60#if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN)
67 inline Char16Ptr(wchar_t *p);
68#endif
74 inline Char16Ptr(std::nullptr_t p);
79 inline ~Char16Ptr();
80
86 inline char16_t *get() const;
92 inline operator char16_t *() const { return get(); }
93
94private:
95 Char16Ptr() = delete;
96
97#ifdef U_ALIASING_BARRIER
98 template<typename T> static char16_t *cast(T *t) {
100 return reinterpret_cast<char16_t *>(t);
101 }
102
103 char16_t *p_;
104#else
105 union {
106 char16_t *cp;
107 uint16_t *up;
108 wchar_t *wp;
109 } u_;
110#endif
111};
112
114#ifdef U_ALIASING_BARRIER
115
116Char16Ptr::Char16Ptr(char16_t *p) : p_(p) {}
117#if !U_CHAR16_IS_TYPEDEF
118Char16Ptr::Char16Ptr(uint16_t *p) : p_(cast(p)) {}
119#endif
120#if U_SIZEOF_WCHAR_T==2
121Char16Ptr::Char16Ptr(wchar_t *p) : p_(cast(p)) {}
122#endif
123Char16Ptr::Char16Ptr(std::nullptr_t p) : p_(p) {}
126}
127
128char16_t *Char16Ptr::get() const { return p_; }
129
130#else
131
132Char16Ptr::Char16Ptr(char16_t *p) { u_.cp = p; }
133#if !U_CHAR16_IS_TYPEDEF
134Char16Ptr::Char16Ptr(uint16_t *p) { u_.up = p; }
135#endif
136#if U_SIZEOF_WCHAR_T==2
137Char16Ptr::Char16Ptr(wchar_t *p) { u_.wp = p; }
138#endif
139Char16Ptr::Char16Ptr(std::nullptr_t p) { u_.cp = p; }
141
142char16_t *Char16Ptr::get() const { return u_.cp; }
143
144#endif
146
152public:
158 inline ConstChar16Ptr(const char16_t *p);
159#if !U_CHAR16_IS_TYPEDEF
165 inline ConstChar16Ptr(const uint16_t *p);
166#endif
167#if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN)
174 inline ConstChar16Ptr(const wchar_t *p);
175#endif
181 inline ConstChar16Ptr(const std::nullptr_t p);
182
188
194 inline const char16_t *get() const;
200 inline operator const char16_t *() const { return get(); }
201
202private:
203 ConstChar16Ptr() = delete;
204
205#ifdef U_ALIASING_BARRIER
206 template<typename T> static const char16_t *cast(const T *t) {
208 return reinterpret_cast<const char16_t *>(t);
209 }
210
211 const char16_t *p_;
212#else
213 union {
214 const char16_t *cp;
215 const uint16_t *up;
216 const wchar_t *wp;
217 } u_;
218#endif
219};
220
222#ifdef U_ALIASING_BARRIER
223
224ConstChar16Ptr::ConstChar16Ptr(const char16_t *p) : p_(p) {}
225#if !U_CHAR16_IS_TYPEDEF
226ConstChar16Ptr::ConstChar16Ptr(const uint16_t *p) : p_(cast(p)) {}
227#endif
228#if U_SIZEOF_WCHAR_T==2
229ConstChar16Ptr::ConstChar16Ptr(const wchar_t *p) : p_(cast(p)) {}
230#endif
231ConstChar16Ptr::ConstChar16Ptr(const std::nullptr_t p) : p_(p) {}
234}
235
236const char16_t *ConstChar16Ptr::get() const { return p_; }
237
238#else
239
240ConstChar16Ptr::ConstChar16Ptr(const char16_t *p) { u_.cp = p; }
241#if !U_CHAR16_IS_TYPEDEF
242ConstChar16Ptr::ConstChar16Ptr(const uint16_t *p) { u_.up = p; }
243#endif
244#if U_SIZEOF_WCHAR_T==2
245ConstChar16Ptr::ConstChar16Ptr(const wchar_t *p) { u_.wp = p; }
246#endif
247ConstChar16Ptr::ConstChar16Ptr(const std::nullptr_t p) { u_.cp = p; }
249
250const char16_t *ConstChar16Ptr::get() const { return u_.cp; }
251
252#endif
254
262inline const UChar *toUCharPtr(const char16_t *p) {
263#ifdef U_ALIASING_BARRIER
265#endif
266 return reinterpret_cast<const UChar *>(p);
267}
268
276inline UChar *toUCharPtr(char16_t *p) {
277#ifdef U_ALIASING_BARRIER
279#endif
280 return reinterpret_cast<UChar *>(p);
281}
282
290inline const OldUChar *toOldUCharPtr(const char16_t *p) {
291#ifdef U_ALIASING_BARRIER
293#endif
294 return reinterpret_cast<const OldUChar *>(p);
295}
296
304inline OldUChar *toOldUCharPtr(char16_t *p) {
305#ifdef U_ALIASING_BARRIER
307#endif
308 return reinterpret_cast<OldUChar *>(p);
309}
310
311#ifndef U_FORCE_HIDE_INTERNAL_API
316template<typename T>
318 std::is_convertible_v<T, std::u16string_view>
319#if !U_CHAR16_IS_TYPEDEF && (!defined(_LIBCPP_VERSION) || _LIBCPP_VERSION < 180000)
320 || std::is_convertible_v<T, std::basic_string_view<uint16_t>>
321#endif
322#if U_SIZEOF_WCHAR_T==2
323 || std::is_convertible_v<T, std::wstring_view>
324#endif
325 ;
326
327namespace internal {
332inline std::u16string_view toU16StringView(std::u16string_view sv) { return sv; }
333
334#if !U_CHAR16_IS_TYPEDEF && (!defined(_LIBCPP_VERSION) || _LIBCPP_VERSION < 180000)
340inline std::u16string_view toU16StringView(std::basic_string_view<uint16_t> sv) {
341 return { ConstChar16Ptr(sv.data()), sv.length() };
342}
343#endif
344
345#if U_SIZEOF_WCHAR_T==2
351inline std::u16string_view toU16StringView(std::wstring_view sv) {
352 return { ConstChar16Ptr(sv.data()), sv.length() };
353}
354#endif
355
360template <typename T,
361 typename = typename std::enable_if_t<!std::is_pointer_v<std::remove_reference_t<T>>>>
362inline std::u16string_view toU16StringViewNullable(const T& text) {
363 return toU16StringView(text);
364}
365
370template <typename T,
371 typename = typename std::enable_if_t<std::is_pointer_v<std::remove_reference_t<T>>>,
372 typename = void>
373inline std::u16string_view toU16StringViewNullable(const T& text) {
374 if (text == nullptr) return {}; // For backward compatibility.
375 return toU16StringView(text);
376}
377
378} // internal
379#endif // U_FORCE_HIDE_INTERNAL_API
380
381U_NAMESPACE_END
382
383#endif /* U_SHOW_CPLUSPLUS_API */
384
385#endif // __CHAR16PTR_H__
std::u16string_view toU16StringView(std::u16string_view sv)
Pass-through overload.
Definition char16ptr.h:332
std::u16string_view toU16StringViewNullable(const T &text)
Pass-through overload.
Definition char16ptr.h:362
#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:44
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:151
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:317
const OldUChar * toOldUCharPtr(const char16_t *p)
Converts from const char16_t * to const OldUChar *.
Definition char16ptr.h:290
const UChar * toUCharPtr(const char16_t *p)
Converts from const char16_t * to const UChar *.
Definition char16ptr.h:262
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