Photon 1.0.0
Loading...
Searching...
No Matches
args.h
Go to the documentation of this file.
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#include <functional> // std::reference_wrapper
12#include <memory> // std::unique_ptr
13#include <vector>
14
15#include "core.h"
16
18
19namespace detail
20{
21
22 template <typename T>
23 struct is_reference_wrapper : std::false_type
24 {
25 };
26 template <typename T>
27 struct is_reference_wrapper<std::reference_wrapper<T>> : std::true_type
28 {
29 };
30
31 template <typename T>
32 const T& unwrap(const T& v)
33 {
34 return v;
35 }
36 template <typename T>
37 const T& unwrap(const std::reference_wrapper<T>& v)
38 {
39 return static_cast<const T&>(v);
40 }
41
43 {
44 // Workaround for clang's -Wweak-vtables. Unlike for regular classes, for
45 // templates it doesn't complain about inability to deduce single translation
46 // unit for placing vtable. So storage_node_base is made a fake template.
47 template <typename = void>
48 struct node
49 {
50 virtual ~node() = default;
51 std::unique_ptr<node<>> next;
52 };
53
54 template <typename T>
55 struct typed_node : node<>
56 {
58
59 template <typename Arg>
61 : value(arg)
62 {
63 }
64
65 template <typename Char>
70 };
71
72 std::unique_ptr<node<>> head_;
73
74 public:
75 template <typename T, typename Arg>
76 const T& push(const Arg& arg)
77 {
78 auto new_node = std::unique_ptr<typed_node<T>>(new typed_node<T>(arg));
79 auto& value = new_node->value;
80 new_node->next = std::move(head_);
81 head_ = std::move(new_node);
82 return value;
83 }
84 };
85} // namespace detail
86
97template <typename Context>
100 // Workaround a GCC template argument substitution bug.
102#endif
103{
104private:
105 using char_type = typename Context::char_type;
106
107 template <typename T>
109 {
110 static constexpr detail::type mapped_type =
111 detail::mapped_type_constant<T, Context>::value;
112
113 enum
114 {
116 std::is_same<T, basic_string_view<char_type>>::value ||
117 std::is_same<T, detail::std_string_view<char_type>>::value ||
118 (mapped_type != detail::type::cstring_type &&
119 mapped_type != detail::type::string_type &&
120 mapped_type != detail::type::custom_type))
121 };
122 };
123
124 template <typename T>
126 std::is_convertible<T, std::basic_string<char_type>>::value &&
128 std::basic_string<char_type>,
129 T>;
130
131 // Storage of basic_format_arg must be contiguous.
132 std::vector<basic_format_arg<Context>> data_;
133 std::vector<detail::named_arg_info<char_type>> named_info_;
134
135 // Storage of arguments not fitting into basic_format_arg must grow
136 // without relocation because items in data_ refer to it.
138
139 friend class basic_format_args<Context>;
140
141 unsigned long long get_types() const
142 {
143 return detail::is_unpacked_bit | data_.size() |
144 (named_info_.empty()
145 ? 0ULL
146 : static_cast<unsigned long long>(detail::has_named_args_bit));
147 }
148
150 {
151 return named_info_.empty() ? data_.data() : data_.data() + 1;
152 }
153
154 template <typename T>
155 void emplace_arg(const T& arg)
156 {
157 data_.emplace_back(detail::make_arg<Context>(arg));
158 }
159
160 template <typename T>
161 void emplace_arg(const detail::named_arg<char_type, T>& arg)
162 {
163 if (named_info_.empty())
164 {
165 constexpr const detail::named_arg_info<char_type>* zero_ptr{nullptr};
166 data_.insert(data_.begin(), {zero_ptr, 0});
167 }
168 data_.emplace_back(detail::make_arg<Context>(detail::unwrap(arg.value)));
169 auto pop_one = [](std::vector<basic_format_arg<Context>>* data) {
170 data->pop_back();
171 };
172 std::unique_ptr<std::vector<basic_format_arg<Context>>, decltype(pop_one)>
173 guard{&data_, pop_one};
174 named_info_.push_back({arg.name, static_cast<int>(data_.size() - 2u)});
175 data_[0].value_.named_args = {named_info_.data(), named_info_.size()};
176 guard.release();
177 }
178
179public:
180 constexpr dynamic_format_arg_store() = default;
181
199 template <typename T>
200 void push_back(const T& arg)
201 {
202 if (detail::const_check(need_copy<T>::value))
203 emplace_arg(dynamic_args_.push<stored_type<T>>(arg));
204 else
205 emplace_arg(detail::unwrap(arg));
206 }
207
223 template <typename T>
224 void push_back(std::reference_wrapper<T> arg)
225 {
226 static_assert(
228 "objects of built-in types and string views are always copied");
229 emplace_arg(arg.get());
230 }
231
237 template <typename T>
238 void push_back(const detail::named_arg<char_type, T>& arg)
239 {
240 const char_type* arg_name =
241 dynamic_args_.push<std::basic_string<char_type>>(arg.name).c_str();
242 if (detail::const_check(need_copy<T>::value))
243 {
244 emplace_arg(
245 fmt::arg(arg_name, dynamic_args_.push<stored_type<T>>(arg.value)));
246 }
247 else
248 {
249 emplace_arg(fmt::arg(arg_name, arg.value));
250 }
251 }
252
254 void clear()
255 {
256 data_.clear();
257 named_info_.clear();
258 dynamic_args_ = detail::dynamic_arg_list();
259 }
260
267 void reserve(size_t new_cap, size_t new_cap_named)
268 {
269 FMT_ASSERT(new_cap >= new_cap_named,
270 "Set of arguments includes set of named arguments");
271 data_.reserve(new_cap);
272 named_info_.reserve(new_cap_named);
273 }
274};
275
277
278#endif // FMT_ARGS_H_
Definition core.h:2054
Definition core.h:2524
Definition core.h:483
Definition args.h:43
const T & push(const Arg &arg)
Definition args.h:76
std::unique_ptr< node<> > head_
Definition args.h:72
Definition args.h:103
void emplace_arg(const detail::named_arg< char_type, T > &arg)
Definition args.h:161
void push_back(const detail::named_arg< char_type, T > &arg)
Definition args.h:238
typename Context::char_type char_type
Definition args.h:105
void emplace_arg(const T &arg)
Definition args.h:155
unsigned long long get_types() const
Definition args.h:141
constexpr dynamic_format_arg_store()=default
void push_back(const T &arg)
Definition args.h:200
const basic_format_arg< Context > * data() const
Definition args.h:149
std::vector< basic_format_arg< Context > > data_
Definition args.h:132
void clear()
Definition args.h:254
conditional_t< std::is_convertible< T, std::basic_string< char_type > >::value &&!detail::is_reference_wrapper< T >::value, std::basic_string< char_type >, T > stored_type
Definition args.h:129
void reserve(size_t new_cap, size_t new_cap_named)
Definition args.h:267
void push_back(std::reference_wrapper< T > arg)
Definition args.h:224
std::vector< detail::named_arg_info< char_type > > named_info_
Definition args.h:133
Definition core.h:1598
constexpr FMT_INLINE value()
Definition core.h:1621
#define FMT_ASSERT(condition, message)
Definition core.h:403
auto arg(const Char *name, const T &arg) -> detail::named_arg< Char, T >
Definition core.h:2506
#define FMT_GCC_VERSION
Definition core.h:32
#define FMT_CONSTEXPR
Definition core.h:106
#define FMT_BEGIN_NAMESPACE
Definition core.h:214
typename std::conditional< B, T, F >::type conditional_t
Definition core.h:304
#define FMT_END_NAMESPACE
Definition core.h:219
Definition args.h:20
const T & unwrap(const T &v)
Definition args.h:32
Definition uuid.h:926
Definition format.h:1901
Definition args.h:49
std::unique_ptr< node<> > next
Definition args.h:51
FMT_CONSTEXPR typed_node(const Arg &arg)
Definition args.h:60
FMT_CONSTEXPR typed_node(const basic_string_view< Char > &arg)
Definition args.h:66
Definition args.h:24