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
21 /// raw_ostream - This class implements an extremely fast bulk output stream
22 /// that can *only* output to a stream. It does not support seeking, reopening,
23 /// rewinding, line buffered disciplines etc. It is a simple buffer that outputs
24 /// a chunk at a time.
27 char *OutBufStart, *OutBufEnd, *OutBufCur;
30 // Start out ready to flush.
31 OutBufStart = OutBufEnd = OutBufCur = 0;
33 virtual ~raw_ostream() {}
35 //===--------------------------------------------------------------------===//
36 // Configuration Interface
37 //===--------------------------------------------------------------------===//
39 /// SetBufferSize - Set the internal buffer size to the specified amount
40 /// instead of the default.
41 void SetBufferSize(unsigned Size) {
43 "Buffer size must be somewhat large for invariants to hold");
46 delete [] OutBufStart;
47 OutBufStart = new char[Size];
48 OutBufEnd = OutBufStart+Size;
49 OutBufCur = OutBufStart;
52 //===--------------------------------------------------------------------===//
53 // Data Output Interface
54 //===--------------------------------------------------------------------===//
57 if (OutBufCur != OutBufStart)
61 raw_ostream &operator<<(char C) {
62 if (OutBufCur >= OutBufEnd)
68 raw_ostream &operator<<(const char *Str) {
69 return OutputData(Str, strlen(Str));
72 raw_ostream &OutputData(const char *Ptr, unsigned Size) {
73 if (OutBufCur+Size > OutBufEnd)
76 // Handle short strings specially, memcpy isn't very good at very short
79 // case 4: OutBufCur[3] = Ptr[3]; // FALL THROUGH
80 case 3: OutBufCur[2] = Ptr[2]; // FALL THROUGH
81 case 2: OutBufCur[1] = Ptr[1]; // FALL THROUGH
82 case 1: OutBufCur[0] = Ptr[0]; // FALL THROUGH
85 // Normally the string to emit is shorter than the buffer.
86 if (Size <= unsigned(OutBufEnd-OutBufStart)) {
87 memcpy(OutBufCur, Ptr, Size);
91 // If emitting a string larger than our buffer, emit in chunks. In this
92 // case we know that we just flushed the buffer.
94 unsigned NumToEmit = OutBufEnd-OutBufStart;
95 if (Size < NumToEmit) NumToEmit = Size;
96 assert(OutBufCur == OutBufStart);
97 memcpy(OutBufStart, Ptr, NumToEmit);
99 OutBufCur = OutBufStart + NumToEmit;
108 //===--------------------------------------------------------------------===//
109 // Subclass Interface
110 //===--------------------------------------------------------------------===//
114 /// flush_impl - The is the piece of the class that is implemented by
115 /// subclasses. This outputs the currently buffered data and resets the
117 virtual void flush_impl() = 0;
119 /// HandleFlush - A stream's implementation of flush should call this after
120 /// emitting the bytes to the data sink.
122 if (OutBufStart == 0)
124 OutBufCur = OutBufStart;
127 // An out of line virtual method to provide a home for the class vtable.
128 virtual void handle();
131 /// raw_fd_ostream - A raw_ostream that writes to a file descriptor.
133 class raw_fd_ostream : public raw_ostream {
137 /// raw_fd_ostream - Open the specified file for writing. If an error occurs,
138 /// information about the error is put into ErrorInfo, and the stream should
139 /// be immediately destroyed.
140 raw_fd_ostream(const char *Filename, std::string &ErrorInfo);
142 /// raw_fd_ostream ctor - FD is the file descriptor that this writes to. If
143 /// ShouldClose is true, this closes the file when
144 raw_fd_ostream(int fd, bool shouldClose) : FD(fd), ShouldClose(shouldClose) {}
148 /// flush_impl - The is the piece of the class that is implemented by
149 /// subclasses. This outputs the currently buffered data and resets the
151 virtual void flush_impl();
154 class raw_stdout_ostream : public raw_fd_ostream {
155 // An out of line virtual method to provide a home for the class vtable.
156 virtual void handle();
158 raw_stdout_ostream();
161 class raw_stderr_ostream : public raw_fd_ostream {
162 // An out of line virtual method to provide a home for the class vtable.
163 virtual void handle();
165 raw_stderr_ostream();
168 } // end llvm namespace