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