xref: /freebsd-src/contrib/llvm-project/libcxx/include/__format/formatter_pointer.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
104eeddc0SDimitry Andric // -*- C++ -*-
204eeddc0SDimitry Andric //===----------------------------------------------------------------------===//
304eeddc0SDimitry Andric //
404eeddc0SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
504eeddc0SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
604eeddc0SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
704eeddc0SDimitry Andric //
804eeddc0SDimitry Andric //===----------------------------------------------------------------------===//
904eeddc0SDimitry Andric 
1004eeddc0SDimitry Andric #ifndef _LIBCPP___FORMAT_FORMATTER_POINTER_H
1104eeddc0SDimitry Andric #define _LIBCPP___FORMAT_FORMATTER_POINTER_H
1204eeddc0SDimitry Andric 
1304eeddc0SDimitry Andric #include <__config>
14bdd1243dSDimitry Andric #include <__format/concepts.h>
1581ad6265SDimitry Andric #include <__format/format_parse_context.h>
1604eeddc0SDimitry Andric #include <__format/formatter.h>
1704eeddc0SDimitry Andric #include <__format/formatter_integral.h>
1881ad6265SDimitry Andric #include <__format/formatter_output.h>
1904eeddc0SDimitry Andric #include <__format/parser_std_format_spec.h>
2081ad6265SDimitry Andric #include <cstddef>
2104eeddc0SDimitry Andric #include <cstdint>
2204eeddc0SDimitry Andric 
2304eeddc0SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
2404eeddc0SDimitry Andric #  pragma GCC system_header
2504eeddc0SDimitry Andric #endif
2604eeddc0SDimitry Andric 
2704eeddc0SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
2804eeddc0SDimitry Andric 
2906c3fb27SDimitry Andric #if _LIBCPP_STD_VER >= 20
3004eeddc0SDimitry Andric 
31bdd1243dSDimitry Andric template <__fmt_char_type _CharT>
3281ad6265SDimitry Andric struct _LIBCPP_TEMPLATE_VIS __formatter_pointer {
3304eeddc0SDimitry Andric public:
3406c3fb27SDimitry Andric   template <class _ParseContext>
3506c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
3606c3fb27SDimitry Andric     typename _ParseContext::iterator __result = __parser_.__parse(__ctx, __format_spec::__fields_pointer);
3706c3fb27SDimitry Andric     __format_spec::__process_display_type_pointer(__parser_.__type_, "a pointer");
3881ad6265SDimitry Andric     return __result;
3904eeddc0SDimitry Andric   }
4004eeddc0SDimitry Andric 
4106c3fb27SDimitry Andric   template <class _FormatContext>
4206c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator format(const void* __ptr, _FormatContext& __ctx) const {
4381ad6265SDimitry Andric     __format_spec::__parsed_specifications<_CharT> __specs = __parser_.__get_parsed_std_specifications(__ctx);
4481ad6265SDimitry Andric     __specs.__std_.__alternate_form_                       = true;
4506c3fb27SDimitry Andric     __specs.__std_.__type_ =
4606c3fb27SDimitry Andric         __specs.__std_.__type_ == __format_spec::__type::__pointer_upper_case
4706c3fb27SDimitry Andric             ? __format_spec::__type::__hexadecimal_upper_case
4806c3fb27SDimitry Andric             : __format_spec::__type::__hexadecimal_lower_case;
4906c3fb27SDimitry Andric 
5081ad6265SDimitry Andric     return __formatter::__format_integer(reinterpret_cast<uintptr_t>(__ptr), __ctx, __specs);
5181ad6265SDimitry Andric   }
5281ad6265SDimitry Andric 
5381ad6265SDimitry Andric   __format_spec::__parser<_CharT> __parser_;
5481ad6265SDimitry Andric };
5504eeddc0SDimitry Andric 
5604eeddc0SDimitry Andric // [format.formatter.spec]/2.4
5704eeddc0SDimitry Andric // For each charT, the pointer type specializations template<>
5804eeddc0SDimitry Andric // - struct formatter<nullptr_t, charT>;
5904eeddc0SDimitry Andric // - template<> struct formatter<void*, charT>;
6004eeddc0SDimitry Andric // - template<> struct formatter<const void*, charT>;
61bdd1243dSDimitry Andric template <__fmt_char_type _CharT>
62*cb14a3feSDimitry Andric struct _LIBCPP_TEMPLATE_VIS formatter<nullptr_t, _CharT> : public __formatter_pointer<_CharT> {};
63bdd1243dSDimitry Andric template <__fmt_char_type _CharT>
64*cb14a3feSDimitry Andric struct _LIBCPP_TEMPLATE_VIS formatter<void*, _CharT> : public __formatter_pointer<_CharT> {};
65bdd1243dSDimitry Andric template <__fmt_char_type _CharT>
66*cb14a3feSDimitry Andric struct _LIBCPP_TEMPLATE_VIS formatter<const void*, _CharT> : public __formatter_pointer<_CharT> {};
6704eeddc0SDimitry Andric 
6806c3fb27SDimitry Andric #endif //_LIBCPP_STD_VER >= 20
6904eeddc0SDimitry Andric 
7004eeddc0SDimitry Andric _LIBCPP_END_NAMESPACE_STD
7104eeddc0SDimitry Andric 
7204eeddc0SDimitry Andric #endif // _LIBCPP___FORMAT_FORMATTER_POINTER_H
73