ampsci
c++ program for high-precision atomic structure calculations of single-valence systems
args.h
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 
8 #ifndef FMT_ARGS_H_
9 #define FMT_ARGS_H_
10 
11 #ifdef __GNUC__
12 // Avoid tons of warnings with root code
13 #pragma GCC system_header
14 #endif
15 #ifdef __clang__
16 // Avoid tons of warnings with root code
17 #pragma clang system_header
18 #endif
19 
20 #include <functional> // std::reference_wrapper
21 #include <memory> // std::unique_ptr
22 #include <vector>
23 
24 #include "core.h"
25 
26 FMT_BEGIN_NAMESPACE
27 
28 namespace detail {
29 
30 template <typename T> struct is_reference_wrapper : std::false_type {};
31 template <typename T>
32 struct is_reference_wrapper<std::reference_wrapper<T>> : std::true_type {};
33 
34 template <typename T> const T &unwrap(const T &v) { return v; }
35 template <typename T> const T &unwrap(const std::reference_wrapper<T> &v) {
36  return static_cast<const T &>(v);
37 }
38 
39 class dynamic_arg_list {
40  // Workaround for clang's -Wweak-vtables. Unlike for regular classes, for
41  // templates it doesn't complain about inability to deduce single translation
42  // unit for placing vtable. So storage_node_base is made a fake template.
43  template <typename = void> struct node {
44  virtual ~node() = default;
45  std::unique_ptr<node<>> next;
46  };
47 
48  template <typename T> struct typed_node : node<> {
49  T value;
50 
51  template <typename Arg>
52  FMT_CONSTEXPR typed_node(const Arg &arg) : value(arg) {}
53 
54  template <typename Char>
55  FMT_CONSTEXPR typed_node(const basic_string_view<Char> &arg)
56  : value(arg.data(), arg.size()) {}
57  };
58 
59  std::unique_ptr<node<>> head_;
60 
61 public:
62  template <typename T, typename Arg> const T &push(const Arg &arg) {
63  auto new_node = std::unique_ptr<typed_node<T>>(new typed_node<T>(arg));
64  auto &value = new_node->value;
65  new_node->next = std::move(head_);
66  head_ = std::move(new_node);
67  return value;
68  }
69 };
70 } // namespace detail
71 
82 template <typename Context>
84 #if FMT_GCC_VERSION && FMT_GCC_VERSION < 409
85  // Workaround a GCC template argument substitution bug.
87 #endif
88 {
89 private:
90  using char_type = typename Context::char_type;
91 
92  template <typename T> struct need_copy {
93  static constexpr detail::type mapped_type =
94  detail::mapped_type_constant<T, Context>::value;
95 
96  enum {
97  value = !(detail::is_reference_wrapper<T>::value ||
98  std::is_same<T, basic_string_view<char_type>>::value ||
99  std::is_same<T, detail::std_string_view<char_type>>::value ||
100  (mapped_type != detail::type::cstring_type &&
101  mapped_type != detail::type::string_type &&
102  mapped_type != detail::type::custom_type))
103  };
104  };
105 
106  template <typename T>
107  using stored_type = conditional_t<
108  std::is_convertible<T, std::basic_string<char_type>>::value &&
109  !detail::is_reference_wrapper<T>::value,
110  std::basic_string<char_type>, T>;
111 
112  // Storage of basic_format_arg must be contiguous.
113  std::vector<basic_format_arg<Context>> data_;
114  std::vector<detail::named_arg_info<char_type>> named_info_;
115 
116  // Storage of arguments not fitting into basic_format_arg must grow
117  // without relocation because items in data_ refer to it.
118  detail::dynamic_arg_list dynamic_args_;
119 
120  friend class basic_format_args<Context>;
121 
122  unsigned long long get_types() const {
123  return detail::is_unpacked_bit | data_.size() |
124  (named_info_.empty() ?
125  0ULL :
126  static_cast<unsigned long long>(detail::has_named_args_bit));
127  }
128 
129  const basic_format_arg<Context> *data() const {
130  return named_info_.empty() ? data_.data() : data_.data() + 1;
131  }
132 
133  template <typename T> void emplace_arg(const T &arg) {
134  data_.emplace_back(detail::make_arg<Context>(arg));
135  }
136 
137  template <typename T>
138  void emplace_arg(const detail::named_arg<char_type, T> &arg) {
139  if (named_info_.empty()) {
140  constexpr const detail::named_arg_info<char_type> *zero_ptr{nullptr};
141  data_.insert(data_.begin(), {zero_ptr, 0});
142  }
143  data_.emplace_back(detail::make_arg<Context>(detail::unwrap(arg.value)));
144  auto pop_one = [](std::vector<basic_format_arg<Context>> *data) {
145  data->pop_back();
146  };
147  std::unique_ptr<std::vector<basic_format_arg<Context>>, decltype(pop_one)>
148  guard{&data_, pop_one};
149  named_info_.push_back({arg.name, static_cast<int>(data_.size() - 2u)});
150  data_[0].value_.named_args = {named_info_.data(), named_info_.size()};
151  guard.release();
152  }
153 
154 public:
155  constexpr dynamic_format_arg_store() = default;
156 
174  template <typename T> void push_back(const T &arg) {
175  if (detail::const_check(need_copy<T>::value))
176  emplace_arg(dynamic_args_.push<stored_type<T>>(arg));
177  else
178  emplace_arg(detail::unwrap(arg));
179  }
180 
196  template <typename T> void push_back(std::reference_wrapper<T> arg) {
197  static_assert(
198  need_copy<T>::value,
199  "objects of built-in types and string views are always copied");
200  emplace_arg(arg.get());
201  }
202 
208  template <typename T>
209  void push_back(const detail::named_arg<char_type, T> &arg) {
210  const char_type *arg_name =
211  dynamic_args_.push<std::basic_string<char_type>>(arg.name).c_str();
212  if (detail::const_check(need_copy<T>::value)) {
213  emplace_arg(
214  fmt::arg(arg_name, dynamic_args_.push<stored_type<T>>(arg.value)));
215  } else {
216  emplace_arg(fmt::arg(arg_name, arg.value));
217  }
218  }
219 
221  void clear() {
222  data_.clear();
223  named_info_.clear();
224  dynamic_args_ = detail::dynamic_arg_list();
225  }
226 
233  void reserve(size_t new_cap, size_t new_cap_named) {
234  FMT_ASSERT(new_cap >= new_cap_named,
235  "Set of arguments includes set of named arguments");
236  data_.reserve(new_cap);
237  named_info_.reserve(new_cap_named);
238  }
239 };
240 
241 FMT_END_NAMESPACE
242 
243 #endif // FMT_ARGS_H_
Definition: core.h:1975
Definition: core.h:435
Definition: args.h:88
void push_back(const detail::named_arg< char_type, T > &arg)
Definition: args.h:209
void push_back(const T &arg)
Definition: args.h:174
void clear()
Definition: args.h:221
void reserve(size_t new_cap, size_t new_cap_named)
Definition: args.h:233
void push_back(std::reference_wrapper< T > arg)
Definition: args.h:196