Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-15 09:08:24

0001 // Formatting library for C++ - dynamic argument lists
0002 //
0003 // Copyright (c) 2012 - present, Victor Zverovich
0004 // All rights reserved.
0005 //
0006 // For the license information refer to format.h.
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 // node is defined outside dynamic_arg_list to workaround a C2504 bug in MSVC
0033 // 2022 (v17.10.0).
0034 //
0035 // Workaround for clang's -Wweak-vtables. Unlike for regular classes, for
0036 // templates it doesn't complain about inability to deduce single translation
0037 // unit for placing vtable. So node is made a fake template.
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 }  // namespace detail
0067 
0068 /**
0069  * A dynamic list of formatting arguments with storage.
0070  *
0071  * It can be implicitly converted into `fmt::basic_format_args` for passing
0072  * into type-erased formatting functions such as `fmt::vformat`.
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   // Storage of basic_format_arg must be contiguous.
0099   std::vector<basic_format_arg<Context>> data_;
0100   std::vector<detail::named_arg_info<char_type>> named_info_;
0101 
0102   // Storage of arguments not fitting into basic_format_arg must grow
0103   // without relocation because items in data_ refer to it.
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    * Adds an argument into the dynamic store for later passing to a formatting
0141    * function.
0142    *
0143    * Note that custom types and string types (but not string views) are copied
0144    * into the store dynamically allocating memory if necessary.
0145    *
0146    * **Example**:
0147    *
0148    *     fmt::dynamic_format_arg_store<fmt::format_context> store;
0149    *     store.push_back(42);
0150    *     store.push_back("abc");
0151    *     store.push_back(1.5f);
0152    *     std::string result = fmt::vformat("{} and {} and {}", store);
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    * Adds a reference to the argument into the dynamic store for later passing
0163    * to a formatting function.
0164    *
0165    * **Example**:
0166    *
0167    *     fmt::dynamic_format_arg_store<fmt::format_context> store;
0168    *     char band[] = "Rolling Stones";
0169    *     store.push_back(std::cref(band));
0170    *     band[9] = 'c'; // Changing str affects the output.
0171    *     std::string result = fmt::vformat("{}", store);
0172    *     // result == "Rolling Scones"
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    * Adds named argument into the dynamic store for later passing to a
0183    * formatting function. `std::reference_wrapper` is supported to avoid
0184    * copying of the argument. The name is always copied into the store.
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   /// Erase all elements from the store.
0199   void clear() {
0200     data_.clear();
0201     named_info_.clear();
0202     dynamic_args_ = {};
0203   }
0204 
0205   /// Reserves space to store at least `new_cap` arguments including
0206   /// `new_cap_named` named arguments.
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   /// Returns the number of elements in the store.
0215   size_t size() const noexcept { return data_.size(); }
0216 };
0217 
0218 FMT_END_NAMESPACE
0219 
0220 #endif  // FMT_ARGS_H_