WPILibC++ 2024.3.2
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> // std::filebuf
12
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
24namespace detail {
25
26// Generate a unique explicit instantion in every translation unit using a tag
27// type in an anonymous namespace.
28namespace {
29struct file_access_tag {};
30} // namespace
31template <typename Tag, typename BufType, FILE* BufType::*FileMemberPtr>
33 friend auto get_file(BufType& obj) -> FILE* { return obj.*FileMemberPtr; }
34};
35
36#if FMT_MSC_VERSION
37template class file_access<file_access_tag, std::filebuf,
38 &std::filebuf::_Myfile>;
39auto get_file(std::filebuf&) -> FILE*;
40#elif defined(_WIN32) && defined(_LIBCPP_VERSION)
41template class file_access<file_access_tag, std::__stdoutbuf<char>,
42 &std::__stdoutbuf<char>::__file_>;
43auto get_file(std::__stdoutbuf<char>&) -> FILE*;
44#endif
45
46inline bool write_ostream_unicode(std::ostream& os, fmt::string_view data) {
47#if FMT_MSC_VERSION
48 if (auto* buf = dynamic_cast<std::filebuf*>(os.rdbuf()))
49 if (FILE* f = get_file(*buf)) return write_console(f, data);
50#elif defined(_WIN32) && defined(__GLIBCXX__)
51 auto* rdbuf = os.rdbuf();
52 FILE* c_file;
53 if (auto* sfbuf = dynamic_cast<__gnu_cxx::stdio_sync_filebuf<char>*>(rdbuf))
54 c_file = sfbuf->file();
55 else if (auto* fbuf = dynamic_cast<__gnu_cxx::stdio_filebuf<char>*>(rdbuf))
56 c_file = fbuf->file();
57 else
58 return false;
59 if (c_file) return write_console(c_file, data);
60#elif defined(_WIN32) && defined(_LIBCPP_VERSION)
61 if (auto* buf = dynamic_cast<std::__stdoutbuf<char>*>(os.rdbuf()))
62 if (FILE* f = get_file(*buf)) return write_console(f, data);
63#else
65#endif
66 return false;
67}
68inline bool write_ostream_unicode(std::wostream&,
69 fmt::basic_string_view<wchar_t>) {
70 return false;
71}
72
73// Write the content of buf to os.
74// It is a separate function rather than a part of vprint to simplify testing.
75template <typename Char>
76void write_buffer(std::basic_ostream<Char>& os, buffer<Char>& buf) {
77 const Char* buf_data = buf.data();
78 using unsigned_streamsize = std::make_unsigned<std::streamsize>::type;
79 unsigned_streamsize size = buf.size();
80 unsigned_streamsize max_size = to_unsigned(max_value<std::streamsize>());
81 do {
82 unsigned_streamsize n = size <= max_size ? size : max_size;
83 os.write(buf_data, static_cast<std::streamsize>(n));
84 buf_data += n;
85 size -= n;
86 } while (size != 0);
87}
88
89template <typename Char, typename T>
90void format_value(buffer<Char>& buf, const T& value,
91 locale_ref loc = locale_ref()) {
92 auto&& format_buf = formatbuf<std::basic_streambuf<Char>>(buf);
93 auto&& output = std::basic_ostream<Char>(&format_buf);
94#if !defined(FMT_STATIC_THOUSANDS_SEPARATOR)
95 if (loc) output.imbue(loc.get<std::locale>());
96#endif
97 output << value;
98 output.exceptions(std::ios_base::failbit | std::ios_base::badbit);
99}
100
101template <typename T> struct streamed_view { const T& value; };
102
103} // namespace detail
104
105// Formats an object of type T that has an overloaded ostream operator<<.
106template <typename Char>
107struct basic_ostream_formatter : formatter<basic_string_view<Char>, Char> {
108 void set_debug_format() = delete;
109
110 template <typename T, typename OutputIt>
111 auto format(const T& value, basic_format_context<OutputIt, Char>& ctx) const
112 -> OutputIt {
113 auto buffer = basic_memory_buffer<Char>();
114 detail::format_value(buffer, value, ctx.locale());
116 {buffer.data(), buffer.size()}, ctx);
117 }
118};
119
121
122template <typename T, typename Char>
123struct formatter<detail::streamed_view<T>, Char>
125 template <typename OutputIt>
127 basic_format_context<OutputIt, Char>& ctx) const -> OutputIt {
128 return basic_ostream_formatter<Char>::format(view.value, ctx);
129 }
130};
131
132/**
133 \rst
134 Returns a view that formats `value` via an ostream ``operator<<``.
135
136 **Example**::
137
138 fmt::print("Current thread id: {}\n",
139 fmt::streamed(std::this_thread::get_id()));
140 \endrst
141 */
142template <typename T>
143auto streamed(const T& value) -> detail::streamed_view<T> {
144 return {value};
145}
146
147namespace detail {
148
149inline void vprint_directly(std::ostream& os, string_view format_str,
150 format_args args) {
151 auto buffer = memory_buffer();
152 detail::vformat_to(buffer, format_str, args);
154}
155
156} // namespace detail
157
158FMT_EXPORT template <typename Char>
159void vprint(std::basic_ostream<Char>& os,
162 auto buffer = basic_memory_buffer<Char>();
163 detail::vformat_to(buffer, format_str, args);
164 if (detail::write_ostream_unicode(os, {buffer.data(), buffer.size()})) return;
165 detail::write_buffer(os, buffer);
166}
167
168/**
169 \rst
170 Prints formatted data to the stream *os*.
171
172 **Example**::
173
174 fmt::print(cerr, "Don't {}!", "panic");
175 \endrst
176 */
177FMT_EXPORT template <typename... T>
178void print(std::ostream& os, format_string<T...> fmt, T&&... args) {
179 const auto& vargs = fmt::make_format_args(args...);
180 if (detail::is_utf8())
181 vprint(os, fmt, vargs);
182 else
183 detail::vprint_directly(os, fmt, vargs);
184}
185
187template <typename... Args>
188void print(std::wostream& os,
190 Args&&... args) {
192}
193
194FMT_EXPORT template <typename... T>
195void println(std::ostream& os, format_string<T...> fmt, T&&... args) {
196 fmt::print(os, "{}\n", fmt::format(fmt, std::forward<T>(args)...));
197}
198
200template <typename... Args>
201void println(std::wostream& os,
203 Args&&... args) {
204 print(os, L"{}\n", fmt::format(fmt, std::forward<Args>(args)...));
205}
206
208
209#endif // FMT_OSTREAM_H_
\rst A view of a collection of formatting arguments.
Definition: core.h:1857
Definition: core.h:1706
A compile-time format string.
Definition: core.h:2721
\rst A dynamically growing memory buffer for trivially copyable/constructible types with the first SI...
Definition: format.h:918
\rst A contiguous memory buffer with an optional growing ability.
Definition: core.h:798
constexpr auto size() const noexcept -> size_t
Returns the size of this buffer.
Definition: core.h:838
FMT_CONSTEXPR auto data() noexcept -> T *
Returns a pointer to the buffer data (not null-terminated).
Definition: core.h:844
Definition: ostream.h:32
friend auto get_file(BufType &obj) -> FILE *
Definition: ostream.h:33
Definition: format.h:310
Definition: core.h:1529
Definition: core.h:1238
basic_string_view< char > string_view
Definition: core.h:501
#define FMT_BEGIN_NAMESPACE
Definition: core.h:174
constexpr auto make_format_args(T &... args) -> format_arg_store< Context, remove_cvref_t< T >... >
\rst Constructs a ~fmtformat_arg_store object that contains references to arguments and can be implic...
Definition: core.h:1824
typename type_identity< T >::type type_identity_t
Definition: core.h:267
#define FMT_END_NAMESPACE
Definition: core.h:177
#define FMT_EXPORT
Definition: core.h:183
basic_memory_buffer< char > memory_buffer
Definition: format.h:1029
detail namespace with internal helper functions
Definition: xchar.h:20
FMT_FUNC bool write_console(std::FILE *, string_view)
Definition: format-inl.h:1428
bool write_ostream_unicode(std::ostream &os, fmt::string_view data)
Definition: ostream.h:46
FMT_CONSTEXPR void ignore_unused(const T &...)
Definition: core.h:302
void vprint_directly(std::ostream &os, string_view format_str, format_args args)
Definition: ostream.h:149
void format_value(buffer< Char > &buf, const T &value, locale_ref loc=locale_ref())
Definition: ostream.h:90
FMT_CONSTEXPR auto is_utf8() -> bool
Definition: core.h:380
void write_buffer(std::basic_ostream< Char > &os, buffer< Char > &buf)
Definition: ostream.h:76
void vformat_to(buffer< Char > &buf, const text_style &ts, basic_string_view< Char > format_str, basic_format_args< buffer_context< type_identity_t< Char > > > args)
Definition: color.h:436
type
Definition: core.h:556
FMT_CONSTEXPR auto to_unsigned(Int value) -> typename std::make_unsigned< Int >::type
Definition: core.h:374
cubed< length::millimeter > L
Definition: volume.h:49
FMT_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:159
FMT_EXPORT void println(std::ostream &os, format_string< T... > fmt, T &&... args)
Definition: ostream.h:195
auto streamed(const T &value) -> detail::streamed_view< T >
\rst Returns a view that formats value via an ostream operator<<.
Definition: ostream.h:143
FMT_EXPORT void print(std::ostream &os, format_string< T... > fmt, T &&... args)
\rst Prints formatted data to the stream os.
Definition: ostream.h:178
Definition: ostream.h:107
void set_debug_format()=delete
auto format(const T &value, basic_format_context< OutputIt, Char > &ctx) const -> OutputIt
Definition: ostream.h:111
Definition: format.h:1762
Definition: ostream.h:101
const T & value
Definition: ostream.h:101
auto format(detail::streamed_view< T > view, basic_format_context< OutputIt, Char > &ctx) const -> OutputIt
Definition: ostream.h:126
Definition: core.h:1068
auto format(wformat_string< T... > fmt, T &&... args) -> std::wstring
Definition: xchar.h:108