llvm.org GIT mirror llvm / 07f51f7
add support for a cout/cerr analog (outs()/errs()) as well as a simple adaptor class to give raw output capabilities to something that wants to write to an ostream. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@54865 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 11 years ago
2 changed file(s) with 59 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
None //===--- raw_ostream.h - Raw output stream ---------------------------------===//
0 //===--- raw_ostream.h - Raw output stream --------------------------------===//
11 //
22 // The LLVM Compiler Infrastructure
33 //
1414 #define LLVM_SUPPORT_RAW_OSTREAM_H
1515
1616 #include
17 #include
1718
1819 namespace llvm {
1920
150151 virtual void flush_impl();
151152 };
152153
154 /// raw_stdout_ostream - This is a stream that always prints to stdout.
155 ///
153156 class raw_stdout_ostream : public raw_fd_ostream {
154157 // An out of line virtual method to provide a home for the class vtable.
155158 virtual void handle();
157160 raw_stdout_ostream();
158161 };
159162
163 /// raw_stderr_ostream - This is a stream that always prints to stderr.
164 ///
160165 class raw_stderr_ostream : public raw_fd_ostream {
161166 // An out of line virtual method to provide a home for the class vtable.
162167 virtual void handle();
164169 raw_stderr_ostream();
165170 };
166171
172 /// outs() - This returns a reference to a raw_ostream for standard output.
173 /// Use it like: outs() << "foo" << "bar";
174 raw_ostream &outs();
175
176 /// errs() - This returns a reference to a raw_ostream for standard error.
177 /// Use it like: errs() << "foo" << "bar";
178 raw_ostream &errs();
179
180
181 /// raw_os_ostream - A raw_ostream that writes to an std::ostream. This is a
182 /// simple adaptor class.
183 class raw_os_ostream : public raw_ostream {
184 std::ostream &OS;
185 public:
186 raw_os_ostream(std::ostream &O) : OS(O) {}
187
188 /// flush_impl - The is the piece of the class that is implemented by
189 /// subclasses. This outputs the currently buffered data and resets the
190 /// buffer to empty.
191 virtual void flush_impl();
192 };
193
167194 } // end llvm namespace
168195
169196 #endif
1111 //===----------------------------------------------------------------------===//
1212
1313 #include "llvm/Support/raw_ostream.h"
14 #include
1415 using namespace llvm;
1516
1617 #if !defined(_MSC_VER)
6162 HandleFlush();
6263 }
6364
65 //===----------------------------------------------------------------------===//
66 // raw_stdout/err_ostream
67 //===----------------------------------------------------------------------===//
6468
6569 raw_stdout_ostream::raw_stdout_ostream():raw_fd_ostream(STDOUT_FILENO, false) {}
6670 raw_stderr_ostream::raw_stderr_ostream():raw_fd_ostream(STDERR_FILENO, false) {}
6872 // An out of line virtual method to provide a home for the class vtable.
6973 void raw_stdout_ostream::handle() {}
7074 void raw_stderr_ostream::handle() {}
75
76 /// outs() - This returns a reference to a raw_ostream for standard output.
77 /// Use it like: outs() << "foo" << "bar";
78 raw_ostream &outs() {
79 static raw_stdout_ostream S;
80 return S;
81 }
82
83 /// errs() - This returns a reference to a raw_ostream for standard error.
84 /// Use it like: errs() << "foo" << "bar";
85 raw_ostream &errs() {
86 static raw_stderr_ostream S;
87 return S;
88 }
89
90 //===----------------------------------------------------------------------===//
91 // raw_os_ostream
92 //===----------------------------------------------------------------------===//
93
94 /// flush_impl - The is the piece of the class that is implemented by
95 /// subclasses. This outputs the currently buffered data and resets the
96 /// buffer to empty.
97 void raw_os_ostream::flush_impl() {
98 if (OutBufCur-OutBufStart)
99 OS.write(OutBufStart, OutBufCur-OutBufStart);
100 HandleFlush();
101 }