bitz-server  2.0.1
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 {
23 private:
24  typedef typename std::basic_streambuf<Char>::int_type int_type;
25  typedef typename std::basic_streambuf<Char>::traits_type traits_type;
26 
27  Buffer<Char> &buffer_;
28 
29 public:
30  FormatBuf(Buffer<Char> &buffer)
31  : buffer_(buffer)
32  {
33  }
34 
35 protected:
36  // The put-area is actually always empty. This makes the implementation
37  // simpler and has the advantage that the streambuf and the buffer are always
38  // in sync and sputc never writes into uninitialized memory. The obvious
39  // disadvantage is that each call to sputc always results in a (virtual) call
40  // to overflow. There is no disadvantage here for sputn since this always
41  // results in a call to xsputn.
42 
43  int_type overflow(int_type ch = traits_type::eof()) FMT_OVERRIDE
44  {
45  if (!traits_type::eq_int_type(ch, traits_type::eof()))
46  buffer_.push_back(static_cast<Char>(ch));
47  return ch;
48  }
49 
50  std::streamsize xsputn(const Char *s, std::streamsize count) FMT_OVERRIDE
51  {
52  buffer_.append(s, s + count);
53  return count;
54  }
55 };
56 
57 Yes &convert(std::ostream &);
58 
59 struct DummyStream : std::ostream
60 {
61  DummyStream(); // Suppress a bogus warning in MSVC.
62 
63  // Hide all operator<< overloads from std::ostream.
64  template<typename T>
65  typename EnableIf<sizeof(T) == 0>::type operator<<(const T &);
66 };
67 
68 No &operator<<(std::ostream &, int);
69 
70 template<typename T>
71 struct ConvertToIntImpl<T, true>
72 {
73  // Convert to int only if T doesn't have an overloaded operator<<.
74  enum
75  {
76  value = sizeof(convert(get<DummyStream>() << get<T>())) == sizeof(No)
77  };
78 };
79 
80 // Write the content of w to os.
81 FMT_API void write(std::ostream &os, Writer &w);
82 } // namespace internal
83 
84 // Formats a value.
85 template<typename Char, typename ArgFormatter_, typename T>
86 void format_arg(BasicFormatter<Char, ArgFormatter_> &f, const Char *&format_str, const T &value)
87 {
89 
90  internal::FormatBuf<Char> format_buf(buffer);
91  std::basic_ostream<Char> output(&format_buf);
92  output.exceptions(std::ios_base::failbit | std::ios_base::badbit);
93  output << value;
94 
95  BasicStringRef<Char> str(&buffer[0], buffer.size());
97  format_str = f.format(format_str, MakeArg(str));
98 }
99 
109 FMT_API void print(std::ostream &os, CStringRef format_str, ArgList args);
110 FMT_VARIADIC(void, print, std::ostream &, CStringRef)
111 } // namespace fmt
112 
113 #ifdef FMT_HEADER_ONLY
114 #include "ostream.cc"
115 #endif
116 
117 #endif // FMT_OSTREAM_H_
Definition: format.h:1450
Definition: format.h:1876
Definition: format.h:705
Definition: format.h:1819
void format(BasicCStringRef< Char > format_str)
Definition: format.h:4738
Definition: ostream.h:21
Definition: format.h:529
Definition: format.h:1506
std::size_t size() const
Definition: format.h:842
Definition: ostream.h:59
Definition: format.h:917
void append(const U *begin, const U *end)
Definition: format.h:902
Definition: format.h:557
Definition: format.cc:84
Definition: format.h:515