File indexing completed on 2026-09-15 09:08:24
0001
0002
0003
0004
0005
0006
0007
0008 #ifndef FMT_ARGS_H_
0009 #define FMT_ARGS_H_
0010
0011 #ifndef FMT_MODULE
0012 # include <functional> // std::reference_wrapper
0013 # include <memory> // std::unique_ptr
0014 # include <vector>
0015 #endif
0016
0017 #include "format.h" // std_string_view
0018
0019 FMT_BEGIN_NAMESPACE
0020 namespace detail {
0021
0022 template <typename T> struct is_reference_wrapper : std::false_type {};
0023 template <typename T>
0024 struct is_reference_wrapper<std::reference_wrapper<T>> : std::true_type {};
0025
0026 template <typename T> auto unwrap(const T& v) -> const T& { return v; }
0027 template <typename T>
0028 auto unwrap(const std::reference_wrapper<T>& v) -> const T& {
0029 return static_cast<const T&>(v);
0030 }
0031
0032
0033
0034
0035
0036
0037
0038 template <typename = void> struct node {
0039 virtual ~node() = default;
0040 std::unique_ptr<node<>> next;
0041 };
0042
0043 class dynamic_arg_list {
0044 template <typename T> struct typed_node : node<> {
0045 T value;
0046
0047 template <typename Arg>
0048 FMT_CONSTEXPR typed_node(const Arg& arg) : value(arg) {}
0049
0050 template <typename Char>
0051 FMT_CONSTEXPR typed_node(const basic_string_view<Char>& arg)
0052 : value(arg.data(), arg.size()) {}
0053 };
0054
0055 std::unique_ptr<node<>> head_;
0056
0057 public:
0058 template <typename T, typename Arg> auto push(const Arg& arg) -> const T& {
0059 auto new_node = std::unique_ptr<typed_node<T>>(new typed_node<T>(arg));
0060 auto& value = new_node->value;
0061 new_node->next = std::move(head_);
0062 head_ = std::move(new_node);
0063 return value;
0064 }
0065 };
0066 }
0067
0068
0069
0070
0071
0072
0073
0074 template <typename Context> class dynamic_format_arg_store {
0075 private:
0076 using char_type = typename Context::char_type;
0077
0078 template <typename T> struct need_copy {
0079 static constexpr detail::type mapped_type =
0080 detail::mapped_type_constant<T, char_type>::value;
0081
0082 enum {
0083 value = !(detail::is_reference_wrapper<T>::value ||
0084 std::is_same<T, basic_string_view<char_type>>::value ||
0085 std::is_same<T, detail::std_string_view<char_type>>::value ||
0086 (mapped_type != detail::type::cstring_type &&
0087 mapped_type != detail::type::string_type &&
0088 mapped_type != detail::type::custom_type))
0089 };
0090 };
0091
0092 template <typename T>
0093 using stored_t = conditional_t<
0094 std::is_convertible<T, std::basic_string<char_type>>::value &&
0095 !detail::is_reference_wrapper<T>::value,
0096 std::basic_string<char_type>, T>;
0097
0098
0099 std::vector<basic_format_arg<Context>> data_;
0100 std::vector<detail::named_arg_info<char_type>> named_info_;
0101
0102
0103
0104 detail::dynamic_arg_list dynamic_args_;
0105
0106 friend class basic_format_args<Context>;
0107
0108 auto data() const -> const basic_format_arg<Context>* {
0109 return named_info_.empty() ? data_.data() : data_.data() + 1;
0110 }
0111
0112 template <typename T> void emplace_arg(const T& arg) {
0113 data_.emplace_back(arg);
0114 }
0115
0116 template <typename T>
0117 void emplace_arg(const detail::named_arg<char_type, T>& arg) {
0118 if (named_info_.empty())
0119 data_.insert(data_.begin(), basic_format_arg<Context>(nullptr, 0));
0120 data_.emplace_back(detail::unwrap(arg.value));
0121 auto pop_one = [](std::vector<basic_format_arg<Context>>* data) {
0122 data->pop_back();
0123 };
0124 std::unique_ptr<std::vector<basic_format_arg<Context>>, decltype(pop_one)>
0125 guard{&data_, pop_one};
0126 named_info_.push_back({arg.name, static_cast<int>(data_.size() - 2u)});
0127 data_[0] = {named_info_.data(), named_info_.size()};
0128 guard.release();
0129 }
0130
0131 public:
0132 constexpr dynamic_format_arg_store() = default;
0133
0134 operator basic_format_args<Context>() const {
0135 return basic_format_args<Context>(data(), static_cast<int>(data_.size()),
0136 !named_info_.empty());
0137 }
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154 template <typename T> void push_back(const T& arg) {
0155 if (detail::const_check(need_copy<T>::value))
0156 emplace_arg(dynamic_args_.push<stored_t<T>>(arg));
0157 else
0158 emplace_arg(detail::unwrap(arg));
0159 }
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174 template <typename T> void push_back(std::reference_wrapper<T> arg) {
0175 static_assert(
0176 need_copy<T>::value,
0177 "objects of built-in types and string views are always copied");
0178 emplace_arg(arg.get());
0179 }
0180
0181
0182
0183
0184
0185
0186 template <typename T>
0187 void push_back(const detail::named_arg<char_type, T>& arg) {
0188 const char_type* arg_name =
0189 dynamic_args_.push<std::basic_string<char_type>>(arg.name).c_str();
0190 if (detail::const_check(need_copy<T>::value)) {
0191 emplace_arg(
0192 fmt::arg(arg_name, dynamic_args_.push<stored_t<T>>(arg.value)));
0193 } else {
0194 emplace_arg(fmt::arg(arg_name, arg.value));
0195 }
0196 }
0197
0198
0199 void clear() {
0200 data_.clear();
0201 named_info_.clear();
0202 dynamic_args_ = {};
0203 }
0204
0205
0206
0207 void reserve(size_t new_cap, size_t new_cap_named) {
0208 FMT_ASSERT(new_cap >= new_cap_named,
0209 "set of arguments includes set of named arguments");
0210 data_.reserve(new_cap);
0211 named_info_.reserve(new_cap_named);
0212 }
0213
0214
0215 size_t size() const noexcept { return data_.size(); }
0216 };
0217
0218 FMT_END_NAMESPACE
0219
0220 #endif