• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_CONTAINER_ADAPTOR_H
11 #define _LIBCPP___FORMAT_CONTAINER_ADAPTOR_H
12 
13 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
14 #  pragma GCC system_header
15 #endif
16 
17 #include <__availability>
18 #include <__config>
19 #include <__format/concepts.h>
20 #include <__format/formatter.h>
21 #include <__format/range_default_formatter.h>
22 #include <__ranges/all.h>
23 #include <__ranges/ref_view.h>
24 #include <__type_traits/is_const.h>
25 #include <__type_traits/maybe_const.h>
26 #include <queue>
27 #include <stack>
28 
29 _LIBCPP_BEGIN_NAMESPACE_STD
30 
31 #if _LIBCPP_STD_VER >= 23
32 
33 // [container.adaptors.format] only specifies the library should provide the
34 // formatter specializations, not which header should provide them.
35 // Since <format> includes a lot of headers, add these headers here instead of
36 // adding more dependencies like, locale, optinal, string, tuple, etc. to the
37 // adaptor headers. To use the format functions users already include <format>.
38 
39 template <class _Adaptor, class _CharT>
40 struct _LIBCPP_TEMPLATE_VIS __formatter_container_adaptor {
41 private:
42   using __maybe_const_container = __fmt_maybe_const<typename _Adaptor::container_type, _CharT>;
43   using __maybe_const_adaptor   = __maybe_const<is_const_v<__maybe_const_container>, _Adaptor>;
44   formatter<ranges::ref_view<__maybe_const_container>, _CharT> __underlying_;
45 
46 public:
47   template <class _ParseContext>
parse__formatter_container_adaptor48   _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
49     return __underlying_.parse(__ctx);
50   }
51 
52   template <class _FormatContext>
53   _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator
format__formatter_container_adaptor54   format(__maybe_const_adaptor& __adaptor, _FormatContext& __ctx) const {
55     return __underlying_.format(__adaptor.__get_container(), __ctx);
56   }
57 };
58 
59 template <class _CharT, class _Tp, formattable<_CharT> _Container>
60 struct _LIBCPP_TEMPLATE_VIS formatter<queue<_Tp, _Container>, _CharT>
61     : public __formatter_container_adaptor<queue<_Tp, _Container>, _CharT> {};
62 
63 template <class _CharT, class _Tp, class _Container, class _Compare>
64 struct _LIBCPP_TEMPLATE_VIS formatter<priority_queue<_Tp, _Container, _Compare>, _CharT>
65     : public __formatter_container_adaptor<priority_queue<_Tp, _Container, _Compare>, _CharT> {};
66 
67 template <class _CharT, class _Tp, formattable<_CharT> _Container>
68 struct _LIBCPP_TEMPLATE_VIS formatter<stack<_Tp, _Container>, _CharT>
69     : public __formatter_container_adaptor<stack<_Tp, _Container>, _CharT> {};
70 
71 #endif //_LIBCPP_STD_VER >= 23
72 
73 _LIBCPP_END_NAMESPACE_STD
74 
75 #endif // _LIBCPP___FORMAT_CONTAINER_ADAPTOR_H
76