xref: /freebsd-src/contrib/llvm-project/libcxx/include/__format/format_context.h (revision 647cbc5de815c5651677bf8582797f716ec7b48d)
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_FORMAT_CONTEXT_H
11 #define _LIBCPP___FORMAT_FORMAT_CONTEXT_H
12 
13 #include <__availability>
14 #include <__concepts/same_as.h>
15 #include <__config>
16 #include <__format/buffer.h>
17 #include <__format/format_arg.h>
18 #include <__format/format_arg_store.h>
19 #include <__format/format_args.h>
20 #include <__format/format_error.h>
21 #include <__format/format_fwd.h>
22 #include <__iterator/back_insert_iterator.h>
23 #include <__iterator/concepts.h>
24 #include <__memory/addressof.h>
25 #include <__utility/move.h>
26 #include <__variant/monostate.h>
27 #include <cstddef>
28 
29 #ifndef _LIBCPP_HAS_NO_LOCALIZATION
30 #  include <locale>
31 #  include <optional>
32 #endif
33 
34 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
35 #  pragma GCC system_header
36 #endif
37 
38 _LIBCPP_BEGIN_NAMESPACE_STD
39 
40 #if _LIBCPP_STD_VER >= 20
41 
42 template <class _OutIt, class _CharT>
43   requires output_iterator<_OutIt, const _CharT&>
44 class _LIBCPP_TEMPLATE_VIS basic_format_context;
45 
46 #  ifndef _LIBCPP_HAS_NO_LOCALIZATION
47 /**
48  * Helper to create a basic_format_context.
49  *
50  * This is needed since the constructor is private.
51  */
52 template <class _OutIt, class _CharT>
53 _LIBCPP_HIDE_FROM_ABI basic_format_context<_OutIt, _CharT>
54 __format_context_create(_OutIt __out_it,
55                         basic_format_args<basic_format_context<_OutIt, _CharT>> __args,
56                         optional<std::locale>&& __loc = nullopt) {
57   return std::basic_format_context(std::move(__out_it), __args, std::move(__loc));
58 }
59 #  else
60 template <class _OutIt, class _CharT>
61 _LIBCPP_HIDE_FROM_ABI basic_format_context<_OutIt, _CharT>
62 __format_context_create(_OutIt __out_it, basic_format_args<basic_format_context<_OutIt, _CharT>> __args) {
63   return std::basic_format_context(std::move(__out_it), __args);
64 }
65 #  endif
66 
67 using format_context = basic_format_context<back_insert_iterator<__format::__output_buffer<char>>, char>;
68 #  ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
69 using wformat_context = basic_format_context< back_insert_iterator<__format::__output_buffer<wchar_t>>, wchar_t>;
70 #  endif
71 
72 template <class _OutIt, class _CharT>
73   requires output_iterator<_OutIt, const _CharT&>
74 class
75     // clang-format off
76     _LIBCPP_TEMPLATE_VIS
77     _LIBCPP_PREFERRED_NAME(format_context)
78     _LIBCPP_IF_WIDE_CHARACTERS(_LIBCPP_PREFERRED_NAME(wformat_context))
79     // clang-format on
80     basic_format_context {
81 public:
82   using iterator  = _OutIt;
83   using char_type = _CharT;
84   template <class _Tp>
85   using formatter_type = formatter<_Tp, _CharT>;
86 
87   _LIBCPP_HIDE_FROM_ABI basic_format_arg<basic_format_context> arg(size_t __id) const noexcept {
88     return __args_.get(__id);
89   }
90 #  ifndef _LIBCPP_HAS_NO_LOCALIZATION
91   _LIBCPP_HIDE_FROM_ABI std::locale locale() {
92     if (!__loc_)
93       __loc_ = std::locale{};
94     return *__loc_;
95   }
96 #  endif
97   _LIBCPP_HIDE_FROM_ABI iterator out() { return std::move(__out_it_); }
98   _LIBCPP_HIDE_FROM_ABI void advance_to(iterator __it) { __out_it_ = std::move(__it); }
99 
100 private:
101   iterator __out_it_;
102   basic_format_args<basic_format_context> __args_;
103 #  ifndef _LIBCPP_HAS_NO_LOCALIZATION
104 
105   // The Standard doesn't specify how the locale is stored.
106   // [format.context]/6
107   // std::locale locale();
108   //   Returns: The locale passed to the formatting function if the latter
109   //   takes one, and std::locale() otherwise.
110   // This is done by storing the locale of the constructor in this optional. If
111   // locale() is called and the optional has no value the value will be created.
112   // This allows the implementation to lazily create the locale.
113   // TODO FMT Validate whether lazy creation is the best solution.
114   optional<std::locale> __loc_;
115 
116   template <class _OtherOutIt, class _OtherCharT>
117   friend _LIBCPP_HIDE_FROM_ABI basic_format_context<_OtherOutIt, _OtherCharT> __format_context_create(
118       _OtherOutIt, basic_format_args<basic_format_context<_OtherOutIt, _OtherCharT>>, optional<std::locale>&&);
119 
120   // Note: the Standard doesn't specify the required constructors.
121   _LIBCPP_HIDE_FROM_ABI explicit basic_format_context(
122       _OutIt __out_it, basic_format_args<basic_format_context> __args, optional<std::locale>&& __loc)
123       : __out_it_(std::move(__out_it)), __args_(__args), __loc_(std::move(__loc)) {}
124 #  else
125   template <class _OtherOutIt, class _OtherCharT>
126   friend _LIBCPP_HIDE_FROM_ABI basic_format_context<_OtherOutIt, _OtherCharT>
127       __format_context_create(_OtherOutIt, basic_format_args<basic_format_context<_OtherOutIt, _OtherCharT>>);
128 
129   _LIBCPP_HIDE_FROM_ABI explicit basic_format_context(_OutIt __out_it, basic_format_args<basic_format_context> __args)
130       : __out_it_(std::move(__out_it)), __args_(__args) {}
131 #  endif
132 };
133 
134 // A specialization for __retarget_buffer
135 //
136 // See __retarget_buffer for the motivation for this specialization.
137 //
138 // This context holds a reference to the instance of the basic_format_context
139 // that is retargeted. It converts a formatting argument when it is requested
140 // during formatting. It is expected that the usage of the arguments is rare so
141 // the lookups are not expected to be used often. An alternative would be to
142 // convert all elements during construction.
143 //
144 // The elements of the retargets context are only used when an underlying
145 // formatter uses a locale specific formatting or an formatting argument is
146 // part for the format spec. For example
147 //   format("{:256:{}}", input, 8);
148 // Here the width of an element in input is determined dynamically.
149 // Note when the top-level element has no width the retargeting is not needed.
150 template <class _CharT>
151 class _LIBCPP_TEMPLATE_VIS basic_format_context<typename __format::__retarget_buffer<_CharT>::__iterator, _CharT> {
152 public:
153   using iterator  = typename __format::__retarget_buffer<_CharT>::__iterator;
154   using char_type = _CharT;
155   template <class _Tp>
156   using formatter_type = formatter<_Tp, _CharT>;
157 
158   template <class _Context>
159   _LIBCPP_HIDE_FROM_ABI explicit basic_format_context(iterator __out_it, _Context& __ctx)
160       : __out_it_(std::move(__out_it)),
161 #  ifndef _LIBCPP_HAS_NO_LOCALIZATION
162         __loc_([](void* __c) { return static_cast<_Context*>(__c)->locale(); }),
163 #  endif
164         __ctx_(std::addressof(__ctx)),
165         __arg_([](void* __c, size_t __id) {
166           return std::visit_format_arg(
167               [&](auto __arg) -> basic_format_arg<basic_format_context> {
168                 if constexpr (same_as<decltype(__arg), monostate>)
169                   return {};
170                 else if constexpr (same_as<decltype(__arg), typename basic_format_arg<_Context>::handle>)
171                   // At the moment it's not possible for formatting to use a re-targeted handle.
172                   // TODO FMT add this when support is needed.
173                   std::__throw_format_error("Re-targeting handle not supported");
174                 else
175                   return basic_format_arg<basic_format_context>{
176                       __format::__determine_arg_t<basic_format_context, decltype(__arg)>(),
177                       __basic_format_arg_value<basic_format_context>(__arg)};
178               },
179               static_cast<_Context*>(__c)->arg(__id));
180         }) {
181   }
182 
183   _LIBCPP_HIDE_FROM_ABI basic_format_arg<basic_format_context> arg(size_t __id) const noexcept {
184     return __arg_(__ctx_, __id);
185   }
186 #  ifndef _LIBCPP_HAS_NO_LOCALIZATION
187   _LIBCPP_HIDE_FROM_ABI std::locale locale() { return __loc_(__ctx_); }
188 #  endif
189   _LIBCPP_HIDE_FROM_ABI iterator out() { return std::move(__out_it_); }
190   _LIBCPP_HIDE_FROM_ABI void advance_to(iterator __it) { __out_it_ = std::move(__it); }
191 
192 private:
193   iterator __out_it_;
194 
195 #  ifndef _LIBCPP_HAS_NO_LOCALIZATION
196   std::locale (*__loc_)(void* __ctx);
197 #  endif
198 
199   void* __ctx_;
200   basic_format_arg<basic_format_context> (*__arg_)(void* __ctx, size_t __id);
201 };
202 
203 _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(basic_format_context);
204 #endif //_LIBCPP_STD_VER >= 20
205 
206 _LIBCPP_END_NAMESPACE_STD
207 
208 #endif // _LIBCPP___FORMAT_FORMAT_CONTEXT_H
209