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<<(unsigned char C) {
66 if (OutBufCur >= OutBufEnd)
72 raw_ostream &operator<<(signed char C) {
73 if (OutBufCur >= OutBufEnd)
79 raw_ostream &operator<<(const char *Str) {
80 return write(Str, strlen(Str));
83 raw_ostream &operator<<(const std::string& Str) {
84 return write(Str.data(), Str.length());
87 raw_ostream &operator<<(unsigned long N);
89 raw_ostream &operator<<(long N);
91 raw_ostream &operator<<(unsigned long long N);
93 raw_ostream &operator<<(long long N);
95 raw_ostream &operator<<(unsigned int N) {
96 return this->operator<<(static_cast<unsigned long>(N));
99 raw_ostream &operator<<(int N) {
100 return this->operator<<(static_cast<long>(N));
103 raw_ostream &operator<<(double N) {
104 return this->operator<<(ftostr(N));
108 raw_ostream &write(const char *Ptr, unsigned Size);
110 //===--------------------------------------------------------------------===//
111 // Subclass Interface
112 //===--------------------------------------------------------------------===//
116 /// flush_impl - The is the piece of the class that is implemented by
117 /// subclasses. This outputs the currently buffered data and resets the
119 virtual void flush_impl() = 0;
121 /// HandleFlush - A stream's implementation of flush should call this after
122 /// emitting the bytes to the data sink.
124 if (OutBufStart == 0)
126 OutBufCur = OutBufStart;
129 // An out of line virtual method to provide a home for the class vtable.
130 virtual void handle();
133 /// raw_fd_ostream - A raw_ostream that writes to a file descriptor.
135 class raw_fd_ostream : public raw_ostream {
139 /// raw_fd_ostream - Open the specified file for writing. If an error occurs,
140 /// information about the error is put into ErrorInfo, and the stream should
141 /// be immediately destroyed.
142 raw_fd_ostream(const char *Filename, std::string &ErrorInfo);
144 /// raw_fd_ostream ctor - FD is the file descriptor that this writes to. If
145 /// ShouldClose is true, this closes the file when
146 raw_fd_ostream(int fd, bool shouldClose) : FD(fd), ShouldClose(shouldClose) {}
150 /// flush_impl - The is the piece of the class that is implemented by
151 /// subclasses. This outputs the currently buffered data and resets the
153 virtual void flush_impl();
156 /// raw_stdout_ostream - This is a stream that always prints to stdout.
158 class raw_stdout_ostream : public raw_fd_ostream {
159 // An out of line virtual method to provide a home for the class vtable.
160 virtual void handle();
162 raw_stdout_ostream();
165 /// raw_stderr_ostream - This is a stream that always prints to stderr.
167 class raw_stderr_ostream : public raw_fd_ostream {
168 // An out of line virtual method to provide a home for the class vtable.
169 virtual void handle();
171 raw_stderr_ostream();
174 /// outs() - This returns a reference to a raw_ostream for standard output.
175 /// Use it like: outs() << "foo" << "bar";
178 /// errs() - This returns a reference to a raw_ostream for standard error.
179 /// Use it like: errs() << "foo" << "bar";
183 /// raw_os_ostream - A raw_ostream that writes to an std::ostream. This is a
184 /// simple adaptor class.
185 class raw_os_ostream : public raw_ostream {
188 raw_os_ostream(std::ostream &O) : OS(O) {}
190 /// flush_impl - The is the piece of the class that is implemented by
191 /// subclasses. This outputs the currently buffered data and resets the
193 virtual void flush_impl();
196 } // end llvm namespace