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 
10 #ifndef _LIBCPP___FORMAT_FORMATTER_POINTER_H
11 #define _LIBCPP___FORMAT_FORMATTER_POINTER_H
12 
13 #include <__availability>
14 #include <__config>
15 #include <__format/format_fwd.h>
16 #include <__format/format_parse_context.h>
17 #include <__format/formatter.h>
18 #include <__format/formatter_integral.h>
19 #include <__format/formatter_output.h>
20 #include <__format/parser_std_format_spec.h>
21 #include <cstddef>
22 #include <cstdint>
23 
24 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
25 #  pragma GCC system_header
26 #endif
27 
28 _LIBCPP_BEGIN_NAMESPACE_STD
29 
30 #if _LIBCPP_STD_VER > 17
31 
32 template <__formatter::__char_type _CharT>
33 struct _LIBCPP_TEMPLATE_VIS __formatter_pointer {
34 public:
35   constexpr __formatter_pointer() { __parser_.__alignment_ = __format_spec::__alignment::__right; }
36 
37   _LIBCPP_HIDE_FROM_ABI constexpr auto
38   parse(basic_format_parse_context<_CharT>& __parse_ctx) -> decltype(__parse_ctx.begin()) {
39     auto __result = __parser_.__parse(__parse_ctx, __format_spec::__fields_pointer);
40     __format_spec::__process_display_type_pointer(__parser_.__type_);
41     return __result;
42   }
43 
44   _LIBCPP_HIDE_FROM_ABI auto format(const void* __ptr, auto& __ctx) const -> decltype(__ctx.out()) {
45     __format_spec::__parsed_specifications<_CharT> __specs = __parser_.__get_parsed_std_specifications(__ctx);
46     __specs.__std_.__alternate_form_                       = true;
47     __specs.__std_.__type_                                 = __format_spec::__type::__hexadecimal_lower_case;
48     return __formatter::__format_integer(reinterpret_cast<uintptr_t>(__ptr), __ctx, __specs);
49   }
50 
51   __format_spec::__parser<_CharT> __parser_;
52 };
53 
54 // [format.formatter.spec]/2.4
55 // For each charT, the pointer type specializations template<>
56 // - struct formatter<nullptr_t, charT>;
57 // - template<> struct formatter<void*, charT>;
58 // - template<> struct formatter<const void*, charT>;
59 template <__formatter::__char_type _CharT>
60 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<nullptr_t, _CharT>
61     : public __formatter_pointer<_CharT> {};
62 template <__formatter::__char_type _CharT>
63 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<void*, _CharT> : public __formatter_pointer<_CharT> {
64 };
65 template <__formatter::__char_type _CharT>
66 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<const void*, _CharT>
67     : public __formatter_pointer<_CharT> {};
68 
69 #endif //_LIBCPP_STD_VER > 17
70 
71 _LIBCPP_END_NAMESPACE_STD
72 
73 #endif // _LIBCPP___FORMAT_FORMATTER_POINTER_H
74