args.h 7.3 KB

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