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
24 /// raw_ostream - This class implements an extremely fast bulk output stream
25 /// that can *only* output to a stream. It does not support seeking, reopening,
26 /// rewinding, line buffered disciplines etc. It is a simple buffer that outputs
27 /// a chunk at a time.
30 char *OutBufStart, *OutBufEnd, *OutBufCur;
33 // Start out ready to flush.
34 OutBufStart = OutBufEnd = OutBufCur = 0;
36 virtual ~raw_ostream() {}
38 //===--------------------------------------------------------------------===//
39 // Configuration Interface
40 //===--------------------------------------------------------------------===//
42 /// SetBufferSize - Set the internal buffer size to the specified amount
43 /// instead of the default.
44 void SetBufferSize(unsigned Size) {
46 "Buffer size must be somewhat large for invariants to hold");
49 delete [] OutBufStart;
50 OutBufStart = new char[Size];
51 OutBufEnd = OutBufStart+Size;
52 OutBufCur = OutBufStart;
55 //===--------------------------------------------------------------------===//
56 // Data Output Interface
57 //===--------------------------------------------------------------------===//
60 if (OutBufCur != OutBufStart)
64 raw_ostream &operator<<(char C) {
65 if (OutBufCur >= OutBufEnd)
71 raw_ostream &operator<<(const char *Str) {
72 return write(Str, strlen(Str));
75 raw_ostream &write(const char *Ptr, unsigned Size) {
76 if (OutBufCur+Size > OutBufEnd)
79 // Handle short strings specially, memcpy isn't very good at very short
82 case 4: OutBufCur[3] = Ptr[3]; // FALL THROUGH
83 case 3: OutBufCur[2] = Ptr[2]; // FALL THROUGH
84 case 2: OutBufCur[1] = Ptr[1]; // FALL THROUGH
85 case 1: OutBufCur[0] = Ptr[0]; // FALL THROUGH
88 // Normally the string to emit is shorter than the buffer.
89 if (Size <= unsigned(OutBufEnd-OutBufStart)) {
90 memcpy(OutBufCur, Ptr, Size);
94 // If emitting a string larger than our buffer, emit in chunks. In this
95 // case we know that we just flushed the buffer.
97 unsigned NumToEmit = OutBufEnd-OutBufStart;
98 if (Size < NumToEmit) NumToEmit = Size;
99 assert(OutBufCur == OutBufStart);
100 memcpy(OutBufStart, Ptr, NumToEmit);
102 OutBufCur = OutBufStart + NumToEmit;
111 //===--------------------------------------------------------------------===//
112 // Subclass Interface
113 //===--------------------------------------------------------------------===//
117 /// flush_impl - The is the piece of the class that is implemented by
118 /// subclasses. This outputs the currently buffered data and resets the
120 virtual void flush_impl() = 0;
122 /// HandleFlush - A stream's implementation of flush should call this after
123 /// emitting the bytes to the data sink.
125 if (OutBufStart == 0)
127 OutBufCur = OutBufStart;
130 // An out of line virtual method to provide a home for the class vtable.
131 virtual void handle();
134 /// raw_fd_ostream - A raw_ostream that writes to a file descriptor.
136 class raw_fd_ostream : public raw_ostream {
140 /// raw_fd_ostream - Open the specified file for writing. If an error occurs,
141 /// information about the error is put into ErrorInfo, and the stream should
142 /// be immediately destroyed.
143 raw_fd_ostream(const char *Filename, std::string &ErrorInfo);
145 /// raw_fd_ostream ctor - FD is the file descriptor that this writes to. If
146 /// ShouldClose is true, this closes the file when
147 raw_fd_ostream(int fd, bool shouldClose) : FD(fd), ShouldClose(shouldClose) {}
151 /// flush_impl - The is the piece of the class that is implemented by
152 /// subclasses. This outputs the currently buffered data and resets the
154 virtual void flush_impl();
157 /// raw_stdout_ostream - This is a stream that always prints to stdout.
159 class raw_stdout_ostream : public raw_fd_ostream {
160 // An out of line virtual method to provide a home for the class vtable.
161 virtual void handle();
163 raw_stdout_ostream();
166 /// raw_stderr_ostream - This is a stream that always prints to stderr.
168 class raw_stderr_ostream : public raw_fd_ostream {
169 // An out of line virtual method to provide a home for the class vtable.
170 virtual void handle();
172 raw_stderr_ostream();
175 /// outs() - This returns a reference to a raw_ostream for standard output.
176 /// Use it like: outs() << "foo" << "bar";
179 /// errs() - This returns a reference to a raw_ostream for standard error.
180 /// Use it like: errs() << "foo" << "bar";
184 /// raw_os_ostream - A raw_ostream that writes to an std::ostream. This is a
185 /// simple adaptor class.
186 class raw_os_ostream : public raw_ostream {
189 raw_os_ostream(std::ostream &O) : OS(O) {}
191 /// flush_impl - The is the piece of the class that is implemented by
192 /// subclasses. This outputs the currently buffered data and resets the
194 virtual void flush_impl();
197 } // end llvm namespace