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