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