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;
38 explicit raw_ostream(bool unbuffered=false) : Unbuffered(unbuffered) {
39 // Start out ready to flush.
40 OutBufStart = OutBufEnd = OutBufCur = 0;
43 virtual ~raw_ostream() {
44 delete [] OutBufStart;
47 //===--------------------------------------------------------------------===//
48 // Configuration Interface
49 //===--------------------------------------------------------------------===//
51 /// SetBufferSize - Set the internal buffer size to the specified amount
52 /// instead of the default.
53 void SetBufferSize(unsigned Size) {
55 "Buffer size must be somewhat large for invariants to hold");
58 delete [] OutBufStart;
59 OutBufStart = new char[Size];
60 OutBufEnd = OutBufStart+Size;
61 OutBufCur = OutBufStart;
64 /// SetUnbuffered - Set the streams buffering status. When
65 /// unbuffered the stream will flush after every write. This routine
66 /// will also flush the buffer immediately when the stream is being
67 /// set to unbuffered.
68 void SetUnbuffered(bool unbuffered) {
69 Unbuffered = unbuffered;
74 //===--------------------------------------------------------------------===//
75 // Data Output Interface
76 //===--------------------------------------------------------------------===//
79 if (OutBufCur != OutBufStart)
83 raw_ostream &operator<<(char C) {
84 if (OutBufCur >= OutBufEnd)
92 raw_ostream &operator<<(unsigned char C) {
93 if (OutBufCur >= OutBufEnd)
101 raw_ostream &operator<<(signed char C) {
102 if (OutBufCur >= OutBufEnd)
110 raw_ostream &operator<<(const char *Str) {
111 return write(Str, strlen(Str));
114 raw_ostream &operator<<(const std::string& Str) {
115 return write(Str.data(), Str.length());
118 raw_ostream &operator<<(unsigned long N);
119 raw_ostream &operator<<(long N);
120 raw_ostream &operator<<(unsigned long long N);
121 raw_ostream &operator<<(long long N);
122 raw_ostream &operator<<(const void *P);
123 raw_ostream &operator<<(unsigned int N) {
124 return this->operator<<(static_cast<unsigned long>(N));
127 raw_ostream &operator<<(int N) {
128 return this->operator<<(static_cast<long>(N));
131 raw_ostream &operator<<(double N) {
132 return this->operator<<(ftostr(N));
135 raw_ostream &write(const char *Ptr, unsigned Size);
137 // Formatted output, see the format() function in Support/Format.h.
138 raw_ostream &operator<<(const format_object_base &Fmt);
140 //===--------------------------------------------------------------------===//
141 // Subclass Interface
142 //===--------------------------------------------------------------------===//
146 /// flush_impl - The is the piece of the class that is implemented by
147 /// subclasses. This outputs the currently buffered data and resets the
149 virtual void flush_impl() = 0;
151 /// HandleFlush - A stream's implementation of flush should call this after
152 /// emitting the bytes to the data sink.
154 if (OutBufStart == 0)
156 OutBufCur = OutBufStart;
159 // An out of line virtual method to provide a home for the class vtable.
160 virtual void handle();
163 //===----------------------------------------------------------------------===//
164 // File Output Streams
165 //===----------------------------------------------------------------------===//
167 /// raw_fd_ostream - A raw_ostream that writes to a file descriptor.
169 class raw_fd_ostream : public raw_ostream {
174 /// raw_fd_ostream - Open the specified file for writing. If an
175 /// error occurs, information about the error is put into ErrorInfo,
176 /// and the stream should be immediately destroyed; the string will
177 /// be empty if no error occurred.
179 /// \param Filename - The file to open. If this is "-" then the
180 /// stream will use stdout instead.
181 /// \param Binary - The file should be opened in binary mode on
182 /// platforms that support this distinction.
183 raw_fd_ostream(const char *Filename, bool Binary, std::string &ErrorInfo);
185 /// raw_fd_ostream ctor - FD is the file descriptor that this writes to. If
186 /// ShouldClose is true, this closes the file when the stream is destroyed.
187 raw_fd_ostream(int fd, bool shouldClose,
188 bool unbuffered=false) : raw_ostream(unbuffered), FD(fd),
189 ShouldClose(shouldClose) {}
193 /// flush_impl - The is the piece of the class that is implemented by
194 /// subclasses. This outputs the currently buffered data and resets the
196 virtual void flush_impl();
198 /// close - Manually flush the stream and close the file.
201 /// tell - Return the current offset with the file.
203 return pos + (OutBufCur - OutBufStart);
206 /// seek - Flushes the stream and repositions the underlying file descriptor
207 /// positition to the offset specified from the beginning of the file.
208 uint64_t seek(uint64_t off);
211 /// raw_stdout_ostream - This is a stream that always prints to stdout.
213 class raw_stdout_ostream : public raw_fd_ostream {
214 // An out of line virtual method to provide a home for the class vtable.
215 virtual void handle();
217 raw_stdout_ostream();
220 /// raw_stderr_ostream - This is a stream that always prints to stderr.
222 class raw_stderr_ostream : public raw_fd_ostream {
223 // An out of line virtual method to provide a home for the class vtable.
224 virtual void handle();
226 raw_stderr_ostream();
229 /// outs() - This returns a reference to a raw_ostream for standard output.
230 /// Use it like: outs() << "foo" << "bar";
233 /// errs() - This returns a reference to a raw_ostream for standard error.
234 /// Use it like: errs() << "foo" << "bar";
238 //===----------------------------------------------------------------------===//
239 // Output Stream Adaptors
240 //===----------------------------------------------------------------------===//
242 /// raw_os_ostream - A raw_ostream that writes to an std::ostream. This is a
243 /// simple adaptor class.
244 class raw_os_ostream : public raw_ostream {
247 raw_os_ostream(std::ostream &O) : OS(O) {}
250 /// flush_impl - The is the piece of the class that is implemented by
251 /// subclasses. This outputs the currently buffered data and resets the
253 virtual void flush_impl();
256 /// raw_string_ostream - A raw_ostream that writes to an std::string. This is a
257 /// simple adaptor class.
258 class raw_string_ostream : public raw_ostream {
261 raw_string_ostream(std::string &O) : OS(O) {}
262 ~raw_string_ostream();
264 /// str - Flushes the stream contents to the target string and returns
265 /// the string's reference.
271 /// flush_impl - The is the piece of the class that is implemented by
272 /// subclasses. This outputs the currently buffered data and resets the
274 virtual void flush_impl();
277 /// raw_svector_ostream - A raw_ostream that writes to an SmallVector or
278 /// SmallString. This is a simple adaptor class.
279 class raw_svector_ostream : public raw_ostream {
280 SmallVectorImpl<char> &OS;
282 raw_svector_ostream(SmallVectorImpl<char> &O) : OS(O) {}
283 ~raw_svector_ostream();
285 /// flush_impl - The is the piece of the class that is implemented by
286 /// subclasses. This outputs the currently buffered data and resets the
288 virtual void flush_impl();
291 } // end llvm namespace