1 | // -*- C++ -*- |
2 | //===----------------------------------------------------------------------===// |
3 | // |
4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
5 | // See https://llvm.org/LICENSE.txt for license information. |
6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
7 | // |
8 | //===----------------------------------------------------------------------===// |
9 | |
10 | #ifndef _LIBCPP___THREAD_FORMATTER_H |
11 | #define _LIBCPP___THREAD_FORMATTER_H |
12 | |
13 | #include <__concepts/arithmetic.h> |
14 | #include <__config> |
15 | #include <__format/concepts.h> |
16 | #include <__format/format_parse_context.h> |
17 | #include <__format/formatter.h> |
18 | #include <__format/formatter_integral.h> |
19 | #include <__format/parser_std_format_spec.h> |
20 | #include <__thread/id.h> |
21 | #include <__type_traits/conditional.h> |
22 | #include <__type_traits/is_pointer.h> |
23 | #include <__type_traits/is_same.h> |
24 | #include <cstdint> |
25 | |
26 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
27 | # pragma GCC system_header |
28 | #endif |
29 | |
30 | #if _LIBCPP_STD_VER >= 23 |
31 | |
32 | _LIBCPP_BEGIN_NAMESPACE_STD |
33 | |
34 | # ifndef _LIBCPP_HAS_NO_THREADS |
35 | |
36 | template <__fmt_char_type _CharT> |
37 | struct _LIBCPP_TEMPLATE_VIS formatter<__thread_id, _CharT> { |
38 | public: |
39 | template <class _ParseContext> |
40 | _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) { |
41 | return __parser_.__parse(__ctx, __format_spec::__fields_fill_align_width); |
42 | } |
43 | |
44 | template <class _FormatContext> |
45 | _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator format(__thread_id __id, _FormatContext& __ctx) const { |
46 | // In __thread/support/pthread.h, __libcpp_thread_id is either a |
47 | // unsigned long long or a pthread_t. |
48 | // |
49 | // The type of pthread_t is left unspecified in POSIX so it can be any |
50 | // type. The most logical types are an integral or pointer. |
51 | // On Linux systems pthread_t is an unsigned long long. |
52 | // On Apple systems pthread_t is a pointer type. |
53 | // |
54 | // Note the output should match what the stream operator does. Since |
55 | // the ostream operator has been shipped years before this formatter |
56 | // was added to the Standard, this formatter does what the stream |
57 | // operator does. This may require platform specific changes. |
58 | |
59 | using _Tp = decltype(__get_underlying_id(__id)); |
60 | using _Cp = conditional_t<integral<_Tp>, _Tp, conditional_t<is_pointer_v<_Tp>, uintptr_t, void>>; |
61 | static_assert(!is_same_v<_Cp, void>, "unsupported thread::id type, please file a bug report" ); |
62 | |
63 | __format_spec::__parsed_specifications<_CharT> __specs = __parser_.__get_parsed_std_specifications(__ctx); |
64 | if constexpr (is_pointer_v<_Tp>) { |
65 | __specs.__std_.__alternate_form_ = true; |
66 | __specs.__std_.__type_ = __format_spec::__type::__hexadecimal_lower_case; |
67 | } |
68 | return __formatter::__format_integer(reinterpret_cast<_Cp>(__get_underlying_id(__id)), __ctx, __specs); |
69 | } |
70 | |
71 | __format_spec::__parser<_CharT> __parser_{.__alignment_ = __format_spec::__alignment::__right}; |
72 | }; |
73 | |
74 | # endif // !_LIBCPP_HAS_NO_THREADS |
75 | |
76 | _LIBCPP_END_NAMESPACE_STD |
77 | |
78 | #endif // _LIBCPP_STD_VER >= 23 |
79 | |
80 | #endif // _LIBCPP___THREAD_FORMATTER_H |
81 | |