args.h 7.2 KB

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