WPILibC++ 2025.2.1
Loading...
Searching...
No Matches
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#ifndef FMT_MODULE
12# include <fstream> // std::filebuf
13#endif
14
15#ifdef _WIN32
16# ifdef __GLIBCXX__
17# include <ext/stdio_filebuf.h>
18# include <ext/stdio_sync_filebuf.h>
19# endif
20# include <io.h>
21#endif
22
23#include "chrono.h" // formatbuf
24
25#ifdef _MSVC_STL_UPDATE
26# define FMT_MSVC_STL_UPDATE _MSVC_STL_UPDATE
27#elif defined(_MSC_VER) && _MSC_VER < 1912 // VS 15.5
28# define FMT_MSVC_STL_UPDATE _MSVC_LANG
29#else
30# define FMT_MSVC_STL_UPDATE 0
31#endif
32
34namespace detail {
35
36// Generate a unique explicit instantion in every translation unit using a tag
37// type in an anonymous namespace.
38namespace {
39struct file_access_tag {};
40} // namespace
41template <typename Tag, typename BufType, FILE* BufType::*FileMemberPtr>
43 friend auto get_file(BufType& obj) -> FILE* { return obj.*FileMemberPtr; }
44};
45
46#if FMT_MSVC_STL_UPDATE
47template class file_access<file_access_tag, std::filebuf,
48 &std::filebuf::_Myfile>;
49auto get_file(std::filebuf&) -> FILE*;
50#endif
51
52// Write the content of buf to os.
53// It is a separate function rather than a part of vprint to simplify testing.
54template <typename Char>
55void write_buffer(std::basic_ostream<Char>& os, buffer<Char>& buf) {
56 const Char* buf_data = buf.data();
57 using unsigned_streamsize = make_unsigned_t<std::streamsize>;
58 unsigned_streamsize size = buf.size();
59 unsigned_streamsize max_size = to_unsigned(max_value<std::streamsize>());
60 do {
61 unsigned_streamsize n = size <= max_size ? size : max_size;
62 os.write(buf_data, static_cast<std::streamsize>(n));
63 buf_data += n;
64 size -= n;
65 } while (size != 0);
66}
67
68template <typename T> struct streamed_view {
69 const T& value;
70};
71} // namespace detail
72
73// Formats an object of type T that has an overloaded ostream operator<<.
74template <typename Char>
75struct basic_ostream_formatter : formatter<basic_string_view<Char>, Char> {
76 void set_debug_format() = delete;
77
78 template <typename T, typename Context>
79 auto format(const T& value, Context& ctx) const -> decltype(ctx.out()) {
80 auto buffer = basic_memory_buffer<Char>();
81 auto&& formatbuf = detail::formatbuf<std::basic_streambuf<Char>>(buffer);
82 auto&& output = std::basic_ostream<Char>(&formatbuf);
83 output.imbue(std::locale::classic()); // The default is always unlocalized.
84 output << value;
85 output.exceptions(std::ios_base::failbit | std::ios_base::badbit);
87 {buffer.data(), buffer.size()}, ctx);
88 }
89};
90
92
93template <typename T, typename Char>
94struct formatter<detail::streamed_view<T>, Char>
96 template <typename Context>
97 auto format(detail::streamed_view<T> view, Context& ctx) const
98 -> decltype(ctx.out()) {
99 return basic_ostream_formatter<Char>::format(view.value, ctx);
100 }
101};
102
103/**
104 * Returns a view that formats `value` via an ostream `operator<<`.
105 *
106 * **Example**:
107 *
108 * fmt::print("Current thread id: {}\n",
109 * fmt::streamed(std::this_thread::get_id()));
110 */
111template <typename T>
112constexpr auto streamed(const T& value) -> detail::streamed_view<T> {
113 return {value};
114}
115
116inline void vprint(std::ostream& os, string_view fmt, format_args args) {
117 auto buffer = memory_buffer();
118 detail::vformat_to(buffer, fmt, args);
119 FILE* f = nullptr;
120#if FMT_MSVC_STL_UPDATE && FMT_USE_RTTI
121 if (auto* buf = dynamic_cast<std::filebuf*>(os.rdbuf()))
122 f = detail::get_file(*buf);
123#elif defined(_WIN32) && defined(__GLIBCXX__) && FMT_USE_RTTI
124 auto* rdbuf = os.rdbuf();
125 if (auto* sfbuf = dynamic_cast<__gnu_cxx::stdio_sync_filebuf<char>*>(rdbuf))
126 f = sfbuf->file();
127 else if (auto* fbuf = dynamic_cast<__gnu_cxx::stdio_filebuf<char>*>(rdbuf))
128 f = fbuf->file();
129#endif
130#ifdef _WIN32
131 if (f) {
132 int fd = _fileno(f);
133 if (_isatty(fd)) {
134 os.flush();
135 if (detail::write_console(fd, {buffer.data(), buffer.size()})) return;
136 }
137 }
138#endif
140 detail::write_buffer(os, buffer);
141}
142
143/**
144 * Prints formatted data to the stream `os`.
145 *
146 * **Example**:
147 *
148 * fmt::print(cerr, "Don't {}!", "panic");
149 */
150FMT_EXPORT template <typename... T>
151void print(std::ostream& os, format_string<T...> fmt, T&&... args) {
152 fmt::vargs<T...> vargs = {{args...}};
153 if (detail::use_utf8) return vprint(os, fmt.str, vargs);
154 auto buffer = memory_buffer();
155 detail::vformat_to(buffer, fmt.str, vargs);
156 detail::write_buffer(os, buffer);
157}
158
159FMT_EXPORT template <typename... T>
160void println(std::ostream& os, format_string<T...> fmt, T&&... args) {
161 fmt::print(os, "{}\n", fmt::format(fmt, std::forward<T>(args)...));
162}
163
165
166#endif // FMT_OSTREAM_H_
A dynamically growing memory buffer for trivially copyable/constructible types with the first SIZE el...
Definition format.h:790
A contiguous memory buffer with an optional growing ability.
Definition base.h:1698
constexpr auto size() const noexcept -> size_t
Returns the size of this buffer.
Definition base.h:1740
FMT_CONSTEXPR auto data() noexcept -> T *
Returns a pointer to the buffer data (not null-terminated).
Definition base.h:1746
Definition ostream.h:42
friend auto get_file(BufType &obj) -> FILE *
Definition ostream.h:43
Definition chrono.h:307
basic_memory_buffer< char > memory_buffer
Definition format.h:891
detail namespace with internal helper functions
Definition input_adapters.h:32
FMT_CONSTEXPR void ignore_unused(const T &...)
Definition base.h:348
constexpr auto max_value() -> T
Definition format.h:407
FMT_CONSTEXPR auto to_unsigned(Int value) -> make_unsigned_t< Int >
Definition base.h:422
auto get_file(F *f, int) -> apple_file< F >
Definition format-inl.h:1642
@ use_utf8
Definition base.h:442
FMT_API auto write_console(int fd, string_view text) -> bool
Definition format-inl.h:1694
void write_buffer(std::basic_ostream< Char > &os, buffer< Char > &buf)
Definition ostream.h:55
void vformat_to(buffer< Char > &buf, basic_string_view< Char > fmt, typename vformat_args< Char >::type args, locale_ref loc={})
Definition format.h:3665
FMT_EXPORT void println(std::ostream &os, format_string< T... > fmt, T &&... args)
Definition ostream.h:160
FMT_EXPORT void print(std::ostream &os, format_string< T... > fmt, T &&... args)
Prints formatted data to the stream os.
Definition ostream.h:151
constexpr auto streamed(const T &value) -> detail::streamed_view< T >
Returns a view that formats value via an ostream operator<<.
Definition ostream.h:112
void vprint(std::ostream &os, string_view fmt, format_args args)
Definition ostream.h:116
Definition ostream.h:75
auto format(const T &value, Context &ctx) const -> decltype(ctx.out())
Definition ostream.h:79
void set_debug_format()=delete
Definition base.h:2323
Definition ostream.h:68
const T & value
Definition ostream.h:69
auto format(detail::streamed_view< T > view, Context &ctx) const -> decltype(ctx.out())
Definition ostream.h:97
Definition base.h:651
typename fstring< T... >::t format_string
Definition base.h:2720
typename std::make_unsigned< T >::type make_unsigned_t
Definition base.h:308
#define FMT_BEGIN_NAMESPACE
Definition base.h:239
#define FMT_END_NAMESPACE
Definition base.h:242
#define FMT_EXPORT
Definition base.h:248