2 //===----------------------------------------------------------------------===//
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
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___THREAD_FORMATTER_H
11 #define _LIBCPP___THREAD_FORMATTER_H
13 #include <__concepts/arithmetic.h>
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>
26 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
27 # pragma GCC system_header
30 #if _LIBCPP_STD_VER >= 23
32 _LIBCPP_BEGIN_NAMESPACE_STD
34 #ifndef _LIBCPP_HAS_NO_THREADS
36 template <__fmt_char_type _CharT
>
37 struct _LIBCPP_TEMPLATE_VIS formatter
<__thread_id
, _CharT
> {
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
);
44 template <class _FormatContext
>
45 _LIBCPP_HIDE_FROM_ABI typename
_FormatContext::iterator
format(__thread_id __id
, _FormatContext
& __ctx
) const {
46 // In __threading_support __libcpp_thread_id is either a
47 // unsigned long long or a pthread_t.
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.
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.
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");
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
;
68 return __formatter::__format_integer(reinterpret_cast<_Cp
>(__get_underlying_id(__id
)), __ctx
, __specs
);
71 __format_spec::__parser
<_CharT
> __parser_
{.__alignment_
= __format_spec::__alignment::__right
};
74 #endif // !_LIBCPP_HAS_NO_THREADS
76 _LIBCPP_END_NAMESPACE_STD
78 #endif // _LIBCPP_STD_VER >= 23
80 #endif // _LIBCPP___THREAD_FORMATTER_H