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 <ostream>
12
13 #include "format.h"
14
15 FMT_BEGIN_NAMESPACE
16
17 template <typename Char> class basic_printf_parse_context;
18 template <typename OutputIt, typename Char> class basic_printf_context;
19
20 namespace detail {
21
22 template <class Char> class formatbuf : public std::basic_streambuf<Char> {
23 private:
24 using int_type = typename std::basic_streambuf<Char>::int_type;
25 using traits_type = typename std::basic_streambuf<Char>::traits_type;
26
27 buffer<Char>& buffer_;
28
29 public:
formatbuf(buffer<Char> & buf)30 formatbuf(buffer<Char>& buf) : buffer_(buf) {}
31
32 protected:
33 // The put-area is actually always empty. This makes the implementation
34 // simpler and has the advantage that the streambuf and the buffer are always
35 // in sync and sputc never writes into uninitialized memory. The obvious
36 // disadvantage is that each call to sputc always results in a (virtual) call
37 // to overflow. There is no disadvantage here for sputn since this always
38 // results in a call to xsputn.
39
40 int_type overflow(int_type ch = traits_type::eof()) FMT_OVERRIDE {
41 if (!traits_type::eq_int_type(ch, traits_type::eof()))
42 buffer_.push_back(static_cast<Char>(ch));
43 return ch;
44 }
45
xsputn(const Char * s,std::streamsize count)46 std::streamsize xsputn(const Char* s, std::streamsize count) FMT_OVERRIDE {
47 buffer_.append(s, s + count);
48 return count;
49 }
50 };
51
52 template <typename Char> struct test_stream : std::basic_ostream<Char> {
53 private:
54 // Hide all operator<< from std::basic_ostream<Char>.
55 void_t<> operator<<(null<>);
56 void_t<> operator<<(const Char*);
57
58 template <typename T, FMT_ENABLE_IF(std::is_convertible<T, int>::value &&
59 !std::is_enum<T>::value)>
60 void_t<> operator<<(T);
61 };
62
63 // Checks if T has a user-defined operator<< (e.g. not a member of
64 // std::ostream).
65 template <typename T, typename Char> class is_streamable {
66 private:
67 template <typename U>
68 static bool_constant<!std::is_same<decltype(std::declval<test_stream<Char>&>()
69 << std::declval<U>()),
70 void_t<>>::value>
71 test(int);
72
73 template <typename> static std::false_type test(...);
74
75 using result = decltype(test<T>(0));
76
77 public:
78 static const bool value = result::value;
79 };
80
81 // Write the content of buf to os.
82 template <typename Char>
write_buffer(std::basic_ostream<Char> & os,buffer<Char> & buf)83 void write_buffer(std::basic_ostream<Char>& os, buffer<Char>& buf) {
84 const Char* buf_data = buf.data();
85 using unsigned_streamsize = std::make_unsigned<std::streamsize>::type;
86 unsigned_streamsize size = buf.size();
87 unsigned_streamsize max_size = to_unsigned(max_value<std::streamsize>());
88 do {
89 unsigned_streamsize n = size <= max_size ? size : max_size;
90 os.write(buf_data, static_cast<std::streamsize>(n));
91 buf_data += n;
92 size -= n;
93 } while (size != 0);
94 }
95
96 template <typename Char, typename T>
97 void format_value(buffer<Char>& buf, const T& value,
98 locale_ref loc = locale_ref()) {
99 formatbuf<Char> format_buf(buf);
100 std::basic_ostream<Char> output(&format_buf);
101 #if !defined(FMT_STATIC_THOUSANDS_SEPARATOR)
102 if (loc) output.imbue(loc.get<std::locale>());
103 #endif
104 output << value;
105 output.exceptions(std::ios_base::failbit | std::ios_base::badbit);
106 buf.resize(buf.size());
107 }
108
109 // Formats an object of type T that has an overloaded ostream operator<<.
110 template <typename T, typename Char>
111 struct fallback_formatter<T, Char, enable_if_t<is_streamable<T, Char>::value>>
112 : private formatter<basic_string_view<Char>, Char> {
113 FMT_CONSTEXPR auto parse(basic_format_parse_context<Char>& ctx)
114 -> decltype(ctx.begin()) {
115 return formatter<basic_string_view<Char>, Char>::parse(ctx);
116 }
117 template <typename ParseCtx,
118 FMT_ENABLE_IF(std::is_same<
119 ParseCtx, basic_printf_parse_context<Char>>::value)>
120 auto parse(ParseCtx& ctx) -> decltype(ctx.begin()) {
121 return ctx.begin();
122 }
123
124 template <typename OutputIt>
125 auto format(const T& value, basic_format_context<OutputIt, Char>& ctx)
126 -> OutputIt {
127 basic_memory_buffer<Char> buffer;
128 format_value(buffer, value, ctx.locale());
129 basic_string_view<Char> str(buffer.data(), buffer.size());
130 return formatter<basic_string_view<Char>, Char>::format(str, ctx);
131 }
132 template <typename OutputIt>
133 auto format(const T& value, basic_printf_context<OutputIt, Char>& ctx)
134 -> OutputIt {
135 basic_memory_buffer<Char> buffer;
136 format_value(buffer, value, ctx.locale());
137 return std::copy(buffer.begin(), buffer.end(), ctx.out());
138 }
139 };
140 } // namespace detail
141
142 template <typename Char>
143 void vprint(std::basic_ostream<Char>& os, basic_string_view<Char> format_str,
144 basic_format_args<buffer_context<type_identity_t<Char>>> args) {
145 basic_memory_buffer<Char> buffer;
146 detail::vformat_to(buffer, format_str, args);
147 detail::write_buffer(os, buffer);
148 }
149
150 /**
151 \rst
152 Prints formatted data to the stream *os*.
153
154 **Example**::
155
156 fmt::print(cerr, "Don't {}!", "panic");
157 \endrst
158 */
159 template <typename S, typename... Args,
160 typename Char = enable_if_t<detail::is_string<S>::value, char_t<S>>>
161 void print(std::basic_ostream<Char>& os, const S& format_str, Args&&... args) {
162 vprint(os, to_string_view(format_str),
163 detail::make_args_checked<Args...>(format_str, args...));
164 }
165 FMT_END_NAMESPACE
166
167 #endif // FMT_OSTREAM_H_
168