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 "format.h"
12 #include <ostream>
13 
15 namespace internal {
16 
17 template <class Char>
18 class formatbuf : public std::basic_streambuf<Char> {
19  private:
21  typedef typename std::basic_streambuf<Char>::traits_type traits_type;
22 
24 
25  public:
26  formatbuf(basic_buffer<Char> &buffer) : buffer_(buffer) {}
27 
28  protected:
29  // The put-area is actually always empty. This makes the implementation
30  // simpler and has the advantage that the streambuf and the buffer are always
31  // in sync and sputc never writes into uninitialized memory. The obvious
32  // disadvantage is that each call to sputc always results in a (virtual) call
33  // to overflow. There is no disadvantage here for sputn since this always
34  // results in a call to xsputn.
35 
36  int_type overflow(int_type ch = traits_type::eof()) FMT_OVERRIDE {
37  if (!traits_type::eq_int_type(ch, traits_type::eof()))
38  buffer_.push_back(static_cast<Char>(ch));
39  return ch;
40  }
41 
42  std::streamsize xsputn(const Char *s, std::streamsize count) FMT_OVERRIDE {
43  buffer_.append(s, s + count);
44  return count;
45  }
46 };
47 
48 template <typename Char>
49 struct test_stream : std::basic_ostream<Char> {
50  private:
51  struct null;
52  // Hide all operator<< from std::basic_ostream<Char>.
53  void operator<<(null);
54 };
55 
56 // Checks if T has a user-defined operator<< (e.g. not a member of std::ostream).
57 template <typename T, typename Char>
59  private:
60  template <typename U>
61  static decltype(
63  << internal::declval<U>(), std::true_type()) test(int);
64 
65  template <typename>
66  static std::false_type test(...);
67 
68  typedef decltype(test<T>(0)) result;
69 
70  public:
71  static const bool value = result::value;
72 };
73 
74 // Write the content of buf to os.
75 template <typename Char>
76 void write(std::basic_ostream<Char> &os, basic_buffer<Char> &buf) {
77  const Char *data = buf.data();
78  typedef std::make_unsigned<std::streamsize>::type UnsignedStreamSize;
79  UnsignedStreamSize size = buf.size();
80  UnsignedStreamSize max_size =
82  do {
83  UnsignedStreamSize n = size <= max_size ? size : max_size;
84  os.write(data, static_cast<std::streamsize>(n));
85  data += n;
86  size -= n;
87  } while (size != 0);
88 }
89 
90 template <typename Char, typename T>
92  internal::formatbuf<Char> format_buf(buffer);
93  std::basic_ostream<Char> output(&format_buf);
94  output.exceptions(std::ios_base::failbit | std::ios_base::badbit);
95  output << value;
96  buffer.resize(buffer.size());
97 }
98 } // namespace internal
99 
100 // Disable conversion to int if T has an overloaded operator<< which is a free
101 // function (not a member of std::ostream).
102 template <typename T, typename Char>
103 struct convert_to_int<T, Char, void> {
104  static const bool value =
107 };
108 
109 // Formats an object of type T that has an overloaded ostream operator<<.
110 template <typename T, typename Char>
111 struct formatter<T, Char,
112  typename std::enable_if<
113  internal::is_streamable<T, Char>::value &&
114  !internal::format_type<
115  typename buffer_context<Char>::type, T>::value>::type>
116  : formatter<basic_string_view<Char>, Char> {
117 
118  template <typename Context>
119  auto format(const T &value, Context &ctx) -> decltype(ctx.out()) {
121  internal::format_value(buffer, value);
122  basic_string_view<Char> str(buffer.data(), buffer.size());
124  }
125 };
126 
127 template <typename Char>
128 inline void vprint(std::basic_ostream<Char> &os,
132  internal::vformat_to(buffer, format_str, args);
133  internal::write(os, buffer);
134 }
135 /**
136  \rst
137  Prints formatted data to the stream *os*.
138 
139  **Example**::
140 
141  fmt::print(cerr, "Don't {}!", "panic");
142  \endrst
143  */
144 template <typename S, typename... Args>
146 print(std::basic_ostream<FMT_CHAR(S)> &os, const S &format_str,
147  const Args & ... args) {
148  internal::checked_args<S, Args...> ca(format_str, args...);
149  vprint(os, to_string_view(format_str), *ca);
150 }
152 
153 #endif // FMT_OSTREAM_H_
static QCString result
void append(const U *begin, const U *end)
Definition: format.h:394
Definition: test.py:1
std::add_rvalue_reference< T >::type declval() FMT_NOEXCEPT
const S & format_str
Definition: format.h:3342
Definition: 044_section.h:5
STL namespace.
void resize(std::size_t new_size)
Definition: core.h:264
void write(std::basic_ostream< Char > &os, basic_buffer< Char > &buf)
Definition: ostream.h:76
static QCString args
Definition: declinfo.cpp:674
#define FMT_END_NAMESPACE
Definition: core.h:153
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:87
T * data() FMT_NOEXCEPT
Definition: core.h:256
buffer_context< Char >::type::iterator vformat_to(internal::basic_buffer< Char > &buf, basic_string_view< Char > format_str, basic_format_args< typename buffer_context< Char >::type > args)
Definition: format.h:3233
dummy_string_view to_string_view(...)
void vprint(std::basic_ostream< Char > &os, basic_string_view< Char > format_str, basic_format_args< typename buffer_context< Char >::type > args)
Definition: ostream.h:128
typedef FMT_CHAR(S) Char
std::size_t size() const FMT_NOEXCEPT
Definition: core.h:250
std::basic_streambuf< Char >::traits_type traits_type
Definition: ostream.h:21
void push_back(const T &value)
Definition: core.h:278
std::string str(const std::pair< Type, Type > &tt)
Definition: test_pimpos.cxx:12
std::ostream & operator<<(std::ostream &os, Analyzer::Table< T > const &t)
Definition: Analyzer.h:134
static int max(int a, int b)
basic_buffer< char > buffer
Definition: core.h:291
std::streamsize xsputn(const Char *s, std::streamsize count) FMT_OVERRIDE
Definition: ostream.h:42
std::basic_streambuf< Char >::int_type int_type
Definition: ostream.h:20
basic_buffer< Char > & buffer_
Definition: ostream.h:23
FMT_CONSTEXPR std::make_unsigned< Int >::type to_unsigned(Int value)
Definition: core.h:208
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1225
int_type overflow(int_type ch=traits_type::eof()) FMT_OVERRIDE
Definition: ostream.h:36
static QCString type
Definition: declinfo.cpp:672
void format_value(basic_buffer< Char > &buffer, const T &value)
Definition: ostream.h:91
formatbuf(basic_buffer< Char > &buffer)
Definition: ostream.h:26
#define FMT_OVERRIDE
Definition: core.h:88
std::size_t n
Definition: format.h:3399
static QCString * s
Definition: config.cpp:1042
std::enable_if< internal::is_string< S >::value >::type print(std::basic_ostream< FMT_CHAR(S)> &os, const S &format_str, const Args &...args)
Definition: ostream.h:146