File indexing completed on 2026-05-03 08:13:49
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef _LIBCPP___FORMAT_FORMAT_CONTEXT_H
0011 #define _LIBCPP___FORMAT_FORMAT_CONTEXT_H
0012
0013 #include <__concepts/same_as.h>
0014 #include <__config>
0015 #include <__format/buffer.h>
0016 #include <__format/format_arg.h>
0017 #include <__format/format_arg_store.h>
0018 #include <__format/format_args.h>
0019 #include <__format/format_error.h>
0020 #include <__fwd/format.h>
0021 #include <__iterator/back_insert_iterator.h>
0022 #include <__iterator/concepts.h>
0023 #include <__memory/addressof.h>
0024 #include <__utility/move.h>
0025 #include <__variant/monostate.h>
0026
0027 #if _LIBCPP_HAS_LOCALIZATION
0028 # include <__locale>
0029 # include <optional>
0030 #endif
0031
0032 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0033 # pragma GCC system_header
0034 #endif
0035
0036 _LIBCPP_PUSH_MACROS
0037 #include <__undef_macros>
0038
0039 _LIBCPP_BEGIN_NAMESPACE_STD
0040
0041 #if _LIBCPP_STD_VER >= 20
0042
0043 template <class _OutIt, class _CharT>
0044 requires output_iterator<_OutIt, const _CharT&>
0045 class _LIBCPP_TEMPLATE_VIS basic_format_context;
0046
0047 # if _LIBCPP_HAS_LOCALIZATION
0048
0049
0050
0051
0052
0053 template <class _OutIt, class _CharT>
0054 _LIBCPP_HIDE_FROM_ABI basic_format_context<_OutIt, _CharT>
0055 __format_context_create(_OutIt __out_it,
0056 basic_format_args<basic_format_context<_OutIt, _CharT>> __args,
0057 optional<std::locale>&& __loc = nullopt) {
0058 return std::basic_format_context(std::move(__out_it), __args, std::move(__loc));
0059 }
0060 # else
0061 template <class _OutIt, class _CharT>
0062 _LIBCPP_HIDE_FROM_ABI basic_format_context<_OutIt, _CharT>
0063 __format_context_create(_OutIt __out_it, basic_format_args<basic_format_context<_OutIt, _CharT>> __args) {
0064 return std::basic_format_context(std::move(__out_it), __args);
0065 }
0066 # endif
0067
0068 using format_context = basic_format_context<back_insert_iterator<__format::__output_buffer<char>>, char>;
0069 # if _LIBCPP_HAS_WIDE_CHARACTERS
0070 using wformat_context = basic_format_context< back_insert_iterator<__format::__output_buffer<wchar_t>>, wchar_t>;
0071 # endif
0072
0073 template <class _OutIt, class _CharT>
0074 requires output_iterator<_OutIt, const _CharT&>
0075 class
0076
0077 _LIBCPP_TEMPLATE_VIS
0078 _LIBCPP_PREFERRED_NAME(format_context)
0079 _LIBCPP_IF_WIDE_CHARACTERS(_LIBCPP_PREFERRED_NAME(wformat_context))
0080
0081 basic_format_context {
0082 public:
0083 using iterator = _OutIt;
0084 using char_type = _CharT;
0085 template <class _Tp>
0086 using formatter_type = formatter<_Tp, _CharT>;
0087
0088 _LIBCPP_HIDE_FROM_ABI basic_format_arg<basic_format_context> arg(size_t __id) const noexcept {
0089 return __args_.get(__id);
0090 }
0091 # if _LIBCPP_HAS_LOCALIZATION
0092 _LIBCPP_HIDE_FROM_ABI std::locale locale() {
0093 if (!__loc_)
0094 __loc_ = std::locale{};
0095 return *__loc_;
0096 }
0097 # endif
0098 _LIBCPP_HIDE_FROM_ABI iterator out() { return std::move(__out_it_); }
0099 _LIBCPP_HIDE_FROM_ABI void advance_to(iterator __it) { __out_it_ = std::move(__it); }
0100
0101 private:
0102 iterator __out_it_;
0103 basic_format_args<basic_format_context> __args_;
0104 # if _LIBCPP_HAS_LOCALIZATION
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115 optional<std::locale> __loc_;
0116
0117 template <class _OtherOutIt, class _OtherCharT>
0118 friend _LIBCPP_HIDE_FROM_ABI basic_format_context<_OtherOutIt, _OtherCharT> __format_context_create(
0119 _OtherOutIt, basic_format_args<basic_format_context<_OtherOutIt, _OtherCharT>>, optional<std::locale>&&);
0120
0121
0122 _LIBCPP_HIDE_FROM_ABI explicit basic_format_context(
0123 _OutIt __out_it, basic_format_args<basic_format_context> __args, optional<std::locale>&& __loc)
0124 : __out_it_(std::move(__out_it)), __args_(__args), __loc_(std::move(__loc)) {}
0125 # else
0126 template <class _OtherOutIt, class _OtherCharT>
0127 friend _LIBCPP_HIDE_FROM_ABI basic_format_context<_OtherOutIt, _OtherCharT>
0128 __format_context_create(_OtherOutIt, basic_format_args<basic_format_context<_OtherOutIt, _OtherCharT>>);
0129
0130 _LIBCPP_HIDE_FROM_ABI explicit basic_format_context(_OutIt __out_it, basic_format_args<basic_format_context> __args)
0131 : __out_it_(std::move(__out_it)), __args_(__args) {}
0132 # endif
0133
0134 public:
0135 basic_format_context(const basic_format_context&) = delete;
0136 basic_format_context& operator=(const basic_format_context&) = delete;
0137 };
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155 template <class _CharT>
0156 class _LIBCPP_TEMPLATE_VIS basic_format_context<typename __format::__retarget_buffer<_CharT>::__iterator, _CharT> {
0157 public:
0158 using iterator = typename __format::__retarget_buffer<_CharT>::__iterator;
0159 using char_type = _CharT;
0160 template <class _Tp>
0161 using formatter_type = formatter<_Tp, _CharT>;
0162
0163 template <class _Context>
0164 _LIBCPP_HIDE_FROM_ABI explicit basic_format_context(iterator __out_it, _Context& __ctx)
0165 : __out_it_(std::move(__out_it)),
0166 # if _LIBCPP_HAS_LOCALIZATION
0167 __loc_([](void* __c) { return static_cast<_Context*>(__c)->locale(); }),
0168 # endif
0169 __ctx_(std::addressof(__ctx)),
0170 __arg_([](void* __c, size_t __id) {
0171 auto __visitor = [&](auto __arg) -> basic_format_arg<basic_format_context> {
0172 if constexpr (same_as<decltype(__arg), monostate>)
0173 return {};
0174 else if constexpr (same_as<decltype(__arg), typename basic_format_arg<_Context>::handle>)
0175
0176
0177 std::__throw_format_error("Re-targeting handle not supported");
0178 else
0179 return basic_format_arg<basic_format_context>{
0180 __format::__determine_arg_t<basic_format_context, decltype(__arg)>(),
0181 __basic_format_arg_value<basic_format_context>(__arg)};
0182 };
0183 # if _LIBCPP_STD_VER >= 26 && _LIBCPP_HAS_EXPLICIT_THIS_PARAMETER
0184 return static_cast<_Context*>(__c)->arg(__id).visit(std::move(__visitor));
0185 # else
0186 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
0187 return std::visit_format_arg(std::move(__visitor), static_cast<_Context*>(__c)->arg(__id));
0188 _LIBCPP_SUPPRESS_DEPRECATED_POP
0189 # endif
0190 }) {
0191 }
0192
0193 _LIBCPP_HIDE_FROM_ABI basic_format_arg<basic_format_context> arg(size_t __id) const noexcept {
0194 return __arg_(__ctx_, __id);
0195 }
0196 # if _LIBCPP_HAS_LOCALIZATION
0197 _LIBCPP_HIDE_FROM_ABI std::locale locale() { return __loc_(__ctx_); }
0198 # endif
0199 _LIBCPP_HIDE_FROM_ABI iterator out() { return std::move(__out_it_); }
0200 _LIBCPP_HIDE_FROM_ABI void advance_to(iterator __it) { __out_it_ = std::move(__it); }
0201
0202 private:
0203 iterator __out_it_;
0204
0205 # if _LIBCPP_HAS_LOCALIZATION
0206 std::locale (*__loc_)(void* __ctx);
0207 # endif
0208
0209 void* __ctx_;
0210 basic_format_arg<basic_format_context> (*__arg_)(void* __ctx, size_t __id);
0211 };
0212
0213 _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(basic_format_context);
0214 #endif
0215
0216 _LIBCPP_END_NAMESPACE_STD
0217
0218 _LIBCPP_POP_MACROS
0219
0220 #endif