1 //===--- raw_ostream.h - Raw output stream --------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the raw_ostream class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_SUPPORT_RAW_OSTREAM_H
15 #define LLVM_SUPPORT_RAW_OSTREAM_H
17 #include "llvm/ADT/StringExtras.h"
25 /// raw_ostream - This class implements an extremely fast bulk output stream
26 /// that can *only* output to a stream. It does not support seeking, reopening,
27 /// rewinding, line buffered disciplines etc. It is a simple buffer that outputs
28 /// a chunk at a time.
31 char *OutBufStart, *OutBufEnd, *OutBufCur;
34 // Start out ready to flush.
35 OutBufStart = OutBufEnd = OutBufCur = 0;
37 virtual ~raw_ostream() {}
39 //===--------------------------------------------------------------------===//
40 // Configuration Interface
41 //===--------------------------------------------------------------------===//
43 /// SetBufferSize - Set the internal buffer size to the specified amount
44 /// instead of the default.
45 void SetBufferSize(unsigned Size) {
47 "Buffer size must be somewhat large for invariants to hold");
50 delete [] OutBufStart;
51 OutBufStart = new char[Size];
52 OutBufEnd = OutBufStart+Size;
53 OutBufCur = OutBufStart;
56 //===--------------------------------------------------------------------===//
57 // Data Output Interface
58 //===--------------------------------------------------------------------===//
61 if (OutBufCur != OutBufStart)
65 raw_ostream &operator<<(char C) {
66 if (OutBufCur >= OutBufEnd)
72 raw_ostream &operator<<(const char *Str) {
73 return write(Str, strlen(Str));
76 raw_ostream &operator<<(const std::string& Str) {
77 return write(Str.data(), Str.length());
80 raw_ostream &operator<<(unsigned long N);
82 raw_ostream &operator<<(long N);
84 raw_ostream &operator<<(unsigned long long N);
86 raw_ostream &operator<<(long long N);
88 raw_ostream &operator<<(unsigned int N) {
89 return this->operator<<(static_cast<unsigned long>(N));
92 raw_ostream &operator<<(int N) {
93 return this->operator<<(static_cast<long>(N));
96 raw_ostream &operator<<(double N) {
97 return this->operator<<(ftostr(N));
101 raw_ostream &write(const char *Ptr, unsigned Size);
103 //===--------------------------------------------------------------------===//
104 // Subclass Interface
105 //===--------------------------------------------------------------------===//
109 /// flush_impl - The is the piece of the class that is implemented by
110 /// subclasses. This outputs the currently buffered data and resets the
112 virtual void flush_impl() = 0;
114 /// HandleFlush - A stream's implementation of flush should call this after
115 /// emitting the bytes to the data sink.
117 if (OutBufStart == 0)
119 OutBufCur = OutBufStart;
122 // An out of line virtual method to provide a home for the class vtable.
123 virtual void handle();
126 /// raw_fd_ostream - A raw_ostream that writes to a file descriptor.
128 class raw_fd_ostream : public raw_ostream {
132 /// raw_fd_ostream - Open the specified file for writing. If an error occurs,
133 /// information about the error is put into ErrorInfo, and the stream should
134 /// be immediately destroyed.
135 raw_fd_ostream(const char *Filename, std::string &ErrorInfo);
137 /// raw_fd_ostream ctor - FD is the file descriptor that this writes to. If
138 /// ShouldClose is true, this closes the file when
139 raw_fd_ostream(int fd, bool shouldClose) : FD(fd), ShouldClose(shouldClose) {}
143 /// flush_impl - The is the piece of the class that is implemented by
144 /// subclasses. This outputs the currently buffered data and resets the
146 virtual void flush_impl();
149 /// raw_stdout_ostream - This is a stream that always prints to stdout.
151 class raw_stdout_ostream : public raw_fd_ostream {
152 // An out of line virtual method to provide a home for the class vtable.
153 virtual void handle();
155 raw_stdout_ostream();
158 /// raw_stderr_ostream - This is a stream that always prints to stderr.
160 class raw_stderr_ostream : public raw_fd_ostream {
161 // An out of line virtual method to provide a home for the class vtable.
162 virtual void handle();
164 raw_stderr_ostream();
167 /// outs() - This returns a reference to a raw_ostream for standard output.
168 /// Use it like: outs() << "foo" << "bar";
171 /// errs() - This returns a reference to a raw_ostream for standard error.
172 /// Use it like: errs() << "foo" << "bar";
176 /// raw_os_ostream - A raw_ostream that writes to an std::ostream. This is a
177 /// simple adaptor class.
178 class raw_os_ostream : public raw_ostream {
181 raw_os_ostream(std::ostream &O) : OS(O) {}
183 /// flush_impl - The is the piece of the class that is implemented by
184 /// subclasses. This outputs the currently buffered data and resets the
186 virtual void flush_impl();
189 } // end llvm namespace