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
22 /// raw_ostream - This class implements an extremely fast bulk output stream
23 /// that can *only* output to a stream. It does not support seeking, reopening,
24 /// rewinding, line buffered disciplines etc. It is a simple buffer that outputs
25 /// a chunk at a time.
28 char *OutBufStart, *OutBufEnd, *OutBufCur;
31 // Start out ready to flush.
32 OutBufStart = OutBufEnd = OutBufCur = 0;
34 virtual ~raw_ostream() {}
36 //===--------------------------------------------------------------------===//
37 // Configuration Interface
38 //===--------------------------------------------------------------------===//
40 /// SetBufferSize - Set the internal buffer size to the specified amount
41 /// instead of the default.
42 void SetBufferSize(unsigned Size) {
44 "Buffer size must be somewhat large for invariants to hold");
47 delete [] OutBufStart;
48 OutBufStart = new char[Size];
49 OutBufEnd = OutBufStart+Size;
50 OutBufCur = OutBufStart;
53 //===--------------------------------------------------------------------===//
54 // Data Output Interface
55 //===--------------------------------------------------------------------===//
58 if (OutBufCur != OutBufStart)
62 raw_ostream &operator<<(char C) {
63 if (OutBufCur >= OutBufEnd)
69 raw_ostream &operator<<(const char *Str) {
70 return write(Str, strlen(Str));
73 raw_ostream &write(const char *Ptr, unsigned Size) {
74 if (OutBufCur+Size > OutBufEnd)
77 // Handle short strings specially, memcpy isn't very good at very short
80 case 4: OutBufCur[3] = Ptr[3]; // FALL THROUGH
81 case 3: OutBufCur[2] = Ptr[2]; // FALL THROUGH
82 case 2: OutBufCur[1] = Ptr[1]; // FALL THROUGH
83 case 1: OutBufCur[0] = Ptr[0]; // FALL THROUGH
86 // Normally the string to emit is shorter than the buffer.
87 if (Size <= unsigned(OutBufEnd-OutBufStart)) {
88 memcpy(OutBufCur, Ptr, Size);
92 // If emitting a string larger than our buffer, emit in chunks. In this
93 // case we know that we just flushed the buffer.
95 unsigned NumToEmit = OutBufEnd-OutBufStart;
96 if (Size < NumToEmit) NumToEmit = Size;
97 assert(OutBufCur == OutBufStart);
98 memcpy(OutBufStart, Ptr, NumToEmit);
100 OutBufCur = OutBufStart + NumToEmit;
109 //===--------------------------------------------------------------------===//
110 // Subclass Interface
111 //===--------------------------------------------------------------------===//
115 /// flush_impl - The is the piece of the class that is implemented by
116 /// subclasses. This outputs the currently buffered data and resets the
118 virtual void flush_impl() = 0;
120 /// HandleFlush - A stream's implementation of flush should call this after
121 /// emitting the bytes to the data sink.
123 if (OutBufStart == 0)
125 OutBufCur = OutBufStart;
128 // An out of line virtual method to provide a home for the class vtable.
129 virtual void handle();
132 /// raw_fd_ostream - A raw_ostream that writes to a file descriptor.
134 class raw_fd_ostream : public raw_ostream {
138 /// raw_fd_ostream - Open the specified file for writing. If an error occurs,
139 /// information about the error is put into ErrorInfo, and the stream should
140 /// be immediately destroyed.
141 raw_fd_ostream(const char *Filename, std::string &ErrorInfo);
143 /// raw_fd_ostream ctor - FD is the file descriptor that this writes to. If
144 /// ShouldClose is true, this closes the file when
145 raw_fd_ostream(int fd, bool shouldClose) : FD(fd), ShouldClose(shouldClose) {}
149 /// flush_impl - The is the piece of the class that is implemented by
150 /// subclasses. This outputs the currently buffered data and resets the
152 virtual void flush_impl();
155 /// raw_stdout_ostream - This is a stream that always prints to stdout.
157 class raw_stdout_ostream : public raw_fd_ostream {
158 // An out of line virtual method to provide a home for the class vtable.
159 virtual void handle();
161 raw_stdout_ostream();
164 /// raw_stderr_ostream - This is a stream that always prints to stderr.
166 class raw_stderr_ostream : public raw_fd_ostream {
167 // An out of line virtual method to provide a home for the class vtable.
168 virtual void handle();
170 raw_stderr_ostream();
173 /// outs() - This returns a reference to a raw_ostream for standard output.
174 /// Use it like: outs() << "foo" << "bar";
177 /// errs() - This returns a reference to a raw_ostream for standard error.
178 /// Use it like: errs() << "foo" << "bar";
182 /// raw_os_ostream - A raw_ostream that writes to an std::ostream. This is a
183 /// simple adaptor class.
184 class raw_os_ostream : public raw_ostream {
187 raw_os_ostream(std::ostream &O) : OS(O) {}
189 /// flush_impl - The is the piece of the class that is implemented by
190 /// subclasses. This outputs the currently buffered data and resets the
192 virtual void flush_impl();
195 } // end llvm namespace