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 /// \invariant { The buffer is uninitialized (OutBufStart,
35 /// OutBufEnd, and OutBufCur are non-zero), or none of them are zero
36 /// and there are at least 64 total bytes in the buffer. }
38 char *OutBufStart, *OutBufEnd, *OutBufCur;
42 explicit raw_ostream(bool unbuffered=false) : Unbuffered(unbuffered) {
43 // Start out ready to flush.
44 OutBufStart = OutBufEnd = OutBufCur = 0;
47 virtual ~raw_ostream() {
48 delete [] OutBufStart;
51 //===--------------------------------------------------------------------===//
52 // Configuration Interface
53 //===--------------------------------------------------------------------===//
55 /// SetBufferSize - Set the internal buffer size to the specified amount
56 /// instead of the default.
57 void SetBufferSize(unsigned Size) {
59 "Buffer size must be somewhat large for invariants to hold");
62 delete [] OutBufStart;
63 OutBufStart = new char[Size];
64 OutBufEnd = OutBufStart+Size;
65 OutBufCur = OutBufStart;
68 /// SetUnbuffered - Set the streams buffering status. When
69 /// unbuffered the stream will flush after every write. This routine
70 /// will also flush the buffer immediately when the stream is being
71 /// set to unbuffered.
72 void SetUnbuffered(bool unbuffered) {
73 Unbuffered = unbuffered;
78 //===--------------------------------------------------------------------===//
79 // Data Output Interface
80 //===--------------------------------------------------------------------===//
83 if (OutBufCur != OutBufStart)
87 raw_ostream &operator<<(char C) {
88 if (OutBufCur >= OutBufEnd)
96 raw_ostream &operator<<(unsigned char C) {
97 if (OutBufCur >= OutBufEnd)
105 raw_ostream &operator<<(signed char C) {
106 if (OutBufCur >= OutBufEnd)
114 raw_ostream &operator<<(const char *Str) {
115 return write(Str, strlen(Str));
118 raw_ostream &operator<<(const std::string& Str) {
119 return write(Str.data(), Str.length());
122 raw_ostream &operator<<(unsigned long N);
123 raw_ostream &operator<<(long N);
124 raw_ostream &operator<<(unsigned long long N);
125 raw_ostream &operator<<(long long N);
126 raw_ostream &operator<<(const void *P);
127 raw_ostream &operator<<(unsigned int N) {
128 return this->operator<<(static_cast<unsigned long>(N));
131 raw_ostream &operator<<(int N) {
132 return this->operator<<(static_cast<long>(N));
135 raw_ostream &operator<<(double N) {
136 return this->operator<<(ftostr(N));
139 raw_ostream &write(unsigned char C);
140 raw_ostream &write(const char *Ptr, unsigned Size);
142 // Formatted output, see the format() function in Support/Format.h.
143 raw_ostream &operator<<(const format_object_base &Fmt);
145 //===--------------------------------------------------------------------===//
146 // Subclass Interface
147 //===--------------------------------------------------------------------===//
150 /// flush_impl - The is the piece of the class that is implemented by
151 /// subclasses. This only outputs the currently buffered data.
153 /// raw_ostream guarantees to only call this routine when there is
154 /// buffered data, i.e. OutBufStart != OutBufCur.
155 virtual void flush_impl() = 0;
157 // An out of line virtual method to provide a home for the class vtable.
158 virtual void handle();
160 //===--------------------------------------------------------------------===//
162 //===--------------------------------------------------------------------===//
164 /// flush_nonempty - Flush the current buffer, which is known to be
165 /// non-empty. This outputs the currently buffered data and resets
166 /// the buffer to empty.
167 void flush_nonempty();
170 //===----------------------------------------------------------------------===//
171 // File Output Streams
172 //===----------------------------------------------------------------------===//
174 /// raw_fd_ostream - A raw_ostream that writes to a file descriptor.
176 class raw_fd_ostream : public raw_ostream {
181 /// raw_fd_ostream - Open the specified file for writing. If an
182 /// error occurs, information about the error is put into ErrorInfo,
183 /// and the stream should be immediately destroyed; the string will
184 /// be empty if no error occurred.
186 /// \param Filename - The file to open. If this is "-" then the
187 /// stream will use stdout instead.
188 /// \param Binary - The file should be opened in binary mode on
189 /// platforms that support this distinction.
190 raw_fd_ostream(const char *Filename, bool Binary, std::string &ErrorInfo);
192 /// raw_fd_ostream ctor - FD is the file descriptor that this writes to. If
193 /// ShouldClose is true, this closes the file when the stream is destroyed.
194 raw_fd_ostream(int fd, bool shouldClose,
195 bool unbuffered=false) : raw_ostream(unbuffered), FD(fd),
196 ShouldClose(shouldClose) {}
200 /// flush_impl - The is the piece of the class that is implemented by
201 /// subclasses. This only outputs the currently buffered data.
203 /// raw_ostream guarantees to only call this routine when there is
204 /// buffered data, i.e. OutBufStart != OutBufCur.
205 virtual void flush_impl();
207 /// close - Manually flush the stream and close the file.
210 /// tell - Return the current offset with the file.
212 return pos + (OutBufCur - OutBufStart);
215 /// seek - Flushes the stream and repositions the underlying file descriptor
216 /// positition to the offset specified from the beginning of the file.
217 uint64_t seek(uint64_t off);
220 /// raw_stdout_ostream - This is a stream that always prints to stdout.
222 class raw_stdout_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_stdout_ostream();
229 /// raw_stderr_ostream - This is a stream that always prints to stderr.
231 class raw_stderr_ostream : public raw_fd_ostream {
232 // An out of line virtual method to provide a home for the class vtable.
233 virtual void handle();
235 raw_stderr_ostream();
238 /// outs() - This returns a reference to a raw_ostream for standard output.
239 /// Use it like: outs() << "foo" << "bar";
242 /// errs() - This returns a reference to a raw_ostream for standard error.
243 /// Use it like: errs() << "foo" << "bar";
247 //===----------------------------------------------------------------------===//
248 // Output Stream Adaptors
249 //===----------------------------------------------------------------------===//
251 /// raw_os_ostream - A raw_ostream that writes to an std::ostream. This is a
252 /// simple adaptor class.
253 class raw_os_ostream : public raw_ostream {
256 raw_os_ostream(std::ostream &O) : OS(O) {}
259 /// flush_impl - The is the piece of the class that is implemented by
260 /// subclasses. This only outputs the currently buffered data.
262 /// raw_ostream guarantees to only call this routine when there is
263 /// buffered data, i.e. OutBufStart != OutBufCur.
264 virtual void flush_impl();
267 /// raw_string_ostream - A raw_ostream that writes to an std::string. This is a
268 /// simple adaptor class.
269 class raw_string_ostream : public raw_ostream {
272 raw_string_ostream(std::string &O) : OS(O) {}
273 ~raw_string_ostream();
275 /// str - Flushes the stream contents to the target string and returns
276 /// the string's reference.
282 /// flush_impl - The is the piece of the class that is implemented by
283 /// subclasses. This only outputs the currently buffered data.
285 /// raw_ostream guarantees to only call this routine when there is
286 /// buffered data, i.e. OutBufStart != OutBufCur.
287 virtual void flush_impl();
290 /// raw_svector_ostream - A raw_ostream that writes to an SmallVector or
291 /// SmallString. This is a simple adaptor class.
292 class raw_svector_ostream : public raw_ostream {
293 SmallVectorImpl<char> &OS;
295 raw_svector_ostream(SmallVectorImpl<char> &O) : OS(O) {}
296 ~raw_svector_ostream();
298 /// flush_impl - The is the piece of the class that is implemented by
299 /// subclasses. This only outputs the currently buffered data.
301 /// raw_ostream guarantees to only call this routine when there is
302 /// buffered data, i.e. OutBufStart != OutBufCur.
303 virtual void flush_impl();
306 } // end llvm namespace