• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // commented out by spdlog
14 //#include "format.h"
15 #include <ostream>
16 
17 namespace fmt
18 {
19 
20 namespace internal
21 {
22 
23 template <class Char>
24 class FormatBuf : public std::basic_streambuf<Char>
25 {
26 private:
27     typedef typename std::basic_streambuf<Char>::int_type int_type;
28     typedef typename std::basic_streambuf<Char>::traits_type traits_type;
29 
30     Buffer<Char> &buffer_;
31     Char *start_;
32 
33 public:
FormatBuf(Buffer<Char> & buffer)34     FormatBuf(Buffer<Char> &buffer) : buffer_(buffer), start_(&buffer[0])
35     {
36         this->setp(start_, start_ + buffer_.capacity());
37     }
38 
39     int_type overflow(int_type ch = traits_type::eof())
40     {
41         if (!traits_type::eq_int_type(ch, traits_type::eof()))
42         {
43             size_t buf_size = size();
44             buffer_.resize(buf_size);
45             buffer_.reserve(buf_size * 2);
46 
47             start_ = &buffer_[0];
48             start_[buf_size] = traits_type::to_char_type(ch);
49             this->setp(start_ + buf_size + 1, start_ + buf_size * 2);
50         }
51         return ch;
52     }
53 
size()54     size_t size() const
55     {
56         return to_unsigned(this->pptr() - start_);
57     }
58 };
59 
60 Yes &convert(std::ostream &);
61 
62 struct DummyStream : std::ostream
63 {
64     DummyStream();  // Suppress a bogus warning in MSVC.
65     // Hide all operator<< overloads from std::ostream.
66     void operator<<(Null<>);
67 };
68 
69 No &operator<<(std::ostream &, int);
70 
71 template<typename T>
72 struct ConvertToIntImpl<T, true>
73 {
74     // Convert to int only if T doesn't have an overloaded operator<<.
75     enum
76     {
77         value = sizeof(convert(get<DummyStream>() << get<T>())) == sizeof(No)
78     };
79 };
80 
81 // Write the content of w to os.
82 void write(std::ostream &os, Writer &w);
83 }  // namespace internal
84 
85 // Formats a value.
86 template <typename Char, typename ArgFormatter, typename T>
87 void format_arg(BasicFormatter<Char, ArgFormatter> &f,
88                 const Char *&format_str, const T &value)
89 {
90     internal::MemoryBuffer<Char, internal::INLINE_BUFFER_SIZE> buffer;
91 
92     internal::FormatBuf<Char> format_buf(buffer);
93     std::basic_ostream<Char> output(&format_buf);
94     output << value;
95 
96     BasicStringRef<Char> str(&buffer[0], format_buf.size());
97     typedef internal::MakeArg< BasicFormatter<Char> > MakeArg;
98     format_str = f.format(format_str, MakeArg(str));
99 }
100 
101 /**
102 \rst
103 Prints formatted data to the stream *os*.
104 
105 **Example**::
106 
107 print(cerr, "Don't {}!", "panic");
108 \endrst
109 */
110 FMT_API void print(std::ostream &os, CStringRef format_str, ArgList args);
111 FMT_VARIADIC(void, print, std::ostream &, CStringRef)
112 }  // namespace fmt
113 
114 #ifdef FMT_HEADER_ONLY
115 # include "ostream.cc"
116 #endif
117 
118 #endif  // FMT_OSTREAM_H_