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 write(Str, strlen(Str));
126 raw_ostream &operator<<(const std::string& Str) {
127 write(Str.data(), Str.length());
131 raw_ostream &operator<<(unsigned long N);
132 raw_ostream &operator<<(long N);
133 raw_ostream &operator<<(unsigned long long N);
134 raw_ostream &operator<<(long long N);
135 raw_ostream &operator<<(const void *P);
136 raw_ostream &operator<<(unsigned int N) {
137 this->operator<<(static_cast<unsigned long>(N));
141 raw_ostream &operator<<(int N) {
142 this->operator<<(static_cast<long>(N));
146 raw_ostream &operator<<(double N) {
147 this->operator<<(ftostr(N));
151 raw_ostream &write(unsigned char C);
152 raw_ostream &write(const char *Ptr, unsigned Size);
154 // Formatted output, see the format() function in Support/Format.h.
155 raw_ostream &operator<<(const format_object_base &Fmt);
157 //===--------------------------------------------------------------------===//
158 // Subclass Interface
159 //===--------------------------------------------------------------------===//
162 /// write_impl - The is the piece of the class that is implemented
163 /// by subclasses. This writes the \args Size bytes starting at
164 /// \arg Ptr to the underlying stream.
166 /// \invariant { Size > 0 }
167 virtual void write_impl(const char *Ptr, unsigned Size) = 0;
169 // An out of line virtual method to provide a home for the class vtable.
170 virtual void handle();
172 //===--------------------------------------------------------------------===//
174 //===--------------------------------------------------------------------===//
176 /// flush_nonempty - Flush the current buffer, which is known to be
177 /// non-empty. This outputs the currently buffered data and resets
178 /// the buffer to empty.
179 void flush_nonempty();
182 //===----------------------------------------------------------------------===//
183 // File Output Streams
184 //===----------------------------------------------------------------------===//
186 /// raw_fd_ostream - A raw_ostream that writes to a file descriptor.
188 class raw_fd_ostream : public raw_ostream {
193 /// write_impl - See raw_ostream::write_impl.
194 virtual void write_impl(const char *Ptr, unsigned Size);
196 /// raw_fd_ostream - Open the specified file for writing. If an
197 /// error occurs, information about the error is put into ErrorInfo,
198 /// and the stream should be immediately destroyed; the string will
199 /// be empty if no error occurred.
201 /// \param Filename - The file to open. If this is "-" then the
202 /// stream will use stdout instead.
203 /// \param Binary - The file should be opened in binary mode on
204 /// platforms that support this distinction.
205 raw_fd_ostream(const char *Filename, bool Binary, std::string &ErrorInfo);
207 /// raw_fd_ostream ctor - FD is the file descriptor that this writes to. If
208 /// ShouldClose is true, this closes the file when the stream is destroyed.
209 raw_fd_ostream(int fd, bool shouldClose,
210 bool unbuffered=false) : raw_ostream(unbuffered), FD(fd),
211 ShouldClose(shouldClose) {}
215 /// close - Manually flush the stream and close the file.
218 /// tell - Return the current offset with the file.
219 uint64_t tell() { return pos + GetNumBytesInBuffer(); }
221 /// seek - Flushes the stream and repositions the underlying file descriptor
222 /// positition to the offset specified from the beginning of the file.
223 uint64_t seek(uint64_t off);
226 /// raw_stdout_ostream - This is a stream that always prints to stdout.
228 class raw_stdout_ostream : public raw_fd_ostream {
229 // An out of line virtual method to provide a home for the class vtable.
230 virtual void handle();
232 raw_stdout_ostream();
235 /// raw_stderr_ostream - This is a stream that always prints to stderr.
237 class raw_stderr_ostream : public raw_fd_ostream {
238 // An out of line virtual method to provide a home for the class vtable.
239 virtual void handle();
241 raw_stderr_ostream();
244 /// outs() - This returns a reference to a raw_ostream for standard output.
245 /// Use it like: outs() << "foo" << "bar";
248 /// errs() - This returns a reference to a raw_ostream for standard error.
249 /// Use it like: errs() << "foo" << "bar";
253 //===----------------------------------------------------------------------===//
254 // Output Stream Adaptors
255 //===----------------------------------------------------------------------===//
257 /// raw_os_ostream - A raw_ostream that writes to an std::ostream. This is a
258 /// simple adaptor class.
259 class raw_os_ostream : public raw_ostream {
262 /// write_impl - See raw_ostream::write_impl.
263 virtual void write_impl(const char *Ptr, unsigned Size);
265 raw_os_ostream(std::ostream &O) : OS(O) {}
269 /// raw_string_ostream - A raw_ostream that writes to an std::string. This is a
270 /// simple adaptor class.
271 class raw_string_ostream : public raw_ostream {
274 /// write_impl - See raw_ostream::write_impl.
275 virtual void write_impl(const char *Ptr, unsigned Size);
277 raw_string_ostream(std::string &O) : OS(O) {}
278 ~raw_string_ostream();
280 /// str - Flushes the stream contents to the target string and returns
281 /// the string's reference.
288 /// raw_svector_ostream - A raw_ostream that writes to an SmallVector or
289 /// SmallString. This is a simple adaptor class.
290 class raw_svector_ostream : public raw_ostream {
291 SmallVectorImpl<char> &OS;
293 /// write_impl - See raw_ostream::write_impl.
294 virtual void write_impl(const char *Ptr, unsigned Size);
296 raw_svector_ostream(SmallVectorImpl<char> &O) : OS(O) {}
297 ~raw_svector_ostream();
300 } // end llvm namespace