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