1 //===--- raw_ostream.h - Raw output stream ----------------------*- C++ -*-===//
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"
24 class format_object_base;
26 class SmallVectorImpl;
28 /// raw_ostream - This class implements an extremely fast bulk output stream
29 /// that can *only* output to a stream. It does not support seeking, reopening,
30 /// rewinding, line buffered disciplines etc. It is a simple buffer that outputs
31 /// a chunk at a time.
34 /// The buffer is handled in such a way that the buffer is
35 /// uninitialized, unbuffered, or out of space when OutBufCur >=
36 /// OutBufEnd. Thus a single comparison suffices to determine if we
37 /// need to take the slow path to write a single character.
39 /// The buffer is in one of three states:
40 /// 1. Unbuffered (Unbuffered == true)
41 /// 1. Uninitialized (Unbuffered == false && OutBufStart == 0).
42 /// 2. Buffered (Unbuffered == false && OutBufStart != 0 &&
43 /// OutBufEnd - OutBufStart >= 64).
44 char *OutBufStart, *OutBufEnd, *OutBufCur;
48 explicit raw_ostream(bool unbuffered=false) : Unbuffered(unbuffered) {
49 // Start out ready to flush.
50 OutBufStart = OutBufEnd = OutBufCur = 0;
53 virtual ~raw_ostream() {
54 delete [] OutBufStart;
57 //===--------------------------------------------------------------------===//
58 // Configuration Interface
59 //===--------------------------------------------------------------------===//
61 /// SetBufferSize - Set the internal buffer size to the specified amount
62 /// instead of the default.
63 void SetBufferSize(unsigned Size=4096) {
65 "Buffer size must be somewhat large for invariants to hold");
68 delete [] OutBufStart;
69 OutBufStart = new char[Size];
70 OutBufEnd = OutBufStart+Size;
71 OutBufCur = OutBufStart;
75 /// SetUnbuffered - Set the streams buffering status. When
76 /// unbuffered the stream will flush after every write. This routine
77 /// will also flush the buffer immediately when the stream is being
78 /// set to unbuffered.
79 void SetUnbuffered() {
82 delete [] OutBufStart;
83 OutBufStart = OutBufEnd = OutBufCur = 0;
87 unsigned GetNumBytesInBuffer() const {
88 return OutBufCur - OutBufStart;
91 //===--------------------------------------------------------------------===//
92 // Data Output Interface
93 //===--------------------------------------------------------------------===//
96 if (OutBufCur != OutBufStart)
100 raw_ostream &operator<<(char C) {
101 if (OutBufCur >= OutBufEnd)
107 raw_ostream &operator<<(unsigned char C) {
108 if (OutBufCur >= OutBufEnd)
114 raw_ostream &operator<<(signed char C) {
115 if (OutBufCur >= OutBufEnd)
121 raw_ostream &operator<<(const char *Str) {
122 return write(Str, strlen(Str));
125 raw_ostream &operator<<(const std::string& Str) {
126 return write(Str.data(), Str.length());
129 raw_ostream &operator<<(unsigned long N);
130 raw_ostream &operator<<(long N);
131 raw_ostream &operator<<(unsigned long long N);
132 raw_ostream &operator<<(long long N);
133 raw_ostream &operator<<(const void *P);
134 raw_ostream &operator<<(unsigned int N) {
135 return this->operator<<(static_cast<unsigned long>(N));
138 raw_ostream &operator<<(int N) {
139 return this->operator<<(static_cast<long>(N));
142 raw_ostream &operator<<(double N) {
143 return this->operator<<(ftostr(N));
146 raw_ostream &write(unsigned char C);
147 raw_ostream &write(const char *Ptr, unsigned Size);
149 // Formatted output, see the format() function in Support/Format.h.
150 raw_ostream &operator<<(const format_object_base &Fmt);
152 //===--------------------------------------------------------------------===//
153 // Subclass Interface
154 //===--------------------------------------------------------------------===//
157 /// write_impl - The is the piece of the class that is implemented
158 /// by subclasses. This writes the \args Size bytes starting at
159 /// \arg Ptr to the underlying stream.
161 /// \invariant { Size > 0 }
162 virtual void write_impl(const char *Ptr, unsigned Size) = 0;
164 // An out of line virtual method to provide a home for the class vtable.
165 virtual void handle();
167 //===--------------------------------------------------------------------===//
169 //===--------------------------------------------------------------------===//
171 /// flush_nonempty - Flush the current buffer, which is known to be
172 /// non-empty. This outputs the currently buffered data and resets
173 /// the buffer to empty.
174 void flush_nonempty();
177 //===----------------------------------------------------------------------===//
178 // File Output Streams
179 //===----------------------------------------------------------------------===//
181 /// raw_fd_ostream - A raw_ostream that writes to a file descriptor.
183 class raw_fd_ostream : public raw_ostream {
188 /// write_impl - See raw_ostream::write_impl.
189 virtual void write_impl(const char *Ptr, unsigned Size);
191 /// raw_fd_ostream - Open the specified file for writing. If an
192 /// error occurs, information about the error is put into ErrorInfo,
193 /// and the stream should be immediately destroyed; the string will
194 /// be empty if no error occurred.
196 /// \param Filename - The file to open. If this is "-" then the
197 /// stream will use stdout instead.
198 /// \param Binary - The file should be opened in binary mode on
199 /// platforms that support this distinction.
200 raw_fd_ostream(const char *Filename, bool Binary, std::string &ErrorInfo);
202 /// raw_fd_ostream ctor - FD is the file descriptor that this writes to. If
203 /// ShouldClose is true, this closes the file when the stream is destroyed.
204 raw_fd_ostream(int fd, bool shouldClose,
205 bool unbuffered=false) : raw_ostream(unbuffered), FD(fd),
206 ShouldClose(shouldClose) {}
210 /// close - Manually flush the stream and close the file.
213 /// tell - Return the current offset with the file.
214 uint64_t tell() { return pos + GetNumBytesInBuffer(); }
216 /// seek - Flushes the stream and repositions the underlying file descriptor
217 /// positition to the offset specified from the beginning of the file.
218 uint64_t seek(uint64_t off);
221 /// raw_stdout_ostream - This is a stream that always prints to stdout.
223 class raw_stdout_ostream : public raw_fd_ostream {
224 // An out of line virtual method to provide a home for the class vtable.
225 virtual void handle();
227 raw_stdout_ostream();
230 /// raw_stderr_ostream - This is a stream that always prints to stderr.
232 class raw_stderr_ostream : public raw_fd_ostream {
233 // An out of line virtual method to provide a home for the class vtable.
234 virtual void handle();
236 raw_stderr_ostream();
239 /// outs() - This returns a reference to a raw_ostream for standard output.
240 /// Use it like: outs() << "foo" << "bar";
243 /// errs() - This returns a reference to a raw_ostream for standard error.
244 /// Use it like: errs() << "foo" << "bar";
248 //===----------------------------------------------------------------------===//
249 // Output Stream Adaptors
250 //===----------------------------------------------------------------------===//
252 /// raw_os_ostream - A raw_ostream that writes to an std::ostream. This is a
253 /// simple adaptor class.
254 class raw_os_ostream : public raw_ostream {
257 /// write_impl - See raw_ostream::write_impl.
258 virtual void write_impl(const char *Ptr, unsigned Size);
260 raw_os_ostream(std::ostream &O) : OS(O) {}
264 /// raw_string_ostream - A raw_ostream that writes to an std::string. This is a
265 /// simple adaptor class.
266 class raw_string_ostream : public raw_ostream {
269 /// write_impl - See raw_ostream::write_impl.
270 virtual void write_impl(const char *Ptr, unsigned Size);
272 raw_string_ostream(std::string &O) : OS(O) {}
273 ~raw_string_ostream();
275 /// str - Flushes the stream contents to the target string and returns
276 /// the string's reference.
283 /// raw_svector_ostream - A raw_ostream that writes to an SmallVector or
284 /// SmallString. This is a simple adaptor class.
285 class raw_svector_ostream : public raw_ostream {
286 SmallVectorImpl<char> &OS;
288 /// write_impl - See raw_ostream::write_impl.
289 virtual void write_impl(const char *Ptr, unsigned Size);
291 raw_svector_ostream(SmallVectorImpl<char> &O) : OS(O) {}
292 ~raw_svector_ostream();
295 } // end llvm namespace