xref: /freebsd-src/contrib/llvm-project/libcxx/include/__format/container_adaptor.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1bdd1243dSDimitry Andric // -*- C++ -*-
2bdd1243dSDimitry Andric //===----------------------------------------------------------------------===//
3bdd1243dSDimitry Andric //
4bdd1243dSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5bdd1243dSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
6bdd1243dSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7bdd1243dSDimitry Andric //
8bdd1243dSDimitry Andric //===----------------------------------------------------------------------===//
9bdd1243dSDimitry Andric 
10bdd1243dSDimitry Andric #ifndef _LIBCPP___FORMAT_CONTAINER_ADAPTOR_H
11bdd1243dSDimitry Andric #define _LIBCPP___FORMAT_CONTAINER_ADAPTOR_H
12bdd1243dSDimitry Andric 
13bdd1243dSDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
14bdd1243dSDimitry Andric #  pragma GCC system_header
15bdd1243dSDimitry Andric #endif
16bdd1243dSDimitry Andric 
17bdd1243dSDimitry Andric #include <__config>
18bdd1243dSDimitry Andric #include <__format/concepts.h>
19bdd1243dSDimitry Andric #include <__format/formatter.h>
20bdd1243dSDimitry Andric #include <__format/range_default_formatter.h>
21*0fca6ea1SDimitry Andric #include <__fwd/queue.h>
22*0fca6ea1SDimitry Andric #include <__fwd/stack.h>
2306c3fb27SDimitry Andric #include <__ranges/ref_view.h>
2406c3fb27SDimitry Andric #include <__type_traits/is_const.h>
2506c3fb27SDimitry Andric #include <__type_traits/maybe_const.h>
26bdd1243dSDimitry Andric 
27bdd1243dSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
28bdd1243dSDimitry Andric 
2906c3fb27SDimitry Andric #if _LIBCPP_STD_VER >= 23
30bdd1243dSDimitry Andric 
31bdd1243dSDimitry Andric // [container.adaptors.format] only specifies the library should provide the
32bdd1243dSDimitry Andric // formatter specializations, not which header should provide them.
33bdd1243dSDimitry Andric // Since <format> includes a lot of headers, add these headers here instead of
34bdd1243dSDimitry Andric // adding more dependencies like, locale, optinal, string, tuple, etc. to the
35bdd1243dSDimitry Andric // adaptor headers. To use the format functions users already include <format>.
36bdd1243dSDimitry Andric 
37bdd1243dSDimitry Andric template <class _Adaptor, class _CharT>
3806c3fb27SDimitry Andric struct _LIBCPP_TEMPLATE_VIS __formatter_container_adaptor {
39bdd1243dSDimitry Andric private:
4006c3fb27SDimitry Andric   using __maybe_const_container = __fmt_maybe_const<typename _Adaptor::container_type, _CharT>;
4106c3fb27SDimitry Andric   using __maybe_const_adaptor   = __maybe_const<is_const_v<__maybe_const_container>, _Adaptor>;
4206c3fb27SDimitry Andric   formatter<ranges::ref_view<__maybe_const_container>, _CharT> __underlying_;
43bdd1243dSDimitry Andric 
44bdd1243dSDimitry Andric public:
45bdd1243dSDimitry Andric   template <class _ParseContext>
46bdd1243dSDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
47bdd1243dSDimitry Andric     return __underlying_.parse(__ctx);
48bdd1243dSDimitry Andric   }
49bdd1243dSDimitry Andric 
50bdd1243dSDimitry Andric   template <class _FormatContext>
51bdd1243dSDimitry Andric   _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator
52bdd1243dSDimitry Andric   format(__maybe_const_adaptor& __adaptor, _FormatContext& __ctx) const {
53bdd1243dSDimitry Andric     return __underlying_.format(__adaptor.__get_container(), __ctx);
54bdd1243dSDimitry Andric   }
55bdd1243dSDimitry Andric };
56bdd1243dSDimitry Andric 
57bdd1243dSDimitry Andric template <class _CharT, class _Tp, formattable<_CharT> _Container>
5806c3fb27SDimitry Andric struct _LIBCPP_TEMPLATE_VIS formatter<queue<_Tp, _Container>, _CharT>
59bdd1243dSDimitry Andric     : public __formatter_container_adaptor<queue<_Tp, _Container>, _CharT> {};
60bdd1243dSDimitry Andric 
61bdd1243dSDimitry Andric template <class _CharT, class _Tp, class _Container, class _Compare>
6206c3fb27SDimitry Andric struct _LIBCPP_TEMPLATE_VIS formatter<priority_queue<_Tp, _Container, _Compare>, _CharT>
63bdd1243dSDimitry Andric     : public __formatter_container_adaptor<priority_queue<_Tp, _Container, _Compare>, _CharT> {};
64bdd1243dSDimitry Andric 
65bdd1243dSDimitry Andric template <class _CharT, class _Tp, formattable<_CharT> _Container>
6606c3fb27SDimitry Andric struct _LIBCPP_TEMPLATE_VIS formatter<stack<_Tp, _Container>, _CharT>
67bdd1243dSDimitry Andric     : public __formatter_container_adaptor<stack<_Tp, _Container>, _CharT> {};
68bdd1243dSDimitry Andric 
6906c3fb27SDimitry Andric #endif //_LIBCPP_STD_VER >= 23
70bdd1243dSDimitry Andric 
71bdd1243dSDimitry Andric _LIBCPP_END_NAMESPACE_STD
72bdd1243dSDimitry Andric 
73bdd1243dSDimitry Andric #endif // _LIBCPP___FORMAT_CONTAINER_ADAPTOR_H
74