DIY  3.0
data-parallel out-of-core C++ library
 All Classes Namespaces Functions Typedefs Groups Pages
ostream.h
1 /*
2  Formatting library for C++ - std::ostream support
3 
4  Copyright (c) 2012 - 2016, Victor Zverovich
5  All rights reserved.
6 
7  For the license information refer to format.h.
8  */
9 
10 #ifndef FMT_OSTREAM_H_
11 #define FMT_OSTREAM_H_
12 
13 #include "format.h"
14 #include <ostream>
15 
16 namespace fmt {
17 
18 namespace internal {
19 
20 template <class Char>
21 class FormatBuf : public std::basic_streambuf<Char> {
22  private:
23  typedef typename std::basic_streambuf<Char>::int_type int_type;
24  typedef typename std::basic_streambuf<Char>::traits_type traits_type;
25 
26  Buffer<Char> &buffer_;
27 
28  public:
29  FormatBuf(Buffer<Char> &buffer) : buffer_(buffer) {}
30 
31  protected:
32  // The put-area is actually always empty. This makes the implementation
33  // simpler and has the advantage that the streambuf and the buffer are always
34  // in sync and sputc never writes into uninitialized memory. The obvious
35  // disadvantage is that each call to sputc always results in a (virtual) call
36  // to overflow. There is no disadvantage here for sputn since this always
37  // results in a call to xsputn.
38 
39  int_type overflow(int_type ch = traits_type::eof()) FMT_OVERRIDE {
40  if (!traits_type::eq_int_type(ch, traits_type::eof()))
41  buffer_.push_back(static_cast<Char>(ch));
42  return ch;
43  }
44 
45  std::streamsize xsputn(const Char *s, std::streamsize count) FMT_OVERRIDE {
46  buffer_.append(s, s + count);
47  return count;
48  }
49 };
50 
51 Yes &convert(std::ostream &);
52 
53 struct DummyStream : std::ostream {
54  DummyStream(); // Suppress a bogus warning in MSVC.
55  // Hide all operator<< overloads from std::ostream.
56  void operator<<(Null<>);
57 };
58 
59 No &operator<<(std::ostream &, int);
60 
61 template<typename T>
62 struct ConvertToIntImpl<T, true> {
63  // Convert to int only if T doesn't have an overloaded operator<<.
64  enum {
65  value = sizeof(convert(get<DummyStream>() << get<T>())) == sizeof(No)
66  };
67 };
68 
69 // Write the content of w to os.
70 FMT_API void write(std::ostream &os, Writer &w);
71 } // namespace internal
72 
73 // Formats a value.
74 template <typename Char, typename ArgFormatter_, typename T>
75 void format_arg(BasicFormatter<Char, ArgFormatter_> &f,
76  const Char *&format_str, const T &value) {
78 
79  internal::FormatBuf<Char> format_buf(buffer);
80  std::basic_ostream<Char> output(&format_buf);
81  output << value;
82 
83  BasicStringRef<Char> str(&buffer[0], buffer.size());
85  format_str = f.format(format_str, MakeArg(str));
86 }
87 
97 FMT_API void print(std::ostream &os, CStringRef format_str, ArgList args);
98 FMT_VARIADIC(void, print, std::ostream &, CStringRef)
99 } // namespace fmt
100 
101 #ifdef FMT_HEADER_ONLY
102 # include "ostream.cc"
103 #endif
104 
105 #endif // FMT_OSTREAM_H_
Definition: format.h:1161
Definition: format.h:1420
void format(BasicCStringRef< Char > format_str)
Definition: format.h:3853
Definition: ostream.h:21
Definition: format.h:454
Definition: ostream.h:53
Definition: format.h:744
void append(const U *begin, const U *end)
Definition: format.h:729
Definition: format.h:482
std::size_t size() const
Definition: format.h:687
Definition: format.h:439