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 /// raw_ostream - This class implements an extremely fast bulk output stream
27 /// that can *only* output to a stream. It does not support seeking, reopening,
28 /// rewinding, line buffered disciplines etc. It is a simple buffer that outputs
29 /// a chunk at a time.
32 char *OutBufStart, *OutBufEnd, *OutBufCur;
35 // Start out ready to flush.
36 OutBufStart = OutBufEnd = OutBufCur = 0;
38 virtual ~raw_ostream() {}
40 //===--------------------------------------------------------------------===//
41 // Configuration Interface
42 //===--------------------------------------------------------------------===//
44 /// SetBufferSize - Set the internal buffer size to the specified amount
45 /// instead of the default.
46 void SetBufferSize(unsigned Size) {
48 "Buffer size must be somewhat large for invariants to hold");
51 delete [] OutBufStart;
52 OutBufStart = new char[Size];
53 OutBufEnd = OutBufStart+Size;
54 OutBufCur = OutBufStart;
57 //===--------------------------------------------------------------------===//
58 // Data Output Interface
59 //===--------------------------------------------------------------------===//
62 if (OutBufCur != OutBufStart)
66 raw_ostream &operator<<(char C) {
67 if (OutBufCur >= OutBufEnd)
73 raw_ostream &operator<<(unsigned char C) {
74 if (OutBufCur >= OutBufEnd)
80 raw_ostream &operator<<(signed char C) {
81 if (OutBufCur >= OutBufEnd)
87 raw_ostream &operator<<(const char *Str) {
88 return write(Str, strlen(Str));
91 raw_ostream &operator<<(const std::string& Str) {
92 return write(Str.data(), Str.length());
95 raw_ostream &operator<<(unsigned long N);
96 raw_ostream &operator<<(long N);
97 raw_ostream &operator<<(unsigned long long N);
98 raw_ostream &operator<<(long long N);
99 raw_ostream &operator<<(const void *P);
100 raw_ostream &operator<<(unsigned int N) {
101 return this->operator<<(static_cast<unsigned long>(N));
104 raw_ostream &operator<<(int N) {
105 return this->operator<<(static_cast<long>(N));
108 raw_ostream &operator<<(double N) {
109 return this->operator<<(ftostr(N));
112 raw_ostream &write(const char *Ptr, unsigned Size);
114 // Formatted output, see the format() function in Support/Format.h.
115 raw_ostream &operator<<(const format_object_base &Fmt);
117 //===--------------------------------------------------------------------===//
118 // Subclass Interface
119 //===--------------------------------------------------------------------===//
123 /// flush_impl - The is the piece of the class that is implemented by
124 /// subclasses. This outputs the currently buffered data and resets the
126 virtual void flush_impl() = 0;
128 /// HandleFlush - A stream's implementation of flush should call this after
129 /// emitting the bytes to the data sink.
131 if (OutBufStart == 0)
133 OutBufCur = OutBufStart;
136 // An out of line virtual method to provide a home for the class vtable.
137 virtual void handle();
140 //===----------------------------------------------------------------------===//
141 // File Output Streams
142 //===----------------------------------------------------------------------===//
144 /// raw_fd_ostream - A raw_ostream that writes to a file descriptor.
146 class raw_fd_ostream : public raw_ostream {
150 /// raw_fd_ostream - Open the specified file for writing. If an error occurs,
151 /// information about the error is put into ErrorInfo, and the stream should
152 /// be immediately destroyed.
153 raw_fd_ostream(const char *Filename, std::string &ErrorInfo);
155 /// raw_fd_ostream ctor - FD is the file descriptor that this writes to. If
156 /// ShouldClose is true, this closes the file when
157 raw_fd_ostream(int fd, bool shouldClose) : FD(fd), ShouldClose(shouldClose) {}
161 /// flush_impl - The is the piece of the class that is implemented by
162 /// subclasses. This outputs the currently buffered data and resets the
164 virtual void flush_impl();
167 /// raw_stdout_ostream - This is a stream that always prints to stdout.
169 class raw_stdout_ostream : public raw_fd_ostream {
170 // An out of line virtual method to provide a home for the class vtable.
171 virtual void handle();
173 raw_stdout_ostream();
176 /// raw_stderr_ostream - This is a stream that always prints to stderr.
178 class raw_stderr_ostream : public raw_fd_ostream {
179 // An out of line virtual method to provide a home for the class vtable.
180 virtual void handle();
182 raw_stderr_ostream();
185 /// outs() - This returns a reference to a raw_ostream for standard output.
186 /// Use it like: outs() << "foo" << "bar";
189 /// errs() - This returns a reference to a raw_ostream for standard error.
190 /// Use it like: errs() << "foo" << "bar";
194 //===----------------------------------------------------------------------===//
195 // Bridge Output Streams
196 //===----------------------------------------------------------------------===//
198 /// raw_os_ostream - A raw_ostream that writes to an std::ostream. This is a
199 /// simple adaptor class.
200 class raw_os_ostream : public raw_ostream {
203 raw_os_ostream(std::ostream &O) : OS(O) {}
206 /// flush_impl - The is the piece of the class that is implemented by
207 /// subclasses. This outputs the currently buffered data and resets the
209 virtual void flush_impl();
212 } // end llvm namespace