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 char *OutBufStart, *OutBufEnd, *OutBufCur;
37 // Start out ready to flush.
38 OutBufStart = OutBufEnd = OutBufCur = 0;
41 virtual ~raw_ostream() {
42 delete [] OutBufStart;
45 //===--------------------------------------------------------------------===//
46 // Configuration Interface
47 //===--------------------------------------------------------------------===//
49 /// SetBufferSize - Set the internal buffer size to the specified amount
50 /// instead of the default.
51 void SetBufferSize(unsigned Size) {
53 "Buffer size must be somewhat large for invariants to hold");
56 delete [] OutBufStart;
57 OutBufStart = new char[Size];
58 OutBufEnd = OutBufStart+Size;
59 OutBufCur = OutBufStart;
62 //===--------------------------------------------------------------------===//
63 // Data Output Interface
64 //===--------------------------------------------------------------------===//
67 if (OutBufCur != OutBufStart)
71 raw_ostream &operator<<(char C) {
72 if (OutBufCur >= OutBufEnd)
78 raw_ostream &operator<<(unsigned char C) {
79 if (OutBufCur >= OutBufEnd)
85 raw_ostream &operator<<(signed char C) {
86 if (OutBufCur >= OutBufEnd)
92 raw_ostream &operator<<(const char *Str) {
93 return write(Str, strlen(Str));
96 raw_ostream &operator<<(const std::string& Str) {
97 return write(Str.data(), Str.length());
100 raw_ostream &operator<<(unsigned long N);
101 raw_ostream &operator<<(long N);
102 raw_ostream &operator<<(unsigned long long N);
103 raw_ostream &operator<<(long long N);
104 raw_ostream &operator<<(const void *P);
105 raw_ostream &operator<<(unsigned int N) {
106 return this->operator<<(static_cast<unsigned long>(N));
109 raw_ostream &operator<<(int N) {
110 return this->operator<<(static_cast<long>(N));
113 raw_ostream &operator<<(double N) {
114 return this->operator<<(ftostr(N));
117 raw_ostream &write(const char *Ptr, unsigned Size);
119 // Formatted output, see the format() function in Support/Format.h.
120 raw_ostream &operator<<(const format_object_base &Fmt);
122 //===--------------------------------------------------------------------===//
123 // Subclass Interface
124 //===--------------------------------------------------------------------===//
128 /// flush_impl - The is the piece of the class that is implemented by
129 /// subclasses. This outputs the currently buffered data and resets the
131 virtual void flush_impl() = 0;
133 /// HandleFlush - A stream's implementation of flush should call this after
134 /// emitting the bytes to the data sink.
136 if (OutBufStart == 0)
138 OutBufCur = OutBufStart;
141 // An out of line virtual method to provide a home for the class vtable.
142 virtual void handle();
145 //===----------------------------------------------------------------------===//
146 // File Output Streams
147 //===----------------------------------------------------------------------===//
149 /// raw_fd_ostream - A raw_ostream that writes to a file descriptor.
151 class raw_fd_ostream : public raw_ostream {
156 /// raw_fd_ostream - Open the specified file for writing. If an
157 /// error occurs, information about the error is put into ErrorInfo,
158 /// and the stream should be immediately destroyed; the string will
159 /// be empty if no error occurred.
161 /// \param Filename - The file to open. If this is "-" then the
162 /// stream will use stdout instead.
163 /// \param Binary - The file should be opened in binary mode on
164 /// platforms that support this distinction.
165 raw_fd_ostream(const char *Filename, bool Binary, std::string &ErrorInfo);
167 /// raw_fd_ostream ctor - FD is the file descriptor that this writes to. If
168 /// ShouldClose is true, this closes the file when
169 raw_fd_ostream(int fd, bool shouldClose) : FD(fd), ShouldClose(shouldClose) {}
173 /// flush_impl - The is the piece of the class that is implemented by
174 /// subclasses. This outputs the currently buffered data and resets the
176 virtual void flush_impl();
178 /// close - Manually flush the stream and close the file.
181 /// tell - Return the current offset with the file.
183 return pos + (OutBufCur - OutBufStart);
186 /// seek - Flushes the stream and repositions the underlying file descriptor
187 /// positition to the offset specified from the beginning of the file.
188 uint64_t seek(uint64_t off);
191 /// raw_stdout_ostream - This is a stream that always prints to stdout.
193 class raw_stdout_ostream : public raw_fd_ostream {
194 // An out of line virtual method to provide a home for the class vtable.
195 virtual void handle();
197 raw_stdout_ostream();
200 /// raw_stderr_ostream - This is a stream that always prints to stderr.
202 class raw_stderr_ostream : public raw_fd_ostream {
203 // An out of line virtual method to provide a home for the class vtable.
204 virtual void handle();
206 raw_stderr_ostream();
209 /// outs() - This returns a reference to a raw_ostream for standard output.
210 /// Use it like: outs() << "foo" << "bar";
213 /// errs() - This returns a reference to a raw_ostream for standard error.
214 /// Use it like: errs() << "foo" << "bar";
218 //===----------------------------------------------------------------------===//
219 // Output Stream Adaptors
220 //===----------------------------------------------------------------------===//
222 /// raw_os_ostream - A raw_ostream that writes to an std::ostream. This is a
223 /// simple adaptor class.
224 class raw_os_ostream : public raw_ostream {
227 raw_os_ostream(std::ostream &O) : OS(O) {}
230 /// flush_impl - The is the piece of the class that is implemented by
231 /// subclasses. This outputs the currently buffered data and resets the
233 virtual void flush_impl();
236 /// raw_string_ostream - A raw_ostream that writes to an std::string. This is a
237 /// simple adaptor class.
238 class raw_string_ostream : public raw_ostream {
241 raw_string_ostream(std::string &O) : OS(O) {}
242 ~raw_string_ostream();
244 /// str - Flushes the stream contents to the target string and returns
245 /// the string's reference.
251 /// flush_impl - The is the piece of the class that is implemented by
252 /// subclasses. This outputs the currently buffered data and resets the
254 virtual void flush_impl();
257 /// raw_svector_ostream - A raw_ostream that writes to an SmallVector or
258 /// SmallString. This is a simple adaptor class.
259 class raw_svector_ostream : public raw_ostream {
260 SmallVectorImpl<char> &OS;
262 raw_svector_ostream(SmallVectorImpl<char> &O) : OS(O) {}
263 ~raw_svector_ostream();
265 /// flush_impl - The is the piece of the class that is implemented by
266 /// subclasses. This outputs the currently buffered data and resets the
268 virtual void flush_impl();
271 } // end llvm namespace