xref: /freebsd/contrib/llvm-project/libcxx/include/__format/container_adaptor.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
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 <__fwd/queue.h>
22 #include <__fwd/stack.h>
23 #include <__ranges/ref_view.h>
24 #include <__type_traits/is_const.h>
25 #include <__type_traits/maybe_const.h>
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 
37 template <class _Adaptor, class _CharT>
38 struct _LIBCPP_TEMPLATE_VIS __formatter_container_adaptor {
39 private:
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 
44 public:
45   template <class _ParseContext>
parse__formatter_container_adaptor46   _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
format__formatter_container_adaptor52   format(__maybe_const_adaptor& __adaptor, _FormatContext& __ctx) const {
53     return __underlying_.format(__adaptor.__get_container(), __ctx);
54   }
55 };
56 
57 template <class _CharT, class _Tp, formattable<_CharT> _Container>
58 struct _LIBCPP_TEMPLATE_VIS formatter<queue<_Tp, _Container>, _CharT>
59     : public __formatter_container_adaptor<queue<_Tp, _Container>, _CharT> {};
60 
61 template <class _CharT, class _Tp, class _Container, class _Compare>
62 struct _LIBCPP_TEMPLATE_VIS formatter<priority_queue<_Tp, _Container, _Compare>, _CharT>
63     : public __formatter_container_adaptor<priority_queue<_Tp, _Container, _Compare>, _CharT> {};
64 
65 template <class _CharT, class _Tp, formattable<_CharT> _Container>
66 struct _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