args.h 7.3 KB

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