formatter.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. #ifndef _LIBCPP___THREAD_FORMATTER_H
  10. #define _LIBCPP___THREAD_FORMATTER_H
  11. #include <__concepts/arithmetic.h>
  12. #include <__config>
  13. #include <__format/concepts.h>
  14. #include <__format/format_parse_context.h>
  15. #include <__format/formatter.h>
  16. #include <__format/formatter_integral.h>
  17. #include <__format/parser_std_format_spec.h>
  18. #include <__thread/id.h>
  19. #include <__type_traits/conditional.h>
  20. #include <__type_traits/is_pointer.h>
  21. #include <__type_traits/is_same.h>
  22. #include <cstdint>
  23. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  24. # pragma GCC system_header
  25. #endif
  26. #if _LIBCPP_STD_VER >= 23
  27. _LIBCPP_BEGIN_NAMESPACE_STD
  28. #ifndef _LIBCPP_HAS_NO_THREADS
  29. template <__fmt_char_type _CharT>
  30. struct _LIBCPP_TEMPLATE_VIS formatter<__thread_id, _CharT> {
  31. public:
  32. template <class _ParseContext>
  33. _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
  34. return __parser_.__parse(__ctx, __format_spec::__fields_fill_align_width);
  35. }
  36. template <class _FormatContext>
  37. _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator format(__thread_id __id, _FormatContext& __ctx) const {
  38. // In __threading_support __libcpp_thread_id is either a
  39. // unsigned long long or a pthread_t.
  40. //
  41. // The type of pthread_t is left unspecified in POSIX so it can be any
  42. // type. The most logical types are an integral or pointer.
  43. // On Linux systems pthread_t is an unsigned long long.
  44. // On Apple systems pthread_t is a pointer type.
  45. //
  46. // Note the output should match what the stream operator does. Since
  47. // the ostream operator has been shipped years before this formatter
  48. // was added to the Standard, this formatter does what the stream
  49. // operator does. This may require platform specific changes.
  50. using _Tp = decltype(__get_underlying_id(__id));
  51. using _Cp = conditional_t<integral<_Tp>, _Tp, conditional_t<is_pointer_v<_Tp>, uintptr_t, void>>;
  52. static_assert(!is_same_v<_Cp, void>, "unsupported thread::id type, please file a bug report");
  53. __format_spec::__parsed_specifications<_CharT> __specs = __parser_.__get_parsed_std_specifications(__ctx);
  54. if constexpr (is_pointer_v<_Tp>) {
  55. __specs.__std_.__alternate_form_ = true;
  56. __specs.__std_.__type_ = __format_spec::__type::__hexadecimal_lower_case;
  57. }
  58. return __formatter::__format_integer(reinterpret_cast<_Cp>(__get_underlying_id(__id)), __ctx, __specs);
  59. }
  60. __format_spec::__parser<_CharT> __parser_{.__alignment_ = __format_spec::__alignment::__right};
  61. };
  62. #endif // !_LIBCPP_HAS_NO_THREADS
  63. _LIBCPP_END_NAMESPACE_STD
  64. #endif // _LIBCPP_STD_VER >= 23
  65. #endif // _LIBCPP___THREAD_FORMATTER_H