WPILibC++ 2023.4.3
ostream.h
Go to the documentation of this file.
1// Formatting library for C++ - std::ostream support
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_OSTREAM_H_
9#define FMT_OSTREAM_H_
10
11#include <fstream>
12#include <ostream>
13#if defined(_WIN32) && defined(__GLIBCXX__)
14# include <ext/stdio_filebuf.h>
15# include <ext/stdio_sync_filebuf.h>
16#elif defined(_WIN32) && defined(_LIBCPP_VERSION)
17# include <__std_stream>
18#endif
19
20#include "format.h"
21
23
24template <typename OutputIt, typename Char> class basic_printf_context;
25
26namespace detail {
27
28// Checks if T has a user-defined operator<<.
29template <typename T, typename Char, typename Enable = void>
31 private:
32 template <typename U>
33 static auto test(int)
34 -> bool_constant<sizeof(std::declval<std::basic_ostream<Char>&>()
35 << std::declval<U>()) != 0>;
36
37 template <typename> static auto test(...) -> std::false_type;
38
39 using result = decltype(test<T>(0));
40
41 public:
42 is_streamable() = default;
43
44 static const bool value = result::value;
45};
46
47// Formatting of built-in types and arrays is intentionally disabled because
48// it's handled by standard (non-ostream) formatters.
49template <typename T, typename Char>
51 T, Char,
53 std::is_arithmetic<T>::value || std::is_array<T>::value ||
54 std::is_pointer<T>::value || std::is_same<T, char8_type>::value ||
55 std::is_convertible<T, fmt::basic_string_view<Char>>::value ||
56 std::is_same<T, std_string_view<Char>>::value ||
57 (std::is_convertible<T, int>::value && !std::is_enum<T>::value)>>
58 : std::false_type {};
59
60// Generate a unique explicit instantion in every translation unit using a tag
61// type in an anonymous namespace.
62namespace {
63struct file_access_tag {};
64} // namespace
65template <class Tag, class BufType, FILE* BufType::*FileMemberPtr>
67 friend auto get_file(BufType& obj) -> FILE* { return obj.*FileMemberPtr; }
68};
69
70#if FMT_MSC_VERSION
71template class file_access<file_access_tag, std::filebuf,
72 &std::filebuf::_Myfile>;
73auto get_file(std::filebuf&) -> FILE*;
74#elif defined(_WIN32) && defined(_LIBCPP_VERSION)
75template class file_access<file_access_tag, std::__stdoutbuf<char>,
76 &std::__stdoutbuf<char>::__file_>;
77auto get_file(std::__stdoutbuf<char>&) -> FILE*;
78#endif
79
80inline bool write_ostream_unicode(std::ostream& os, fmt::string_view data) {
81#if FMT_MSC_VERSION
82 if (auto* buf = dynamic_cast<std::filebuf*>(os.rdbuf()))
83 if (FILE* f = get_file(*buf)) return write_console(f, data);
84#elif defined(_WIN32) && defined(__GLIBCXX__)
85 auto* rdbuf = os.rdbuf();
86 FILE* c_file;
87 if (auto* fbuf = dynamic_cast<__gnu_cxx::stdio_sync_filebuf<char>*>(rdbuf))
88 c_file = fbuf->file();
89 else if (auto* fbuf = dynamic_cast<__gnu_cxx::stdio_filebuf<char>*>(rdbuf))
90 c_file = fbuf->file();
91 else
92 return false;
93 if (c_file) return write_console(c_file, data);
94#elif defined(_WIN32) && defined(_LIBCPP_VERSION)
95 if (auto* buf = dynamic_cast<std::__stdoutbuf<char>*>(os.rdbuf()))
96 if (FILE* f = get_file(*buf)) return write_console(f, data);
97#else
99#endif
100 return false;
101}
102inline bool write_ostream_unicode(std::wostream&,
103 fmt::basic_string_view<wchar_t>) {
104 return false;
105}
106
107// Write the content of buf to os.
108// It is a separate function rather than a part of vprint to simplify testing.
109template <typename Char>
110void write_buffer(std::basic_ostream<Char>& os, buffer<Char>& buf) {
111 const Char* buf_data = buf.data();
112 using unsigned_streamsize = std::make_unsigned<std::streamsize>::type;
113 unsigned_streamsize size = buf.size();
114 unsigned_streamsize max_size = to_unsigned(max_value<std::streamsize>());
115 do {
116 unsigned_streamsize n = size <= max_size ? size : max_size;
117 os.write(buf_data, static_cast<std::streamsize>(n));
118 buf_data += n;
119 size -= n;
120 } while (size != 0);
121}
122
123template <typename Char, typename T>
124void format_value(buffer<Char>& buf, const T& value,
125 locale_ref loc = locale_ref()) {
126 auto&& format_buf = formatbuf<std::basic_streambuf<Char>>(buf);
127 auto&& output = std::basic_ostream<Char>(&format_buf);
128#if !defined(FMT_STATIC_THOUSANDS_SEPARATOR)
129 if (loc) output.imbue(loc.get<std::locale>());
130#endif
131 output << value;
132 output.exceptions(std::ios_base::failbit | std::ios_base::badbit);
133}
134
135template <typename T> struct streamed_view { const T& value; };
136
137} // namespace detail
138
139// Formats an object of type T that has an overloaded ostream operator<<.
140template <typename Char>
141struct basic_ostream_formatter : formatter<basic_string_view<Char>, Char> {
142 void set_debug_format() = delete;
143
144 template <typename T, typename OutputIt>
146 -> OutputIt {
148 format_value(buffer, value, ctx.locale());
150 {buffer.data(), buffer.size()}, ctx);
151 }
152};
153
155
156template <typename T, typename Char>
157struct formatter<detail::streamed_view<T>, Char>
159 template <typename OutputIt>
161 basic_format_context<OutputIt, Char>& ctx) const -> OutputIt {
163 }
164};
165
166/**
167 \rst
168 Returns a view that formats `value` via an ostream ``operator<<``.
169
170 **Example**::
171
172 fmt::print("Current thread id: {}\n",
173 fmt::streamed(std::this_thread::get_id()));
174 \endrst
175 */
176template <typename T>
178 return {value};
179}
180
181namespace detail {
182
183// Formats an object of type T that has an overloaded ostream operator<<.
184template <typename T, typename Char>
188};
189
190inline void vprint_directly(std::ostream& os, string_view format_str,
191 format_args args) {
192 auto buffer = memory_buffer();
193 detail::vformat_to(buffer, format_str, args);
194 detail::write_buffer(os, buffer);
195}
196
197} // namespace detail
198
199FMT_MODULE_EXPORT template <typename Char>
200void vprint(std::basic_ostream<Char>& os,
204 detail::vformat_to(buffer, format_str, args);
205 if (detail::write_ostream_unicode(os, {buffer.data(), buffer.size()})) return;
207}
208
209/**
210 \rst
211 Prints formatted data to the stream *os*.
212
213 **Example**::
214
215 fmt::print(cerr, "Don't {}!", "panic");
216 \endrst
217 */
218FMT_MODULE_EXPORT template <typename... T>
219void print(std::ostream& os, format_string<T...> fmt, T&&... args) {
220 const auto& vargs = fmt::make_format_args(args...);
221 if (detail::is_utf8())
222 vprint(os, fmt, vargs);
223 else
224 detail::vprint_directly(os, fmt, vargs);
225}
226
228template <typename... Args>
229void print(std::wostream& os,
231 Args&&... args) {
233}
234
236
237#endif // FMT_OSTREAM_H_
\rst A view of a collection of formatting arguments.
Definition: core.h:1948
Definition: core.h:1788
A compile-time format string.
Definition: core.h:3140
\rst A dynamically growing memory buffer for trivially copyable/constructible types with the first SI...
Definition: format.h:819
Definition: printf.h:26
\rst A contiguous memory buffer with an optional growing ability.
Definition: core.h:862
FMT_CONSTEXPR auto data() noexcept -> T *
Returns a pointer to the buffer data.
Definition: core.h:908
constexpr auto size() const noexcept -> size_t
Returns the size of this buffer.
Definition: core.h:902
Definition: ostream.h:66
friend auto get_file(BufType &obj) -> FILE *
Definition: ostream.h:67
Definition: format.h:264
Definition: ostream.h:30
Definition: core.h:1710
Definition: core.h:1240
typename std::enable_if< B, T >::type enable_if_t
Definition: core.h:298
std::integral_constant< bool, B > bool_constant
Definition: core.h:301
basic_string_view< char > string_view
Definition: core.h:520
constexpr auto make_format_args(Args &&... args) -> format_arg_store< Context, remove_cvref_t< Args >... >
\rst Constructs a ~fmtformat_arg_store object that contains references to arguments and can be implic...
Definition: core.h:1916
type
Definition: core.h:575
#define FMT_BEGIN_NAMESPACE
Definition: core.h:214
#define FMT_MODULE_EXPORT
Definition: core.h:223
FMT_BEGIN_DETAIL_NAMESPACE FMT_CONSTEXPR void ignore_unused(const T &...)
Definition: core.h:343
constexpr auto is_utf8() -> bool
Definition: core.h:415
FMT_CONSTEXPR auto to_unsigned(Int value) -> typename std::make_unsigned< Int >::type
Definition: core.h:407
typename type_identity< T >::type type_identity_t
Definition: core.h:309
#define FMT_END_NAMESPACE
Definition: core.h:217
basic_memory_buffer< char > memory_buffer
Definition: format.h:934
EIGEN_CONSTEXPR Index size(const T &x)
Definition: Meta.h:479
Definition: chrono.h:303
bool write_ostream_unicode(std::ostream &os, fmt::string_view data)
Definition: ostream.h:80
void vprint_directly(std::ostream &os, string_view format_str, format_args args)
Definition: ostream.h:190
void format_value(buffer< Char > &buf, const T &value, locale_ref loc=locale_ref())
Definition: ostream.h:124
void write_buffer(std::basic_ostream< Char > &os, buffer< Char > &buf)
Definition: ostream.h:110
basic_filebuf< char > filebuf
Definition: filesystem.hpp:1240
Definition: StdDeque.h:50
auto streamed(const T &value) -> detail::streamed_view< T >
\rst Returns a view that formats value via an ostream operator<<.
Definition: ostream.h:177
FMT_MODULE_EXPORT void print(std::ostream &os, format_string< T... > fmt, T &&... args)
\rst Prints formatted data to the stream os.
Definition: ostream.h:219
FMT_MODULE_EXPORT void vprint(std::basic_ostream< Char > &os, basic_string_view< type_identity_t< Char > > format_str, basic_format_args< buffer_context< type_identity_t< Char > > > args)
Definition: ostream.h:200
Definition: ostream.h:141
void set_debug_format()=delete
auto format(const T &value, basic_format_context< OutputIt, Char > &ctx) const -> OutputIt
Definition: ostream.h:145
Definition: format.h:1544
Definition: ostream.h:135
const T & value
Definition: ostream.h:135
Definition: core.h:1123
auto format(detail::streamed_view< T > view, basic_format_context< OutputIt, Char > &ctx) const -> OutputIt
Definition: ostream.h:160
Definition: core.h:791
Definition: core.h:1136
auto format(wformat_string< T... > fmt, T &&... args) -> std::wstring
Definition: xchar.h:87
auto vformat_to(OutputIt out, const S &format_str, basic_format_args< buffer_context< type_identity_t< Char > > > args) -> OutputIt
Definition: xchar.h:124