| 1 |
// Formatting library for C++ - dynamic argument lists |
| 2 |
// |
| 3 |
// Copyright (c) 2012 - present, Victor Zverovich and {fmt} contributors |
| 4 |
// All rights reserved. |
| 5 |
// |
| 6 |
// For the license information refer to format.h. |
| 7 |
|
| 8 |
#ifndef FMT_ARGS_H_ |
| 9 |
#define FMT_ARGS_H_ |
| 10 |
|
| 11 |
#ifndef FMT_MODULE |
| 12 |
# include <functional> // std::reference_wrapper |
| 13 |
# include <memory> // std::unique_ptr |
| 14 |
# include <vector> |
| 15 |
#endif |
| 16 |
|
| 17 |
#include "format.h" // std_string_view |
| 18 |
|
| 19 |
FMT_BEGIN_NAMESPACE |
| 20 |
namespace detail { |
| 21 |
|
| 22 |
template <typename T> struct is_reference_wrapper : std::false_type {}; |
| 23 |
template <typename T> |
| 24 |
struct is_reference_wrapper<std::reference_wrapper<T>> : std::true_type {}; |
| 25 |
|
| 26 |
template <typename T> auto unwrap(const T& v) -> const T& { return v; } |
| 27 |
template <typename T> |
| 28 |
auto unwrap(const std::reference_wrapper<T>& v) -> const T& { |
| 29 |
return static_cast<const T&>(v); |
| 30 |
} |
| 31 |
|
| 32 |
// node is defined outside dynamic_arg_list to workaround a C2504 bug in MSVC |
| 33 |
// 2022 (v17.10.0). |
| 34 |
// |
| 35 |
// Workaround for clang's -Wweak-vtables. Unlike for regular classes, for |
| 36 |
// templates it doesn't complain about inability to deduce single translation |
| 37 |
// unit for placing vtable. So node is made a fake template. |
| 38 |
template <typename = void> struct node { |
| 39 |
virtual ~node() = default; |
| 40 |
std::unique_ptr<node<>> next; |
| 41 |
}; |
| 42 |
|
| 43 |
class dynamic_arg_list { |
| 44 |
template <typename T> struct typed_node : node<> { |
| 45 |
T value; |
| 46 |
|
| 47 |
template <typename Arg> |
| 48 |
FMT_CONSTEXPR typed_node(const Arg& arg) : value(arg) {} |
| 49 |
|
| 50 |
template <typename Char> |
| 51 |
FMT_CONSTEXPR typed_node(const basic_string_view<Char>& arg) |
| 52 |
: value(arg.data(), arg.size()) {} |
| 53 |
}; |
| 54 |
|
| 55 |
std::unique_ptr<node<>> head_; |
| 56 |
|
| 57 |
public: |
| 58 |
template <typename T, typename Arg> auto push(const Arg& arg) -> const T& { |
| 59 |
auto new_node = std::unique_ptr<typed_node<T>>(new typed_node<T>(arg)); |
| 60 |
auto& value = new_node->value; |
| 61 |
new_node->next = std::move(head_); |
| 62 |
head_ = std::move(new_node); |
| 63 |
return value; |
| 64 |
} |
| 65 |
}; |
| 66 |
} // namespace detail |
| 67 |
|
| 68 |
/** |
| 69 |
* A dynamic list of formatting arguments with storage. |
| 70 |
* |
| 71 |
* It can be implicitly converted into `fmt::basic_format_args` for passing |
| 72 |
* into type-erased formatting functions such as `fmt::vformat`. |
| 73 |
*/ |
| 74 |
FMT_EXPORT template <typename Context> class dynamic_format_arg_store { |
| 75 |
private: |
| 76 |
using char_type = typename Context::char_type; |
| 77 |
|
| 78 |
template <typename T> struct need_copy { |
| 79 |
static constexpr detail::type mapped_type = |
| 80 |
detail::mapped_type_constant<T, char_type>::value; |
| 81 |
|
| 82 |
enum { |
| 83 |
value = !(detail::is_reference_wrapper<T>::value || |
| 84 |
std::is_same<T, basic_string_view<char_type>>::value || |
| 85 |
std::is_same<T, detail::std_string_view<char_type>>::value || |
| 86 |
(mapped_type != detail::type::cstring_type && |
| 87 |
mapped_type != detail::type::string_type && |
| 88 |
mapped_type != detail::type::custom_type)) |
| 89 |
}; |
| 90 |
}; |
| 91 |
|
| 92 |
template <typename T> |
| 93 |
using stored_t = conditional_t< |
| 94 |
std::is_convertible<T, std::basic_string<char_type>>::value && |
| 95 |
!detail::is_reference_wrapper<T>::value, |
| 96 |
std::basic_string<char_type>, T>; |
| 97 |
|
| 98 |
// Storage of basic_format_arg must be contiguous. |
| 99 |
std::vector<basic_format_arg<Context>> data_; |
| 100 |
std::vector<detail::named_arg_info<char_type>> named_info_; |
| 101 |
|
| 102 |
// Storage of arguments not fitting into basic_format_arg must grow |
| 103 |
// without relocation because items in data_ refer to it. |
| 104 |
detail::dynamic_arg_list dynamic_args_; |
| 105 |
|
| 106 |
friend class basic_format_args<Context>; |
| 107 |
|
| 108 |
auto data() const -> const basic_format_arg<Context>* { |
| 109 |
return named_info_.empty() ? data_.data() : data_.data() + 1; |
| 110 |
} |
| 111 |
|
| 112 |
template <typename T> void emplace_arg(const T& arg) { |
| 113 |
data_.emplace_back(arg); |
| 114 |
} |
| 115 |
|
| 116 |
template <typename T> void emplace_arg(const named_arg<T, char_type>& arg) { |
| 117 |
if (named_info_.empty()) |
| 118 |
data_.insert(data_.begin(), basic_format_arg<Context>(nullptr, 0)); |
| 119 |
data_.emplace_back(detail::unwrap(arg.value)); |
| 120 |
auto pop_one = [](std::vector<basic_format_arg<Context>>* data) { |
| 121 |
data->pop_back(); |
| 122 |
}; |
| 123 |
std::unique_ptr<std::vector<basic_format_arg<Context>>, decltype(pop_one)> |
| 124 |
guard{&data_, pop_one}; |
| 125 |
named_info_.push_back({arg.name, static_cast<int>(data_.size() - 2u)}); |
| 126 |
data_[0] = {named_info_.data(), named_info_.size()}; |
| 127 |
guard.release(); |
| 128 |
} |
| 129 |
|
| 130 |
public: |
| 131 |
constexpr dynamic_format_arg_store() = default; |
| 132 |
|
| 133 |
operator basic_format_args<Context>() const { |
| 134 |
return basic_format_args<Context>(data(), static_cast<int>(data_.size()), |
| 135 |
!named_info_.empty()); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Adds an argument into the dynamic store for later passing to a formatting |
| 140 |
* function. |
| 141 |
* |
| 142 |
* Note that custom types and string types (but not string views) are copied |
| 143 |
* into the store dynamically allocating memory if necessary. |
| 144 |
* |
| 145 |
* **Example**: |
| 146 |
* |
| 147 |
* fmt::dynamic_format_arg_store<fmt::format_context> store; |
| 148 |
* store.push_back(42); |
| 149 |
* store.push_back("abc"); |
| 150 |
* store.push_back(1.5f); |
| 151 |
* std::string result = fmt::vformat("{} and {} and {}", store); |
| 152 |
*/ |
| 153 |
template <typename T> void push_back(const T& arg) { |
| 154 |
if FMT_CONSTEXPR20 (need_copy<T>::value) |
| 155 |
emplace_arg(dynamic_args_.push<stored_t<T>>(arg)); |
| 156 |
else |
| 157 |
emplace_arg(detail::unwrap(arg)); |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Adds a reference to the argument into the dynamic store for later passing |
| 162 |
* to a formatting function. |
| 163 |
* |
| 164 |
* **Example**: |
| 165 |
* |
| 166 |
* fmt::dynamic_format_arg_store<fmt::format_context> store; |
| 167 |
* char band[] = "Rolling Stones"; |
| 168 |
* store.push_back(std::cref(band)); |
| 169 |
* band[9] = 'c'; // Changing str affects the output. |
| 170 |
* std::string result = fmt::vformat("{}", store); |
| 171 |
* // result == "Rolling Scones" |
| 172 |
*/ |
| 173 |
template <typename T> void push_back(std::reference_wrapper<T> arg) { |
| 174 |
static_assert( |
| 175 |
need_copy<T>::value, |
| 176 |
"objects of built-in types and string views are always copied"); |
| 177 |
emplace_arg(arg.get()); |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Adds named argument into the dynamic store for later passing to a |
| 182 |
* formatting function. `std::reference_wrapper` is supported to avoid |
| 183 |
* copying of the argument. The name is always copied into the store. |
| 184 |
*/ |
| 185 |
template <typename T> void push_back(const named_arg<T, char_type>& arg) { |
| 186 |
const char_type* arg_name = |
| 187 |
dynamic_args_.push<std::basic_string<char_type>>(arg.name).c_str(); |
| 188 |
if FMT_CONSTEXPR20 (need_copy<T>::value) { |
| 189 |
emplace_arg( |
| 190 |
fmt::arg(arg_name, dynamic_args_.push<stored_t<T>>(arg.value))); |
| 191 |
} else { |
| 192 |
emplace_arg(fmt::arg(arg_name, arg.value)); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
/// Erase all elements from the store. |
| 197 |
void clear() { |
| 198 |
data_.clear(); |
| 199 |
named_info_.clear(); |
| 200 |
dynamic_args_ = {}; |
| 201 |
} |
| 202 |
|
| 203 |
/// Reserves space to store at least `new_cap` arguments including |
| 204 |
/// `new_cap_named` named arguments. |
| 205 |
void reserve(size_t new_cap, size_t new_cap_named) { |
| 206 |
FMT_ASSERT(new_cap >= new_cap_named, |
| 207 |
"set of arguments includes set of named arguments"); |
| 208 |
data_.reserve(new_cap); |
| 209 |
named_info_.reserve(new_cap_named); |
| 210 |
} |
| 211 |
|
| 212 |
/// Returns the number of elements in the store. |
| 213 |
auto size() const noexcept -> size_t { return data_.size(); } |
| 214 |
}; |
| 215 |
|
| 216 |
FMT_END_NAMESPACE |
| 217 |
|
| 218 |
#endif // FMT_ARGS_H_ |