2 //===----------------------------------------------------------------------===//
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
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___FORMAT_FORMAT_CONTEXT_H
11 #define _LIBCPP___FORMAT_FORMAT_CONTEXT_H
13 #include <__availability>
14 #include <__concepts/same_as.h>
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>
29 #ifndef _LIBCPP_HAS_NO_LOCALIZATION
34 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
35 # pragma GCC system_header
38 _LIBCPP_BEGIN_NAMESPACE_STD
40 #if _LIBCPP_STD_VER >= 20
42 template <class _OutIt
, class _CharT
>
43 requires output_iterator
<_OutIt
, const _CharT
&>
44 class _LIBCPP_TEMPLATE_VIS basic_format_context
;
46 #ifndef _LIBCPP_HAS_NO_LOCALIZATION
48 * Helper to create a basic_format_context.
50 * This is needed since the constructor is private.
52 template <class _OutIt
, class _CharT
>
53 _LIBCPP_HIDE_FROM_ABI basic_format_context
<_OutIt
, _CharT
>
54 __format_context_create(
56 basic_format_args
<basic_format_context
<_OutIt
, _CharT
>> __args
,
57 optional
<_VSTD::locale
>&& __loc
= nullopt
) {
58 return _VSTD::basic_format_context(_VSTD::move(__out_it
), __args
, _VSTD::move(__loc
));
61 template <class _OutIt
, class _CharT
>
62 _LIBCPP_HIDE_FROM_ABI basic_format_context
<_OutIt
, _CharT
>
63 __format_context_create(
65 basic_format_args
<basic_format_context
<_OutIt
, _CharT
>> __args
) {
66 return _VSTD::basic_format_context(_VSTD::move(__out_it
), __args
);
70 using format_context
=
71 basic_format_context
<back_insert_iterator
<__format::__output_buffer
<char>>,
73 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
74 using wformat_context
= basic_format_context
<
75 back_insert_iterator
<__format::__output_buffer
<wchar_t>>, wchar_t>;
78 template <class _OutIt
, class _CharT
>
79 requires output_iterator
<_OutIt
, const _CharT
&>
83 _LIBCPP_PREFERRED_NAME(format_context
)
84 _LIBCPP_IF_WIDE_CHARACTERS(_LIBCPP_PREFERRED_NAME(wformat_context
))
86 basic_format_context
{
88 using iterator
= _OutIt
;
89 using char_type
= _CharT
;
91 using formatter_type
= formatter
<_Tp
, _CharT
>;
93 _LIBCPP_HIDE_FROM_ABI basic_format_arg
<basic_format_context
>
94 arg(size_t __id
) const noexcept
{
95 return __args_
.get(__id
);
97 #ifndef _LIBCPP_HAS_NO_LOCALIZATION
98 _LIBCPP_HIDE_FROM_ABI
_VSTD::locale
locale() {
100 __loc_
= _VSTD::locale
{};
104 _LIBCPP_HIDE_FROM_ABI iterator
out() { return std::move(__out_it_
); }
105 _LIBCPP_HIDE_FROM_ABI
void advance_to(iterator __it
) { __out_it_
= std::move(__it
); }
109 basic_format_args
<basic_format_context
> __args_
;
110 #ifndef _LIBCPP_HAS_NO_LOCALIZATION
112 // The Standard doesn't specify how the locale is stored.
113 // [format.context]/6
114 // std::locale locale();
115 // Returns: The locale passed to the formatting function if the latter
116 // takes one, and std::locale() otherwise.
117 // This is done by storing the locale of the constructor in this optional. If
118 // locale() is called and the optional has no value the value will be created.
119 // This allows the implementation to lazily create the locale.
120 // TODO FMT Validate whether lazy creation is the best solution.
121 optional
<_VSTD::locale
> __loc_
;
123 template <class _OtherOutIt
, class _OtherCharT
>
124 friend _LIBCPP_HIDE_FROM_ABI basic_format_context
<_OtherOutIt
, _OtherCharT
>
125 __format_context_create(_OtherOutIt
, basic_format_args
<basic_format_context
<_OtherOutIt
, _OtherCharT
>>,
126 optional
<_VSTD::locale
>&&);
128 // Note: the Standard doesn't specify the required constructors.
129 _LIBCPP_HIDE_FROM_ABI
130 explicit basic_format_context(_OutIt __out_it
,
131 basic_format_args
<basic_format_context
> __args
,
132 optional
<_VSTD::locale
>&& __loc
)
133 : __out_it_(_VSTD::move(__out_it
)), __args_(__args
),
134 __loc_(_VSTD::move(__loc
)) {}
136 template <class _OtherOutIt
, class _OtherCharT
>
137 friend _LIBCPP_HIDE_FROM_ABI basic_format_context
<_OtherOutIt
, _OtherCharT
>
138 __format_context_create(_OtherOutIt
, basic_format_args
<basic_format_context
<_OtherOutIt
, _OtherCharT
>>);
140 _LIBCPP_HIDE_FROM_ABI
141 explicit basic_format_context(_OutIt __out_it
,
142 basic_format_args
<basic_format_context
> __args
)
143 : __out_it_(_VSTD::move(__out_it
)), __args_(__args
) {}
147 // A specialization for __retarget_buffer
149 // See __retarget_buffer for the motivation for this specialization.
151 // This context holds a reference to the instance of the basic_format_context
152 // that is retargeted. It converts a formatting argument when it is requested
153 // during formatting. It is expected that the usage of the arguments is rare so
154 // the lookups are not expected to be used often. An alternative would be to
155 // convert all elements during construction.
157 // The elements of the retargets context are only used when an underlying
158 // formatter uses a locale specific formatting or an formatting argument is
159 // part for the format spec. For example
160 // format("{:256:{}}", input, 8);
161 // Here the width of an element in input is determined dynamically.
162 // Note when the top-level element has no width the retargeting is not needed.
163 template <class _CharT
>
164 class _LIBCPP_TEMPLATE_VIS
165 basic_format_context
<typename
__format::__retarget_buffer
<_CharT
>::__iterator
, _CharT
> {
167 using iterator
= typename
__format::__retarget_buffer
<_CharT
>::__iterator
;
168 using char_type
= _CharT
;
170 using formatter_type
= formatter
<_Tp
, _CharT
>;
172 template <class _Context
>
173 _LIBCPP_HIDE_FROM_ABI
explicit basic_format_context(iterator __out_it
, _Context
& __ctx
)
174 : __out_it_(std::move(__out_it
)),
175 # ifndef _LIBCPP_HAS_NO_LOCALIZATION
176 __loc_([](void* __c
) { return static_cast<_Context
*>(__c
)->locale(); }),
178 __ctx_(std::addressof(__ctx
)),
179 __arg_([](void* __c
, size_t __id
) {
180 return std::visit_format_arg(
181 [&](auto __arg
) -> basic_format_arg
<basic_format_context
> {
182 if constexpr (same_as
<decltype(__arg
), monostate
>)
184 else if constexpr (same_as
<decltype(__arg
), typename basic_format_arg
<_Context
>::handle
>)
185 // At the moment it's not possible for formatting to use a re-targeted handle.
186 // TODO FMT add this when support is needed.
187 std::__throw_format_error("Re-targeting handle not supported");
189 return basic_format_arg
<basic_format_context
>{
190 __format::__determine_arg_t
<basic_format_context
, decltype(__arg
)>(),
191 __basic_format_arg_value
<basic_format_context
>(__arg
)};
193 static_cast<_Context
*>(__c
)->arg(__id
));
197 _LIBCPP_HIDE_FROM_ABI basic_format_arg
<basic_format_context
> arg(size_t __id
) const noexcept
{
198 return __arg_(__ctx_
, __id
);
200 # ifndef _LIBCPP_HAS_NO_LOCALIZATION
201 _LIBCPP_HIDE_FROM_ABI
_VSTD::locale
locale() { return __loc_(__ctx_
); }
203 _LIBCPP_HIDE_FROM_ABI iterator
out() { return std::move(__out_it_
); }
204 _LIBCPP_HIDE_FROM_ABI
void advance_to(iterator __it
) { __out_it_
= std::move(__it
); }
209 # ifndef _LIBCPP_HAS_NO_LOCALIZATION
210 std::locale (*__loc_
)(void* __ctx
);
214 basic_format_arg
<basic_format_context
> (*__arg_
)(void* __ctx
, size_t __id
);
217 _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(basic_format_context
);
218 #endif //_LIBCPP_STD_VER >= 20
220 _LIBCPP_END_NAMESPACE_STD
222 #endif // _LIBCPP___FORMAT_FORMAT_CONTEXT_H