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
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 char *OutBufStart, *OutBufEnd, *OutBufCur;
37 // Start out ready to flush.
38 OutBufStart = OutBufEnd = OutBufCur = 0;
40 virtual ~raw_ostream() {}
42 //===--------------------------------------------------------------------===//
43 // Configuration Interface
44 //===--------------------------------------------------------------------===//
46 /// SetBufferSize - Set the internal buffer size to the specified amount
47 /// instead of the default.
48 void SetBufferSize(unsigned Size) {
50 "Buffer size must be somewhat large for invariants to hold");
53 delete [] OutBufStart;
54 OutBufStart = new char[Size];
55 OutBufEnd = OutBufStart+Size;
56 OutBufCur = OutBufStart;
59 //===--------------------------------------------------------------------===//
60 // Data Output Interface
61 //===--------------------------------------------------------------------===//
64 if (OutBufCur != OutBufStart)
68 raw_ostream &operator<<(char C) {
69 if (OutBufCur >= OutBufEnd)
75 raw_ostream &operator<<(unsigned char C) {
76 if (OutBufCur >= OutBufEnd)
82 raw_ostream &operator<<(signed char C) {
83 if (OutBufCur >= OutBufEnd)
89 raw_ostream &operator<<(const char *Str) {
90 return write(Str, strlen(Str));
93 raw_ostream &operator<<(const std::string& Str) {
94 return write(Str.data(), Str.length());
97 raw_ostream &operator<<(unsigned long N);
98 raw_ostream &operator<<(long N);
99 raw_ostream &operator<<(unsigned long long N);
100 raw_ostream &operator<<(long long N);
101 raw_ostream &operator<<(const void *P);
102 raw_ostream &operator<<(unsigned int N) {
103 return this->operator<<(static_cast<unsigned long>(N));
106 raw_ostream &operator<<(int N) {
107 return this->operator<<(static_cast<long>(N));
110 raw_ostream &operator<<(double N) {
111 return this->operator<<(ftostr(N));
114 raw_ostream &write(const char *Ptr, unsigned Size);
116 // Formatted output, see the format() function in Support/Format.h.
117 raw_ostream &operator<<(const format_object_base &Fmt);
119 //===--------------------------------------------------------------------===//
120 // Subclass Interface
121 //===--------------------------------------------------------------------===//
125 /// flush_impl - The is the piece of the class that is implemented by
126 /// subclasses. This outputs the currently buffered data and resets the
128 virtual void flush_impl() = 0;
130 /// HandleFlush - A stream's implementation of flush should call this after
131 /// emitting the bytes to the data sink.
133 if (OutBufStart == 0)
135 OutBufCur = OutBufStart;
138 // An out of line virtual method to provide a home for the class vtable.
139 virtual void handle();
142 //===----------------------------------------------------------------------===//
143 // File Output Streams
144 //===----------------------------------------------------------------------===//
146 /// raw_fd_ostream - A raw_ostream that writes to a file descriptor.
148 class raw_fd_ostream : public raw_ostream {
152 /// raw_fd_ostream - Open the specified file for writing. If an error occurs,
153 /// information about the error is put into ErrorInfo, and the stream should
154 /// be immediately destroyed.
155 raw_fd_ostream(const char *Filename, std::string &ErrorInfo);
157 /// raw_fd_ostream ctor - FD is the file descriptor that this writes to. If
158 /// ShouldClose is true, this closes the file when
159 raw_fd_ostream(int fd, bool shouldClose) : FD(fd), ShouldClose(shouldClose) {}
163 /// flush_impl - The is the piece of the class that is implemented by
164 /// subclasses. This outputs the currently buffered data and resets the
166 virtual void flush_impl();
169 /// raw_stdout_ostream - This is a stream that always prints to stdout.
171 class raw_stdout_ostream : public raw_fd_ostream {
172 // An out of line virtual method to provide a home for the class vtable.
173 virtual void handle();
175 raw_stdout_ostream();
178 /// raw_stderr_ostream - This is a stream that always prints to stderr.
180 class raw_stderr_ostream : public raw_fd_ostream {
181 // An out of line virtual method to provide a home for the class vtable.
182 virtual void handle();
184 raw_stderr_ostream();
187 /// outs() - This returns a reference to a raw_ostream for standard output.
188 /// Use it like: outs() << "foo" << "bar";
191 /// errs() - This returns a reference to a raw_ostream for standard error.
192 /// Use it like: errs() << "foo" << "bar";
196 //===----------------------------------------------------------------------===//
197 // Output Stream Adaptors
198 //===----------------------------------------------------------------------===//
200 /// raw_os_ostream - A raw_ostream that writes to an std::ostream. This is a
201 /// simple adaptor class.
202 class raw_os_ostream : public raw_ostream {
205 raw_os_ostream(std::ostream &O) : OS(O) {}
208 /// flush_impl - The is the piece of the class that is implemented by
209 /// subclasses. This outputs the currently buffered data and resets the
211 virtual void flush_impl();
214 /// raw_string_ostream - A raw_ostream that writes to an std::string. This is a
215 /// simple adaptor class.
216 class raw_string_ostream : public raw_ostream {
219 raw_string_ostream(std::string &O) : OS(O) {}
220 ~raw_string_ostream();
222 /// flush_impl - The is the piece of the class that is implemented by
223 /// subclasses. This outputs the currently buffered data and resets the
225 virtual void flush_impl();
228 /// raw_svector_ostream - A raw_ostream that writes to an SmallVector or
229 /// SmallString. This is a simple adaptor class.
230 class raw_svector_ostream : public raw_ostream {
231 SmallVectorImpl<char> &OS;
233 raw_svector_ostream(SmallVectorImpl<char> &O) : OS(O) {}
234 ~raw_svector_ostream();
236 /// flush_impl - The is the piece of the class that is implemented by
237 /// subclasses. This outputs the currently buffered data and resets the
239 virtual void flush_impl();
242 } // end llvm namespace