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 <queue>
23 #include <stack>
24 
25 _LIBCPP_BEGIN_NAMESPACE_STD
26 
27 #if _LIBCPP_STD_VER > 20
28 
29 // [container.adaptors.format] only specifies the library should provide the
30 // formatter specializations, not which header should provide them.
31 // Since <format> includes a lot of headers, add these headers here instead of
32 // adding more dependencies like, locale, optinal, string, tuple, etc. to the
33 // adaptor headers. To use the format functions users already include <format>.
34 
35 template <class _Adaptor, class _CharT>
36 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT __formatter_container_adaptor {
37 private:
38   using __maybe_const_adaptor = __fmt_maybe_const<_Adaptor, _CharT>;
39   formatter<typename _Adaptor::container_type, _CharT> __underlying_;
40 
41 public:
42   template <class _ParseContext>
43   _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
44     return __underlying_.parse(__ctx);
45   }
46 
47   template <class _FormatContext>
48   _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator
49   format(__maybe_const_adaptor& __adaptor, _FormatContext& __ctx) const {
50     return __underlying_.format(__adaptor.__get_container(), __ctx);
51   }
52 };
53 
54 template <class _CharT, class _Tp, formattable<_CharT> _Container>
55 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<queue<_Tp, _Container>, _CharT>
56     : public __formatter_container_adaptor<queue<_Tp, _Container>, _CharT> {};
57 
58 template <class _CharT, class _Tp, class _Container, class _Compare>
59 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<priority_queue<_Tp, _Container, _Compare>, _CharT>
60     : public __formatter_container_adaptor<priority_queue<_Tp, _Container, _Compare>, _CharT> {};
61 
62 template <class _CharT, class _Tp, formattable<_CharT> _Container>
63 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<stack<_Tp, _Container>, _CharT>
64     : public __formatter_container_adaptor<stack<_Tp, _Container>, _CharT> {};
65 
66 #endif //_LIBCPP_STD_VER > 20
67 
68 _LIBCPP_END_NAMESPACE_STD
69 
70 #endif // _LIBCPP___FORMAT_CONTAINER_ADAPTOR_H
71