]> git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/fmt/include/fmt/core.h
update download target update for octopus release
[ceph.git] / ceph / src / seastar / fmt / include / fmt / core.h
1 // Formatting library for C++ - the core API
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_CORE_H_
9 #define FMT_CORE_H_
10
11 #include <cassert>
12 #include <cstdio> // std::FILE
13 #include <cstring>
14 #include <iterator>
15 #include <string>
16 #include <type_traits>
17
18 // The fmt library version in the form major * 10000 + minor * 100 + patch.
19 #define FMT_VERSION 50202
20
21 #ifdef __has_feature
22 # define FMT_HAS_FEATURE(x) __has_feature(x)
23 #else
24 # define FMT_HAS_FEATURE(x) 0
25 #endif
26
27 #if defined(__has_include) && !defined(__INTELLISENSE__) && \
28 !(defined(__INTEL_COMPILER) && __INTEL_COMPILER < 1600)
29 # define FMT_HAS_INCLUDE(x) __has_include(x)
30 #else
31 # define FMT_HAS_INCLUDE(x) 0
32 #endif
33
34 #ifdef __has_cpp_attribute
35 # define FMT_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
36 #else
37 # define FMT_HAS_CPP_ATTRIBUTE(x) 0
38 #endif
39
40 #if defined(__GNUC__) && !defined(__clang__)
41 # define FMT_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
42 #else
43 # define FMT_GCC_VERSION 0
44 #endif
45
46 #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
47 # define FMT_HAS_GXX_CXX11 FMT_GCC_VERSION
48 #else
49 # define FMT_HAS_GXX_CXX11 0
50 #endif
51
52 #ifdef _MSC_VER
53 # define FMT_MSC_VER _MSC_VER
54 #else
55 # define FMT_MSC_VER 0
56 #endif
57
58 // Check if relaxed C++14 constexpr is supported.
59 // GCC doesn't allow throw in constexpr until version 6 (bug 67371).
60 #ifndef FMT_USE_CONSTEXPR
61 # define FMT_USE_CONSTEXPR \
62 (FMT_HAS_FEATURE(cxx_relaxed_constexpr) || FMT_MSC_VER >= 1910 || \
63 (FMT_GCC_VERSION >= 600 && __cplusplus >= 201402L))
64 #endif
65 #if FMT_USE_CONSTEXPR
66 # define FMT_CONSTEXPR constexpr
67 # define FMT_CONSTEXPR_DECL constexpr
68 #else
69 # define FMT_CONSTEXPR inline
70 # define FMT_CONSTEXPR_DECL
71 #endif
72
73 #ifndef FMT_USE_CONSTEXPR11
74 # define FMT_USE_CONSTEXPR11 \
75 (FMT_USE_CONSTEXPR || FMT_GCC_VERSION >= 406 || FMT_MSC_VER >= 1900)
76 #endif
77 #if FMT_USE_CONSTEXPR11
78 # define FMT_CONSTEXPR11 constexpr
79 #else
80 # define FMT_CONSTEXPR11
81 #endif
82
83 #ifndef FMT_OVERRIDE
84 # if FMT_HAS_FEATURE(cxx_override) || \
85 (FMT_GCC_VERSION >= 408 && FMT_HAS_GXX_CXX11) || FMT_MSC_VER >= 1900
86 # define FMT_OVERRIDE override
87 # else
88 # define FMT_OVERRIDE
89 # endif
90 #endif
91
92 #if FMT_HAS_FEATURE(cxx_explicit_conversions) || \
93 FMT_GCC_VERSION >= 405 || FMT_MSC_VER >= 1800
94 # define FMT_USE_EXPLICIT 1
95 # define FMT_EXPLICIT explicit
96 #else
97 # define FMT_USE_EXPLICIT 0
98 # define FMT_EXPLICIT
99 #endif
100
101 #ifndef FMT_NULL
102 # if FMT_HAS_FEATURE(cxx_nullptr) || \
103 (FMT_GCC_VERSION >= 408 && FMT_HAS_GXX_CXX11) || FMT_MSC_VER >= 1600
104 # define FMT_NULL nullptr
105 # define FMT_USE_NULLPTR 1
106 # else
107 # define FMT_NULL NULL
108 # endif
109 #endif
110 #ifndef FMT_USE_NULLPTR
111 # define FMT_USE_NULLPTR 0
112 #endif
113
114 // Check if exceptions are disabled.
115 #ifndef FMT_EXCEPTIONS
116 # if (defined(__GNUC__) && !defined(__EXCEPTIONS)) || \
117 FMT_MSC_VER && !_HAS_EXCEPTIONS
118 # define FMT_EXCEPTIONS 0
119 # else
120 # define FMT_EXCEPTIONS 1
121 # endif
122 #endif
123
124 // Define FMT_USE_NOEXCEPT to make fmt use noexcept (C++11 feature).
125 #ifndef FMT_USE_NOEXCEPT
126 # define FMT_USE_NOEXCEPT 0
127 #endif
128
129 #if FMT_USE_NOEXCEPT || FMT_HAS_FEATURE(cxx_noexcept) || \
130 (FMT_GCC_VERSION >= 408 && FMT_HAS_GXX_CXX11) || FMT_MSC_VER >= 1900
131 # define FMT_DETECTED_NOEXCEPT noexcept
132 # define FMT_HAS_CXX11_NOEXCEPT 1
133 #else
134 # define FMT_DETECTED_NOEXCEPT throw()
135 # define FMT_HAS_CXX11_NOEXCEPT 0
136 #endif
137
138 #ifndef FMT_NOEXCEPT
139 # if FMT_EXCEPTIONS || FMT_HAS_CXX11_NOEXCEPT
140 # define FMT_NOEXCEPT FMT_DETECTED_NOEXCEPT
141 # else
142 # define FMT_NOEXCEPT
143 # endif
144 #endif
145
146 #ifndef FMT_BEGIN_NAMESPACE
147 # if FMT_HAS_FEATURE(cxx_inline_namespaces) || FMT_GCC_VERSION >= 404 || \
148 FMT_MSC_VER >= 1900
149 # define FMT_INLINE_NAMESPACE inline namespace
150 # define FMT_END_NAMESPACE }}
151 # else
152 # define FMT_INLINE_NAMESPACE namespace
153 # define FMT_END_NAMESPACE } using namespace v5; }
154 # endif
155 # define FMT_BEGIN_NAMESPACE namespace fmt { FMT_INLINE_NAMESPACE v5 {
156 #endif
157
158 #if !defined(FMT_HEADER_ONLY) && defined(_WIN32)
159 # ifdef FMT_EXPORT
160 # define FMT_API __declspec(dllexport)
161 # elif defined(FMT_SHARED)
162 # define FMT_API __declspec(dllimport)
163 # endif
164 #endif
165 #ifndef FMT_API
166 # define FMT_API
167 #endif
168
169 #ifndef FMT_ASSERT
170 # define FMT_ASSERT(condition, message) assert((condition) && message)
171 #endif
172
173 // libc++ supports string_view in pre-c++17.
174 #if (FMT_HAS_INCLUDE(<string_view>) && \
175 (__cplusplus > 201402L || defined(_LIBCPP_VERSION))) || \
176 (defined(_MSVC_LANG) && _MSVC_LANG > 201402L && _MSC_VER >= 1910)
177 # include <string_view>
178 # define FMT_STRING_VIEW std::basic_string_view
179 #elif FMT_HAS_INCLUDE(<experimental/string_view>) && __cplusplus >= 201402L
180 # include <experimental/string_view>
181 # define FMT_STRING_VIEW std::experimental::basic_string_view
182 #endif
183
184 // std::result_of is defined in <functional> in gcc 4.4.
185 #if FMT_GCC_VERSION && FMT_GCC_VERSION <= 404
186 # include <functional>
187 #endif
188
189 FMT_BEGIN_NAMESPACE
190 namespace internal {
191
192 // An implementation of declval for pre-C++11 compilers such as gcc 4.
193 template <typename T>
194 typename std::add_rvalue_reference<T>::type declval() FMT_NOEXCEPT;
195
196 template <typename>
197 struct result_of;
198
199 template <typename F, typename... Args>
200 struct result_of<F(Args...)> {
201 // A workaround for gcc 4.4 that doesn't allow F to be a reference.
202 typedef typename std::result_of<
203 typename std::remove_reference<F>::type(Args...)>::type type;
204 };
205
206 // Casts nonnegative integer to unsigned.
207 template <typename Int>
208 FMT_CONSTEXPR typename std::make_unsigned<Int>::type to_unsigned(Int value) {
209 FMT_ASSERT(value >= 0, "negative value");
210 return static_cast<typename std::make_unsigned<Int>::type>(value);
211 }
212
213 /** A contiguous memory buffer with an optional growing ability. */
214 template <typename T>
215 class basic_buffer {
216 private:
217 basic_buffer(const basic_buffer &) = delete;
218 void operator=(const basic_buffer &) = delete;
219
220 T *ptr_;
221 std::size_t size_;
222 std::size_t capacity_;
223
224 protected:
225 // Don't initialize ptr_ since it is not accessed to save a few cycles.
226 basic_buffer(std::size_t sz) FMT_NOEXCEPT: size_(sz), capacity_(sz) {}
227
228 basic_buffer(T *p = FMT_NULL, std::size_t sz = 0, std::size_t cap = 0)
229 FMT_NOEXCEPT: ptr_(p), size_(sz), capacity_(cap) {}
230
231 /** Sets the buffer data and capacity. */
232 void set(T *buf_data, std::size_t buf_capacity) FMT_NOEXCEPT {
233 ptr_ = buf_data;
234 capacity_ = buf_capacity;
235 }
236
237 /** Increases the buffer capacity to hold at least *capacity* elements. */
238 virtual void grow(std::size_t capacity) = 0;
239
240 public:
241 typedef T value_type;
242 typedef const T &const_reference;
243
244 virtual ~basic_buffer() {}
245
246 T *begin() FMT_NOEXCEPT { return ptr_; }
247 T *end() FMT_NOEXCEPT { return ptr_ + size_; }
248
249 /** Returns the size of this buffer. */
250 std::size_t size() const FMT_NOEXCEPT { return size_; }
251
252 /** Returns the capacity of this buffer. */
253 std::size_t capacity() const FMT_NOEXCEPT { return capacity_; }
254
255 /** Returns a pointer to the buffer data. */
256 T *data() FMT_NOEXCEPT { return ptr_; }
257
258 /** Returns a pointer to the buffer data. */
259 const T *data() const FMT_NOEXCEPT { return ptr_; }
260
261 /**
262 Resizes the buffer. If T is a POD type new elements may not be initialized.
263 */
264 void resize(std::size_t new_size) {
265 reserve(new_size);
266 size_ = new_size;
267 }
268
269 /** Clears this buffer. */
270 void clear() { size_ = 0; }
271
272 /** Reserves space to store at least *capacity* elements. */
273 void reserve(std::size_t new_capacity) {
274 if (new_capacity > capacity_)
275 grow(new_capacity);
276 }
277
278 void push_back(const T &value) {
279 reserve(size_ + 1);
280 ptr_[size_++] = value;
281 }
282
283 /** Appends data to the end of the buffer. */
284 template <typename U>
285 void append(const U *begin, const U *end);
286
287 T &operator[](std::size_t index) { return ptr_[index]; }
288 const T &operator[](std::size_t index) const { return ptr_[index]; }
289 };
290
291 typedef basic_buffer<char> buffer;
292 typedef basic_buffer<wchar_t> wbuffer;
293
294 // A container-backed buffer.
295 template <typename Container>
296 class container_buffer : public basic_buffer<typename Container::value_type> {
297 private:
298 Container &container_;
299
300 protected:
301 void grow(std::size_t capacity) FMT_OVERRIDE {
302 container_.resize(capacity);
303 this->set(&container_[0], capacity);
304 }
305
306 public:
307 explicit container_buffer(Container &c)
308 : basic_buffer<typename Container::value_type>(c.size()), container_(c) {}
309 };
310
311 // Extracts a reference to the container from back_insert_iterator.
312 template <typename Container>
313 inline Container &get_container(std::back_insert_iterator<Container> it) {
314 typedef std::back_insert_iterator<Container> bi_iterator;
315 struct accessor: bi_iterator {
316 accessor(bi_iterator iter) : bi_iterator(iter) {}
317 using bi_iterator::container;
318 };
319 return *accessor(it).container;
320 }
321
322 struct error_handler {
323 FMT_CONSTEXPR error_handler() {}
324 FMT_CONSTEXPR error_handler(const error_handler &) {}
325
326 // This function is intentionally not constexpr to give a compile-time error.
327 FMT_API void on_error(const char *message);
328 };
329
330 template <typename T>
331 struct no_formatter_error : std::false_type {};
332 } // namespace internal
333
334 #if FMT_GCC_VERSION && FMT_GCC_VERSION < 405
335 template <typename... T>
336 struct is_constructible: std::false_type {};
337 #else
338 template <typename... T>
339 struct is_constructible : std::is_constructible<T...> {};
340 #endif
341
342 /**
343 An implementation of ``std::basic_string_view`` for pre-C++17. It provides a
344 subset of the API. ``fmt::basic_string_view`` is used for format strings even
345 if ``std::string_view`` is available to prevent issues when a library is
346 compiled with a different ``-std`` option than the client code (which is not
347 recommended).
348 */
349 template <typename Char>
350 class basic_string_view {
351 private:
352 const Char *data_;
353 size_t size_;
354
355 public:
356 typedef Char char_type;
357 typedef const Char *iterator;
358
359 FMT_CONSTEXPR basic_string_view() FMT_NOEXCEPT : data_(FMT_NULL), size_(0) {}
360
361 /** Constructs a string reference object from a C string and a size. */
362 FMT_CONSTEXPR basic_string_view(const Char *s, size_t count) FMT_NOEXCEPT
363 : data_(s), size_(count) {}
364
365 /**
366 \rst
367 Constructs a string reference object from a C string computing
368 the size with ``std::char_traits<Char>::length``.
369 \endrst
370 */
371 basic_string_view(const Char *s)
372 : data_(s), size_(std::char_traits<Char>::length(s)) {}
373
374 /** Constructs a string reference from a ``std::basic_string`` object. */
375 template <typename Alloc>
376 FMT_CONSTEXPR basic_string_view(
377 const std::basic_string<Char, Alloc> &s) FMT_NOEXCEPT
378 : data_(s.data()), size_(s.size()) {}
379
380 #ifdef FMT_STRING_VIEW
381 FMT_CONSTEXPR basic_string_view(FMT_STRING_VIEW<Char> s) FMT_NOEXCEPT
382 : data_(s.data()), size_(s.size()) {}
383 #endif
384
385 /** Returns a pointer to the string data. */
386 FMT_CONSTEXPR const Char *data() const { return data_; }
387
388 /** Returns the string size. */
389 FMT_CONSTEXPR size_t size() const { return size_; }
390
391 FMT_CONSTEXPR iterator begin() const { return data_; }
392 FMT_CONSTEXPR iterator end() const { return data_ + size_; }
393
394 FMT_CONSTEXPR void remove_prefix(size_t n) {
395 data_ += n;
396 size_ -= n;
397 }
398
399 // Lexicographically compare this string reference to other.
400 int compare(basic_string_view other) const {
401 size_t str_size = size_ < other.size_ ? size_ : other.size_;
402 int result = std::char_traits<Char>::compare(data_, other.data_, str_size);
403 if (result == 0)
404 result = size_ == other.size_ ? 0 : (size_ < other.size_ ? -1 : 1);
405 return result;
406 }
407
408 friend bool operator==(basic_string_view lhs, basic_string_view rhs) {
409 return lhs.compare(rhs) == 0;
410 }
411 friend bool operator!=(basic_string_view lhs, basic_string_view rhs) {
412 return lhs.compare(rhs) != 0;
413 }
414 friend bool operator<(basic_string_view lhs, basic_string_view rhs) {
415 return lhs.compare(rhs) < 0;
416 }
417 friend bool operator<=(basic_string_view lhs, basic_string_view rhs) {
418 return lhs.compare(rhs) <= 0;
419 }
420 friend bool operator>(basic_string_view lhs, basic_string_view rhs) {
421 return lhs.compare(rhs) > 0;
422 }
423 friend bool operator>=(basic_string_view lhs, basic_string_view rhs) {
424 return lhs.compare(rhs) >= 0;
425 }
426 };
427
428 typedef basic_string_view<char> string_view;
429 typedef basic_string_view<wchar_t> wstring_view;
430
431 /**
432 \rst
433 The function ``to_string_view`` adapts non-intrusively any kind of string or
434 string-like type if the user provides a (possibly templated) overload of
435 ``to_string_view`` which takes an instance of the string class
436 ``StringType<Char>`` and returns a ``fmt::basic_string_view<Char>``.
437 The conversion function must live in the very same namespace as
438 ``StringType<Char>`` to be picked up by ADL. Non-templated string types
439 like f.e. QString must return a ``basic_string_view`` with a fixed matching
440 char type.
441
442 **Example**::
443
444 namespace my_ns {
445 inline string_view to_string_view(const my_string &s) {
446 return { s.data(), s.length() };
447 }
448 }
449
450 std::string message = fmt::format(my_string("The answer is {}"), 42);
451 \endrst
452 */
453 template <typename Char>
454 inline basic_string_view<Char>
455 to_string_view(basic_string_view<Char> s) { return s; }
456
457 template <typename Char>
458 inline basic_string_view<Char>
459 to_string_view(const std::basic_string<Char> &s) { return s; }
460
461 template <typename Char>
462 inline basic_string_view<Char> to_string_view(const Char *s) { return s; }
463
464 #ifdef FMT_STRING_VIEW
465 template <typename Char>
466 inline basic_string_view<Char>
467 to_string_view(FMT_STRING_VIEW<Char> s) { return s; }
468 #endif
469
470 // A base class for compile-time strings. It is defined in the fmt namespace to
471 // make formatting functions visible via ADL, e.g. format(fmt("{}"), 42).
472 struct compile_string {};
473
474 template <typename S>
475 struct is_compile_string : std::is_base_of<compile_string, S> {};
476
477 template <
478 typename S,
479 typename Enable = typename std::enable_if<is_compile_string<S>::value>::type>
480 FMT_CONSTEXPR basic_string_view<typename S::char_type>
481 to_string_view(const S &s) { return s; }
482
483 template <typename Context>
484 class basic_format_arg;
485
486 template <typename Context>
487 class basic_format_args;
488
489 // A formatter for objects of type T.
490 template <typename T, typename Char = char, typename Enable = void>
491 struct formatter {
492 static_assert(internal::no_formatter_error<T>::value,
493 "don't know how to format the type, include fmt/ostream.h if it provides "
494 "an operator<< that should be used");
495
496 // The following functions are not defined intentionally.
497 template <typename ParseContext>
498 typename ParseContext::iterator parse(ParseContext &);
499 template <typename FormatContext>
500 auto format(const T &val, FormatContext &ctx) -> decltype(ctx.out());
501 };
502
503 template <typename T, typename Char, typename Enable = void>
504 struct convert_to_int: std::integral_constant<
505 bool, !std::is_arithmetic<T>::value && std::is_convertible<T, int>::value> {};
506
507 namespace internal {
508
509 struct dummy_string_view { typedef void char_type; };
510 dummy_string_view to_string_view(...);
511 using fmt::v5::to_string_view;
512
513 // Specifies whether S is a string type convertible to fmt::basic_string_view.
514 template <typename S>
515 struct is_string : std::integral_constant<bool, !std::is_same<
516 dummy_string_view, decltype(to_string_view(declval<S>()))>::value> {};
517
518 template <typename S>
519 struct char_t {
520 typedef decltype(to_string_view(declval<S>())) result;
521 typedef typename result::char_type type;
522 };
523
524 template <typename Char>
525 struct named_arg_base;
526
527 template <typename T, typename Char>
528 struct named_arg;
529
530 enum type {
531 none_type, named_arg_type,
532 // Integer types should go first,
533 int_type, uint_type, long_long_type, ulong_long_type, bool_type, char_type,
534 last_integer_type = char_type,
535 // followed by floating-point types.
536 double_type, long_double_type, last_numeric_type = long_double_type,
537 cstring_type, string_type, pointer_type, custom_type
538 };
539
540 FMT_CONSTEXPR bool is_integral(type t) {
541 FMT_ASSERT(t != internal::named_arg_type, "invalid argument type");
542 return t > internal::none_type && t <= internal::last_integer_type;
543 }
544
545 FMT_CONSTEXPR bool is_arithmetic(type t) {
546 FMT_ASSERT(t != internal::named_arg_type, "invalid argument type");
547 return t > internal::none_type && t <= internal::last_numeric_type;
548 }
549
550 template <typename Char>
551 struct string_value {
552 const Char *value;
553 std::size_t size;
554 };
555
556 template <typename Context>
557 struct custom_value {
558 const void *value;
559 void (*format)(const void *arg, Context &ctx);
560 };
561
562 // A formatting argument value.
563 template <typename Context>
564 class value {
565 public:
566 typedef typename Context::char_type char_type;
567
568 union {
569 int int_value;
570 unsigned uint_value;
571 long long long_long_value;
572 unsigned long long ulong_long_value;
573 double double_value;
574 long double long_double_value;
575 const void *pointer;
576 string_value<char_type> string;
577 string_value<signed char> sstring;
578 string_value<unsigned char> ustring;
579 custom_value<Context> custom;
580 };
581
582 FMT_CONSTEXPR value(int val = 0) : int_value(val) {}
583 value(unsigned val) { uint_value = val; }
584 value(long long val) { long_long_value = val; }
585 value(unsigned long long val) { ulong_long_value = val; }
586 value(double val) { double_value = val; }
587 value(long double val) { long_double_value = val; }
588 value(const char_type *val) { string.value = val; }
589 value(const signed char *val) {
590 static_assert(std::is_same<char, char_type>::value,
591 "incompatible string types");
592 sstring.value = val;
593 }
594 value(const unsigned char *val) {
595 static_assert(std::is_same<char, char_type>::value,
596 "incompatible string types");
597 ustring.value = val;
598 }
599 value(basic_string_view<char_type> val) {
600 string.value = val.data();
601 string.size = val.size();
602 }
603 value(const void *val) { pointer = val; }
604
605 template <typename T>
606 explicit value(const T &val) {
607 custom.value = &val;
608 custom.format = &format_custom_arg<T>;
609 }
610
611 const named_arg_base<char_type> &as_named_arg() {
612 return *static_cast<const named_arg_base<char_type>*>(pointer);
613 }
614
615 private:
616 // Formats an argument of a custom type, such as a user-defined class.
617 template <typename T>
618 static void format_custom_arg(const void *arg, Context &ctx) {
619 // Get the formatter type through the context to allow different contexts
620 // have different extension points, e.g. `formatter<T>` for `format` and
621 // `printf_formatter<T>` for `printf`.
622 typename Context::template formatter_type<T>::type f;
623 auto &&parse_ctx = ctx.parse_context();
624 parse_ctx.advance_to(f.parse(parse_ctx));
625 ctx.advance_to(f.format(*static_cast<const T*>(arg), ctx));
626 }
627 };
628
629 // Value initializer used to delay conversion to value and reduce memory churn.
630 template <typename Context, typename T, type TYPE>
631 struct init {
632 T val;
633 static const type type_tag = TYPE;
634
635 FMT_CONSTEXPR init(const T &v) : val(v) {}
636 FMT_CONSTEXPR operator value<Context>() const { return value<Context>(val); }
637 };
638
639 template <typename Context, typename T>
640 FMT_CONSTEXPR basic_format_arg<Context> make_arg(const T &value);
641
642 #define FMT_MAKE_VALUE(TAG, ArgType, ValueType) \
643 template <typename C> \
644 FMT_CONSTEXPR init<C, ValueType, TAG> make_value(ArgType val) { \
645 return static_cast<ValueType>(val); \
646 }
647
648 #define FMT_MAKE_VALUE_SAME(TAG, Type) \
649 template <typename C> \
650 FMT_CONSTEXPR init<C, Type, TAG> make_value(Type val) { return val; }
651
652 FMT_MAKE_VALUE(bool_type, bool, int)
653 FMT_MAKE_VALUE(int_type, short, int)
654 FMT_MAKE_VALUE(uint_type, unsigned short, unsigned)
655 FMT_MAKE_VALUE_SAME(int_type, int)
656 FMT_MAKE_VALUE_SAME(uint_type, unsigned)
657
658 // To minimize the number of types we need to deal with, long is translated
659 // either to int or to long long depending on its size.
660 typedef std::conditional<sizeof(long) == sizeof(int), int, long long>::type
661 long_type;
662 FMT_MAKE_VALUE(
663 (sizeof(long) == sizeof(int) ? int_type : long_long_type), long, long_type)
664 typedef std::conditional<sizeof(unsigned long) == sizeof(unsigned),
665 unsigned, unsigned long long>::type ulong_type;
666 FMT_MAKE_VALUE(
667 (sizeof(unsigned long) == sizeof(unsigned) ? uint_type : ulong_long_type),
668 unsigned long, ulong_type)
669
670 FMT_MAKE_VALUE_SAME(long_long_type, long long)
671 FMT_MAKE_VALUE_SAME(ulong_long_type, unsigned long long)
672 FMT_MAKE_VALUE(int_type, signed char, int)
673 FMT_MAKE_VALUE(uint_type, unsigned char, unsigned)
674
675 // This doesn't use FMT_MAKE_VALUE because of ambiguity in gcc 4.4.
676 template <typename C, typename Char>
677 FMT_CONSTEXPR typename std::enable_if<
678 std::is_same<typename C::char_type, Char>::value,
679 init<C, int, char_type>>::type make_value(Char val) { return val; }
680
681 template <typename C>
682 FMT_CONSTEXPR typename std::enable_if<
683 !std::is_same<typename C::char_type, char>::value,
684 init<C, int, char_type>>::type make_value(char val) { return val; }
685
686 FMT_MAKE_VALUE(double_type, float, double)
687 FMT_MAKE_VALUE_SAME(double_type, double)
688 FMT_MAKE_VALUE_SAME(long_double_type, long double)
689
690 // Formatting of wide strings into a narrow buffer and multibyte strings
691 // into a wide buffer is disallowed (https://github.com/fmtlib/fmt/pull/606).
692 FMT_MAKE_VALUE(cstring_type, typename C::char_type*,
693 const typename C::char_type*)
694 FMT_MAKE_VALUE(cstring_type, const typename C::char_type*,
695 const typename C::char_type*)
696
697 FMT_MAKE_VALUE(cstring_type, signed char*, const signed char*)
698 FMT_MAKE_VALUE_SAME(cstring_type, const signed char*)
699 FMT_MAKE_VALUE(cstring_type, unsigned char*, const unsigned char*)
700 FMT_MAKE_VALUE_SAME(cstring_type, const unsigned char*)
701 FMT_MAKE_VALUE_SAME(string_type, basic_string_view<typename C::char_type>)
702 FMT_MAKE_VALUE(string_type,
703 typename basic_string_view<typename C::char_type>::type,
704 basic_string_view<typename C::char_type>)
705 FMT_MAKE_VALUE(string_type, const std::basic_string<typename C::char_type>&,
706 basic_string_view<typename C::char_type>)
707 FMT_MAKE_VALUE(pointer_type, void*, const void*)
708 FMT_MAKE_VALUE_SAME(pointer_type, const void*)
709
710 #if FMT_USE_NULLPTR
711 FMT_MAKE_VALUE(pointer_type, std::nullptr_t, const void*)
712 #endif
713
714 // Formatting of arbitrary pointers is disallowed. If you want to output a
715 // pointer cast it to "void *" or "const void *". In particular, this forbids
716 // formatting of "[const] volatile char *" which is printed as bool by
717 // iostreams.
718 template <typename C, typename T>
719 typename std::enable_if<!std::is_same<T, typename C::char_type>::value>::type
720 make_value(const T *) {
721 static_assert(!sizeof(T), "formatting of non-void pointers is disallowed");
722 }
723
724 template <typename C, typename T>
725 inline typename std::enable_if<
726 std::is_enum<T>::value && convert_to_int<T, typename C::char_type>::value,
727 init<C, int, int_type>>::type
728 make_value(const T &val) { return static_cast<int>(val); }
729
730 template <typename C, typename T, typename Char = typename C::char_type>
731 inline typename std::enable_if<
732 is_constructible<basic_string_view<Char>, T>::value &&
733 !internal::is_string<T>::value,
734 init<C, basic_string_view<Char>, string_type>>::type
735 make_value(const T &val) { return basic_string_view<Char>(val); }
736
737 template <typename C, typename T, typename Char = typename C::char_type>
738 inline typename std::enable_if<
739 !convert_to_int<T, Char>::value && !std::is_same<T, Char>::value &&
740 !std::is_convertible<T, basic_string_view<Char>>::value &&
741 !is_constructible<basic_string_view<Char>, T>::value &&
742 !internal::is_string<T>::value,
743 // Implicit conversion to std::string is not handled here because it's
744 // unsafe: https://github.com/fmtlib/fmt/issues/729
745 init<C, const T &, custom_type>>::type
746 make_value(const T &val) { return val; }
747
748 template <typename C, typename T>
749 init<C, const void*, named_arg_type>
750 make_value(const named_arg<T, typename C::char_type> &val) {
751 basic_format_arg<C> arg = make_arg<C>(val.value);
752 std::memcpy(val.data, &arg, sizeof(arg));
753 return static_cast<const void*>(&val);
754 }
755
756 template <typename C, typename S>
757 FMT_CONSTEXPR11 typename std::enable_if<
758 internal::is_string<S>::value,
759 init<C, basic_string_view<typename C::char_type>, string_type>>::type
760 make_value(const S &val) {
761 // Handle adapted strings.
762 static_assert(std::is_same<
763 typename C::char_type, typename internal::char_t<S>::type>::value,
764 "mismatch between char-types of context and argument");
765 return to_string_view(val);
766 }
767
768 // Maximum number of arguments with packed types.
769 enum { max_packed_args = 15 };
770 enum : unsigned long long { is_unpacked_bit = 1ull << 63 };
771
772 template <typename Context>
773 class arg_map;
774 } // namespace internal
775
776 // A formatting argument. It is a trivially copyable/constructible type to
777 // allow storage in basic_memory_buffer.
778 template <typename Context>
779 class basic_format_arg {
780 private:
781 internal::value<Context> value_;
782 internal::type type_;
783
784 template <typename ContextType, typename T>
785 friend FMT_CONSTEXPR basic_format_arg<ContextType>
786 internal::make_arg(const T &value);
787
788 template <typename Visitor, typename Ctx>
789 friend FMT_CONSTEXPR typename internal::result_of<Visitor(int)>::type
790 visit_format_arg(Visitor &&vis, const basic_format_arg<Ctx> &arg);
791
792 friend class basic_format_args<Context>;
793 friend class internal::arg_map<Context>;
794
795 typedef typename Context::char_type char_type;
796
797 public:
798 class handle {
799 public:
800 explicit handle(internal::custom_value<Context> custom): custom_(custom) {}
801
802 void format(Context &ctx) const { custom_.format(custom_.value, ctx); }
803
804 private:
805 internal::custom_value<Context> custom_;
806 };
807
808 FMT_CONSTEXPR basic_format_arg() : type_(internal::none_type) {}
809
810 FMT_EXPLICIT operator bool() const FMT_NOEXCEPT {
811 return type_ != internal::none_type;
812 }
813
814 internal::type type() const { return type_; }
815
816 bool is_integral() const { return internal::is_integral(type_); }
817 bool is_arithmetic() const { return internal::is_arithmetic(type_); }
818 };
819
820 struct monostate {};
821
822 /**
823 \rst
824 Visits an argument dispatching to the appropriate visit method based on
825 the argument type. For example, if the argument type is ``double`` then
826 ``vis(value)`` will be called with the value of type ``double``.
827 \endrst
828 */
829 template <typename Visitor, typename Context>
830 FMT_CONSTEXPR typename internal::result_of<Visitor(int)>::type
831 visit_format_arg(Visitor &&vis, const basic_format_arg<Context> &arg) {
832 typedef typename Context::char_type char_type;
833 switch (arg.type_) {
834 case internal::none_type:
835 break;
836 case internal::named_arg_type:
837 FMT_ASSERT(false, "invalid argument type");
838 break;
839 case internal::int_type:
840 return vis(arg.value_.int_value);
841 case internal::uint_type:
842 return vis(arg.value_.uint_value);
843 case internal::long_long_type:
844 return vis(arg.value_.long_long_value);
845 case internal::ulong_long_type:
846 return vis(arg.value_.ulong_long_value);
847 case internal::bool_type:
848 return vis(arg.value_.int_value != 0);
849 case internal::char_type:
850 return vis(static_cast<char_type>(arg.value_.int_value));
851 case internal::double_type:
852 return vis(arg.value_.double_value);
853 case internal::long_double_type:
854 return vis(arg.value_.long_double_value);
855 case internal::cstring_type:
856 return vis(arg.value_.string.value);
857 case internal::string_type:
858 return vis(basic_string_view<char_type>(
859 arg.value_.string.value, arg.value_.string.size));
860 case internal::pointer_type:
861 return vis(arg.value_.pointer);
862 case internal::custom_type:
863 return vis(typename basic_format_arg<Context>::handle(arg.value_.custom));
864 }
865 return vis(monostate());
866 }
867
868 template <typename Visitor, typename Context>
869 FMT_CONSTEXPR typename internal::result_of<Visitor(int)>::type
870 visit(Visitor &&vis, const basic_format_arg<Context> &arg) {
871 return visit_format_arg(std::forward<Visitor>(vis), arg);
872 }
873
874 // Parsing context consisting of a format string range being parsed and an
875 // argument counter for automatic indexing.
876 template <typename Char, typename ErrorHandler = internal::error_handler>
877 class basic_parse_context : private ErrorHandler {
878 private:
879 basic_string_view<Char> format_str_;
880 int next_arg_id_;
881
882 public:
883 typedef Char char_type;
884 typedef typename basic_string_view<Char>::iterator iterator;
885
886 explicit FMT_CONSTEXPR basic_parse_context(
887 basic_string_view<Char> format_str, ErrorHandler eh = ErrorHandler())
888 : ErrorHandler(eh), format_str_(format_str), next_arg_id_(0) {}
889
890 // Returns an iterator to the beginning of the format string range being
891 // parsed.
892 FMT_CONSTEXPR iterator begin() const FMT_NOEXCEPT {
893 return format_str_.begin();
894 }
895
896 // Returns an iterator past the end of the format string range being parsed.
897 FMT_CONSTEXPR iterator end() const FMT_NOEXCEPT { return format_str_.end(); }
898
899 // Advances the begin iterator to ``it``.
900 FMT_CONSTEXPR void advance_to(iterator it) {
901 format_str_.remove_prefix(internal::to_unsigned(it - begin()));
902 }
903
904 // Returns the next argument index.
905 FMT_CONSTEXPR unsigned next_arg_id();
906
907 FMT_CONSTEXPR bool check_arg_id(unsigned) {
908 if (next_arg_id_ > 0) {
909 on_error("cannot switch from automatic to manual argument indexing");
910 return false;
911 }
912 next_arg_id_ = -1;
913 return true;
914 }
915 void check_arg_id(basic_string_view<Char>) {}
916
917 FMT_CONSTEXPR void on_error(const char *message) {
918 ErrorHandler::on_error(message);
919 }
920
921 FMT_CONSTEXPR ErrorHandler error_handler() const { return *this; }
922 };
923
924 typedef basic_parse_context<char> format_parse_context;
925 typedef basic_parse_context<wchar_t> wformat_parse_context;
926
927 // DEPRECATED!
928 typedef basic_parse_context<char> parse_context;
929 typedef basic_parse_context<wchar_t> wparse_context;
930
931 namespace internal {
932 // A map from argument names to their values for named arguments.
933 template <typename Context>
934 class arg_map {
935 private:
936 arg_map(const arg_map &) = delete;
937 void operator=(const arg_map &) = delete;
938
939 typedef typename Context::char_type char_type;
940
941 struct entry {
942 basic_string_view<char_type> name;
943 basic_format_arg<Context> arg;
944 };
945
946 entry *map_;
947 unsigned size_;
948
949 void push_back(value<Context> val) {
950 const internal::named_arg_base<char_type> &named = val.as_named_arg();
951 map_[size_] = entry{named.name, named.template deserialize<Context>()};
952 ++size_;
953 }
954
955 public:
956 arg_map() : map_(FMT_NULL), size_(0) {}
957 void init(const basic_format_args<Context> &args);
958 ~arg_map() { delete [] map_; }
959
960 basic_format_arg<Context> find(basic_string_view<char_type> name) const {
961 // The list is unsorted, so just return the first matching name.
962 for (entry *it = map_, *end = map_ + size_; it != end; ++it) {
963 if (it->name == name)
964 return it->arg;
965 }
966 return {};
967 }
968 };
969
970 // A type-erased reference to an std::locale to avoid heavy <locale> include.
971 class locale_ref {
972 private:
973 const void *locale_; // A type-erased pointer to std::locale.
974 friend class locale;
975
976 public:
977 locale_ref() : locale_(FMT_NULL) {}
978
979 template <typename Locale>
980 explicit locale_ref(const Locale &loc);
981
982 template <typename Locale>
983 Locale get() const;
984 };
985
986 template <typename OutputIt, typename Context, typename Char>
987 class context_base {
988 public:
989 typedef OutputIt iterator;
990
991 private:
992 basic_parse_context<Char> parse_context_;
993 iterator out_;
994 basic_format_args<Context> args_;
995 locale_ref loc_;
996
997 protected:
998 typedef Char char_type;
999 typedef basic_format_arg<Context> format_arg;
1000
1001 context_base(OutputIt out, basic_string_view<char_type> format_str,
1002 basic_format_args<Context> ctx_args,
1003 locale_ref loc = locale_ref())
1004 : parse_context_(format_str), out_(out), args_(ctx_args), loc_(loc) {}
1005
1006 // Returns the argument with specified index.
1007 format_arg do_get_arg(unsigned arg_id) {
1008 format_arg arg = args_.get(arg_id);
1009 if (!arg)
1010 parse_context_.on_error("argument index out of range");
1011 return arg;
1012 }
1013
1014 // Checks if manual indexing is used and returns the argument with
1015 // specified index.
1016 format_arg get_arg(unsigned arg_id) {
1017 return this->parse_context().check_arg_id(arg_id) ?
1018 this->do_get_arg(arg_id) : format_arg();
1019 }
1020
1021 public:
1022 basic_parse_context<char_type> &parse_context() { return parse_context_; }
1023 basic_format_args<Context> args() const { return args_; } // DEPRECATED!
1024 basic_format_arg<Context> arg(unsigned id) const { return args_.get(id); }
1025
1026 internal::error_handler error_handler() {
1027 return parse_context_.error_handler();
1028 }
1029
1030 void on_error(const char *message) { parse_context_.on_error(message); }
1031
1032 // Returns an iterator to the beginning of the output range.
1033 iterator out() { return out_; }
1034 iterator begin() { return out_; } // deprecated
1035
1036 // Advances the begin iterator to ``it``.
1037 void advance_to(iterator it) { out_ = it; }
1038
1039 locale_ref locale() { return loc_; }
1040 };
1041
1042 template <typename Context, typename T>
1043 struct get_type {
1044 typedef decltype(make_value<Context>(
1045 declval<typename std::decay<T>::type&>())) value_type;
1046 static const type value = value_type::type_tag;
1047 };
1048
1049 template <typename Context>
1050 FMT_CONSTEXPR11 unsigned long long get_types() { return 0; }
1051
1052 template <typename Context, typename Arg, typename... Args>
1053 FMT_CONSTEXPR11 unsigned long long get_types() {
1054 return get_type<Context, Arg>::value | (get_types<Context, Args...>() << 4);
1055 }
1056
1057 template <typename Context, typename T>
1058 FMT_CONSTEXPR basic_format_arg<Context> make_arg(const T &value) {
1059 basic_format_arg<Context> arg;
1060 arg.type_ = get_type<Context, T>::value;
1061 arg.value_ = make_value<Context>(value);
1062 return arg;
1063 }
1064
1065 template <bool IS_PACKED, typename Context, typename T>
1066 inline typename std::enable_if<IS_PACKED, value<Context>>::type
1067 make_arg(const T &value) {
1068 return make_value<Context>(value);
1069 }
1070
1071 template <bool IS_PACKED, typename Context, typename T>
1072 inline typename std::enable_if<!IS_PACKED, basic_format_arg<Context>>::type
1073 make_arg(const T &value) {
1074 return make_arg<Context>(value);
1075 }
1076 } // namespace internal
1077
1078 // Formatting context.
1079 template <typename OutputIt, typename Char>
1080 class basic_format_context :
1081 public internal::context_base<
1082 OutputIt, basic_format_context<OutputIt, Char>, Char> {
1083 public:
1084 /** The character type for the output. */
1085 typedef Char char_type;
1086
1087 // using formatter_type = formatter<T, char_type>;
1088 template <typename T>
1089 struct formatter_type { typedef formatter<T, char_type> type; };
1090
1091 private:
1092 internal::arg_map<basic_format_context> map_;
1093
1094 basic_format_context(const basic_format_context &) = delete;
1095 void operator=(const basic_format_context &) = delete;
1096
1097 typedef internal::context_base<OutputIt, basic_format_context, Char> base;
1098 typedef typename base::format_arg format_arg;
1099 using base::get_arg;
1100
1101 public:
1102 using typename base::iterator;
1103
1104 /**
1105 Constructs a ``basic_format_context`` object. References to the arguments are
1106 stored in the object so make sure they have appropriate lifetimes.
1107 */
1108 basic_format_context(OutputIt out, basic_string_view<char_type> format_str,
1109 basic_format_args<basic_format_context> ctx_args,
1110 internal::locale_ref loc = internal::locale_ref())
1111 : base(out, format_str, ctx_args, loc) {}
1112
1113 format_arg next_arg() {
1114 return this->do_get_arg(this->parse_context().next_arg_id());
1115 }
1116 format_arg get_arg(unsigned arg_id) { return this->do_get_arg(arg_id); }
1117
1118 // Checks if manual indexing is used and returns the argument with the
1119 // specified name.
1120 format_arg get_arg(basic_string_view<char_type> name);
1121 };
1122
1123 template <typename Char>
1124 struct buffer_context {
1125 typedef basic_format_context<
1126 std::back_insert_iterator<internal::basic_buffer<Char>>, Char> type;
1127 };
1128 typedef buffer_context<char>::type format_context;
1129 typedef buffer_context<wchar_t>::type wformat_context;
1130
1131 /**
1132 \rst
1133 An array of references to arguments. It can be implicitly converted into
1134 `~fmt::basic_format_args` for passing into type-erased formatting functions
1135 such as `~fmt::vformat`.
1136 \endrst
1137 */
1138 template <typename Context, typename ...Args>
1139 class format_arg_store {
1140 private:
1141 static const size_t NUM_ARGS = sizeof...(Args);
1142
1143 // Packed is a macro on MinGW so use IS_PACKED instead.
1144 static const bool IS_PACKED = NUM_ARGS < internal::max_packed_args;
1145
1146 typedef typename std::conditional<IS_PACKED,
1147 internal::value<Context>, basic_format_arg<Context>>::type value_type;
1148
1149 // If the arguments are not packed, add one more element to mark the end.
1150 static const size_t DATA_SIZE =
1151 NUM_ARGS + (IS_PACKED && NUM_ARGS != 0 ? 0 : 1);
1152 value_type data_[DATA_SIZE];
1153
1154 friend class basic_format_args<Context>;
1155
1156 static FMT_CONSTEXPR11 unsigned long long get_types() {
1157 return IS_PACKED ?
1158 internal::get_types<Context, Args...>() :
1159 internal::is_unpacked_bit | NUM_ARGS;
1160 }
1161
1162 public:
1163 #if FMT_USE_CONSTEXPR11
1164 static FMT_CONSTEXPR11 unsigned long long TYPES = get_types();
1165 #else
1166 static const unsigned long long TYPES;
1167 #endif
1168
1169 #if (FMT_GCC_VERSION && FMT_GCC_VERSION <= 405) || \
1170 (FMT_MSC_VER && FMT_MSC_VER <= 1800)
1171 // Workaround array initialization issues in gcc <= 4.5 and MSVC <= 2013.
1172 format_arg_store(const Args &... args) {
1173 value_type init[DATA_SIZE] =
1174 {internal::make_arg<IS_PACKED, Context>(args)...};
1175 std::memcpy(data_, init, sizeof(init));
1176 }
1177 #else
1178 format_arg_store(const Args &... args)
1179 : data_{internal::make_arg<IS_PACKED, Context>(args)...} {}
1180 #endif
1181 };
1182
1183 #if !FMT_USE_CONSTEXPR11
1184 template <typename Context, typename ...Args>
1185 const unsigned long long format_arg_store<Context, Args...>::TYPES =
1186 get_types();
1187 #endif
1188
1189 /**
1190 \rst
1191 Constructs an `~fmt::format_arg_store` object that contains references to
1192 arguments and can be implicitly converted to `~fmt::format_args`. `Context`
1193 can be omitted in which case it defaults to `~fmt::context`.
1194 \endrst
1195 */
1196 template <typename Context = format_context, typename ...Args>
1197 inline format_arg_store<Context, Args...>
1198 make_format_args(const Args &... args) { return {args...}; }
1199
1200 /** Formatting arguments. */
1201 template <typename Context>
1202 class basic_format_args {
1203 public:
1204 typedef unsigned size_type;
1205 typedef basic_format_arg<Context> format_arg;
1206
1207 private:
1208 // To reduce compiled code size per formatting function call, types of first
1209 // max_packed_args arguments are passed in the types_ field.
1210 unsigned long long types_;
1211 union {
1212 // If the number of arguments is less than max_packed_args, the argument
1213 // values are stored in values_, otherwise they are stored in args_.
1214 // This is done to reduce compiled code size as storing larger objects
1215 // may require more code (at least on x86-64) even if the same amount of
1216 // data is actually copied to stack. It saves ~10% on the bloat test.
1217 const internal::value<Context> *values_;
1218 const format_arg *args_;
1219 };
1220
1221 bool is_packed() const { return (types_ & internal::is_unpacked_bit) == 0; }
1222
1223 typename internal::type type(unsigned index) const {
1224 unsigned shift = index * 4;
1225 return static_cast<typename internal::type>(
1226 (types_ & (0xfull << shift)) >> shift);
1227 }
1228
1229 friend class internal::arg_map<Context>;
1230
1231 void set_data(const internal::value<Context> *values) { values_ = values; }
1232 void set_data(const format_arg *args) { args_ = args; }
1233
1234 format_arg do_get(size_type index) const {
1235 format_arg arg;
1236 if (!is_packed()) {
1237 auto num_args = max_size();
1238 if (index < num_args)
1239 arg = args_[index];
1240 return arg;
1241 }
1242 if (index > internal::max_packed_args)
1243 return arg;
1244 arg.type_ = type(index);
1245 if (arg.type_ == internal::none_type)
1246 return arg;
1247 internal::value<Context> &val = arg.value_;
1248 val = values_[index];
1249 return arg;
1250 }
1251
1252 public:
1253 basic_format_args() : types_(0) {}
1254
1255 /**
1256 \rst
1257 Constructs a `basic_format_args` object from `~fmt::format_arg_store`.
1258 \endrst
1259 */
1260 template <typename... Args>
1261 basic_format_args(const format_arg_store<Context, Args...> &store)
1262 : types_(static_cast<unsigned long long>(store.TYPES)) {
1263 set_data(store.data_);
1264 }
1265
1266 /**
1267 \rst
1268 Constructs a `basic_format_args` object from a dynamic set of arguments.
1269 \endrst
1270 */
1271 basic_format_args(const format_arg *args, size_type count)
1272 : types_(internal::is_unpacked_bit | count) {
1273 set_data(args);
1274 }
1275
1276 /** Returns the argument at specified index. */
1277 format_arg get(size_type index) const {
1278 format_arg arg = do_get(index);
1279 if (arg.type_ == internal::named_arg_type)
1280 arg = arg.value_.as_named_arg().template deserialize<Context>();
1281 return arg;
1282 }
1283
1284 size_type max_size() const {
1285 unsigned long long max_packed = internal::max_packed_args;
1286 return static_cast<size_type>(
1287 is_packed() ? max_packed : types_ & ~internal::is_unpacked_bit);
1288 }
1289 };
1290
1291 /** An alias to ``basic_format_args<context>``. */
1292 // It is a separate type rather than a typedef to make symbols readable.
1293 struct format_args : basic_format_args<format_context> {
1294 template <typename ...Args>
1295 format_args(Args &&... arg)
1296 : basic_format_args<format_context>(std::forward<Args>(arg)...) {}
1297 };
1298 struct wformat_args : basic_format_args<wformat_context> {
1299 template <typename ...Args>
1300 wformat_args(Args &&... arg)
1301 : basic_format_args<wformat_context>(std::forward<Args>(arg)...) {}
1302 };
1303
1304 #ifndef FMT_USE_ALIAS_TEMPLATES
1305 # define FMT_USE_ALIAS_TEMPLATES FMT_HAS_FEATURE(cxx_alias_templates)
1306 #endif
1307 #if FMT_USE_ALIAS_TEMPLATES
1308 /** String's character type. */
1309 template <typename S>
1310 using char_t = typename std::enable_if<internal::is_string<S>::value,
1311 typename internal::char_t<S>::type>::type;
1312 #define FMT_CHAR(S) fmt::char_t<S>
1313
1314 template <typename S, typename T>
1315 using enable_if_string_t =
1316 typename std::enable_if<internal::is_string<S>::value, T>::type;
1317 #define FMT_ENABLE_IF_STRING(S, T) enable_if_string_t<S, T>
1318 #else
1319 template <typename S>
1320 struct char_t : std::enable_if<
1321 internal::is_string<S>::value, typename internal::char_t<S>::type> {};
1322 #define FMT_CHAR(S) typename char_t<S>::type
1323
1324 #define FMT_ENABLE_IF_STRING(S, T) \
1325 typename std::enable_if<internal::is_string<S>::value, T>::type
1326 #endif
1327
1328 namespace internal {
1329 template <typename Char>
1330 struct named_arg_base {
1331 basic_string_view<Char> name;
1332
1333 // Serialized value<context>.
1334 mutable char data[
1335 sizeof(basic_format_arg<typename buffer_context<Char>::type>)];
1336
1337 named_arg_base(basic_string_view<Char> nm) : name(nm) {}
1338
1339 template <typename Context>
1340 basic_format_arg<Context> deserialize() const {
1341 basic_format_arg<Context> arg;
1342 std::memcpy(&arg, data, sizeof(basic_format_arg<Context>));
1343 return arg;
1344 }
1345 };
1346
1347 template <typename T, typename Char>
1348 struct named_arg : named_arg_base<Char> {
1349 const T &value;
1350
1351 named_arg(basic_string_view<Char> name, const T &val)
1352 : named_arg_base<Char>(name), value(val) {}
1353 };
1354
1355 template <typename... Args, typename S>
1356 inline typename std::enable_if<!is_compile_string<S>::value>::type
1357 check_format_string(const S &) {}
1358 template <typename... Args, typename S>
1359 typename std::enable_if<is_compile_string<S>::value>::type
1360 check_format_string(S);
1361
1362 template <typename S, typename... Args>
1363 struct checked_args : format_arg_store<
1364 typename buffer_context<FMT_CHAR(S)>::type, Args...> {
1365 typedef typename buffer_context<FMT_CHAR(S)>::type context;
1366
1367 checked_args(const S &format_str, const Args &... args):
1368 format_arg_store<context, Args...>(args...) {
1369 internal::check_format_string<Args...>(format_str);
1370 }
1371
1372 basic_format_args<context> operator*() const { return *this; }
1373 };
1374
1375 template <typename Char>
1376 std::basic_string<Char> vformat(
1377 basic_string_view<Char> format_str,
1378 basic_format_args<typename buffer_context<Char>::type> args);
1379
1380 template <typename Char>
1381 typename buffer_context<Char>::type::iterator vformat_to(
1382 internal::basic_buffer<Char> &buf, basic_string_view<Char> format_str,
1383 basic_format_args<typename buffer_context<Char>::type> args);
1384 }
1385
1386 /**
1387 \rst
1388 Returns a named argument to be used in a formatting function.
1389
1390 **Example**::
1391
1392 fmt::print("Elapsed time: {s:.2f} seconds", fmt::arg("s", 1.23));
1393 \endrst
1394 */
1395 template <typename T>
1396 inline internal::named_arg<T, char> arg(string_view name, const T &arg) {
1397 return {name, arg};
1398 }
1399
1400 template <typename T>
1401 inline internal::named_arg<T, wchar_t> arg(wstring_view name, const T &arg) {
1402 return {name, arg};
1403 }
1404
1405 // Disable nested named arguments, e.g. ``arg("a", arg("b", 42))``.
1406 template <typename S, typename T, typename Char>
1407 void arg(S, internal::named_arg<T, Char>) = delete;
1408
1409 template <typename Container>
1410 struct is_contiguous: std::false_type {};
1411
1412 template <typename Char>
1413 struct is_contiguous<std::basic_string<Char> >: std::true_type {};
1414
1415 template <typename Char>
1416 struct is_contiguous<internal::basic_buffer<Char> >: std::true_type {};
1417
1418 /** Formats a string and writes the output to ``out``. */
1419 template <typename Container, typename S>
1420 typename std::enable_if<
1421 is_contiguous<Container>::value, std::back_insert_iterator<Container>>::type
1422 vformat_to(
1423 std::back_insert_iterator<Container> out,
1424 const S &format_str,
1425 basic_format_args<typename buffer_context<FMT_CHAR(S)>::type> args) {
1426 internal::container_buffer<Container> buf(internal::get_container(out));
1427 internal::vformat_to(buf, to_string_view(format_str), args);
1428 return out;
1429 }
1430
1431 template <typename Container, typename S, typename... Args>
1432 inline typename std::enable_if<
1433 is_contiguous<Container>::value && internal::is_string<S>::value,
1434 std::back_insert_iterator<Container>>::type
1435 format_to(std::back_insert_iterator<Container> out, const S &format_str,
1436 const Args &... args) {
1437 internal::checked_args<S, Args...> ca(format_str, args...);
1438 return vformat_to(out, to_string_view(format_str), *ca);
1439 }
1440
1441 template <typename S, typename Char = FMT_CHAR(S)>
1442 inline std::basic_string<Char> vformat(
1443 const S &format_str,
1444 basic_format_args<typename buffer_context<Char>::type> args) {
1445 return internal::vformat(to_string_view(format_str), args);
1446 }
1447
1448 /**
1449 \rst
1450 Formats arguments and returns the result as a string.
1451
1452 **Example**::
1453
1454 #include <fmt/core.h>
1455 std::string message = fmt::format("The answer is {}", 42);
1456 \endrst
1457 */
1458 template <typename S, typename... Args>
1459 inline std::basic_string<FMT_CHAR(S)> format(
1460 const S &format_str, const Args &... args) {
1461 return internal::vformat(
1462 to_string_view(format_str),
1463 *internal::checked_args<S, Args...>(format_str, args...));
1464 }
1465
1466 FMT_API void vprint(std::FILE *f, string_view format_str, format_args args);
1467 FMT_API void vprint(std::FILE *f, wstring_view format_str, wformat_args args);
1468
1469 /**
1470 \rst
1471 Prints formatted data to the file *f*. For wide format strings,
1472 *f* should be in wide-oriented mode set via ``fwide(f, 1)`` or
1473 ``_setmode(_fileno(f), _O_U8TEXT)`` on Windows.
1474
1475 **Example**::
1476
1477 fmt::print(stderr, "Don't {}!", "panic");
1478 \endrst
1479 */
1480 template <typename S, typename... Args>
1481 inline FMT_ENABLE_IF_STRING(S, void)
1482 print(std::FILE *f, const S &format_str, const Args &... args) {
1483 vprint(f, to_string_view(format_str),
1484 internal::checked_args<S, Args...>(format_str, args...));
1485 }
1486
1487 FMT_API void vprint(string_view format_str, format_args args);
1488 FMT_API void vprint(wstring_view format_str, wformat_args args);
1489
1490 /**
1491 \rst
1492 Prints formatted data to ``stdout``.
1493
1494 **Example**::
1495
1496 fmt::print("Elapsed time: {0:.2f} seconds", 1.23);
1497 \endrst
1498 */
1499 template <typename S, typename... Args>
1500 inline FMT_ENABLE_IF_STRING(S, void)
1501 print(const S &format_str, const Args &... args) {
1502 vprint(to_string_view(format_str),
1503 internal::checked_args<S, Args...>(format_str, args...));
1504 }
1505 FMT_END_NAMESPACE
1506
1507 #endif // FMT_CORE_H_