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 /// The buffer is handled in such a way that the buffer is
35 /// uninitialized, unbuffered, or out of space when OutBufCur >=
36 /// OutBufEnd. Thus a single comparison suffices to determine if we
37 /// need to take the slow path to write a single character.
39 /// The buffer is in one of three states:
40 /// 1. Unbuffered (Unbuffered == true)
41 /// 1. Uninitialized (Unbuffered == false && OutBufStart == 0).
42 /// 2. Buffered (Unbuffered == false && OutBufStart != 0 &&
43 /// OutBufEnd - OutBufStart >= 64).
44 char *OutBufStart, *OutBufEnd, *OutBufCur;
48 explicit raw_ostream(bool unbuffered=false) : Unbuffered(unbuffered) {
49 // Start out ready to flush.
50 OutBufStart = OutBufEnd = OutBufCur = 0;
53 virtual ~raw_ostream() {
54 delete [] OutBufStart;
57 //===--------------------------------------------------------------------===//
58 // Configuration Interface
59 //===--------------------------------------------------------------------===//
61 /// SetBufferSize - Set the internal buffer size to the specified amount
62 /// instead of the default.
63 void SetBufferSize(unsigned Size=4096) {
65 "Buffer size must be somewhat large for invariants to hold");
68 delete [] OutBufStart;
69 OutBufStart = new char[Size];
70 OutBufEnd = OutBufStart+Size;
71 OutBufCur = OutBufStart;
75 /// SetUnbuffered - Set the streams buffering status. When
76 /// unbuffered the stream will flush after every write. This routine
77 /// will also flush the buffer immediately when the stream is being
78 /// set to unbuffered.
79 void SetUnbuffered() {
82 delete [] OutBufStart;
83 OutBufStart = OutBufEnd = OutBufCur = 0;
87 unsigned GetNumBytesInBuffer() const {
88 return OutBufCur - OutBufStart;
91 //===--------------------------------------------------------------------===//
92 // Data Output Interface
93 //===--------------------------------------------------------------------===//
96 if (OutBufCur != OutBufStart)
100 raw_ostream &operator<<(char C) {
101 if (OutBufCur >= OutBufEnd)
107 raw_ostream &operator<<(unsigned char C) {
108 if (OutBufCur >= OutBufEnd)
114 raw_ostream &operator<<(signed char C) {
115 if (OutBufCur >= OutBufEnd)
121 raw_ostream &operator<<(const char *Str) {
122 // Inline fast path, particulary for constant strings where a
123 // sufficiently smart compiler will simplify strlen.
125 unsigned Size = strlen(Str);
127 // Make sure we can use the fast path.
128 if (OutBufCur+Size > OutBufEnd)
129 return write(Str, Size);
131 memcpy(OutBufCur, Str, Size);
136 raw_ostream &operator<<(const std::string& Str) {
137 write(Str.data(), Str.length());
141 raw_ostream &operator<<(unsigned long N);
142 raw_ostream &operator<<(long N);
143 raw_ostream &operator<<(unsigned long long N);
144 raw_ostream &operator<<(long long N);
145 raw_ostream &operator<<(const void *P);
146 raw_ostream &operator<<(unsigned int N) {
147 this->operator<<(static_cast<unsigned long>(N));
151 raw_ostream &operator<<(int N) {
152 this->operator<<(static_cast<long>(N));
156 raw_ostream &operator<<(double N) {
157 this->operator<<(ftostr(N));
161 raw_ostream &write(unsigned char C);
162 raw_ostream &write(const char *Ptr, unsigned Size);
164 // Formatted output, see the format() function in Support/Format.h.
165 raw_ostream &operator<<(const format_object_base &Fmt);
167 //===--------------------------------------------------------------------===//
168 // Subclass Interface
169 //===--------------------------------------------------------------------===//
172 /// write_impl - The is the piece of the class that is implemented
173 /// by subclasses. This writes the \args Size bytes starting at
174 /// \arg Ptr to the underlying stream.
176 /// \invariant { Size > 0 }
177 virtual void write_impl(const char *Ptr, unsigned Size) = 0;
179 // An out of line virtual method to provide a home for the class vtable.
180 virtual void handle();
182 //===--------------------------------------------------------------------===//
184 //===--------------------------------------------------------------------===//
186 /// flush_nonempty - Flush the current buffer, which is known to be
187 /// non-empty. This outputs the currently buffered data and resets
188 /// the buffer to empty.
189 void flush_nonempty();
192 //===----------------------------------------------------------------------===//
193 // File Output Streams
194 //===----------------------------------------------------------------------===//
196 /// raw_fd_ostream - A raw_ostream that writes to a file descriptor.
198 class raw_fd_ostream : public raw_ostream {
203 /// write_impl - See raw_ostream::write_impl.
204 virtual void write_impl(const char *Ptr, unsigned Size);
206 /// raw_fd_ostream - Open the specified file for writing. If an
207 /// error occurs, information about the error is put into ErrorInfo,
208 /// and the stream should be immediately destroyed; the string will
209 /// be empty if no error occurred.
211 /// \param Filename - The file to open. If this is "-" then the
212 /// stream will use stdout instead.
213 /// \param Binary - The file should be opened in binary mode on
214 /// platforms that support this distinction.
215 raw_fd_ostream(const char *Filename, bool Binary, std::string &ErrorInfo);
217 /// raw_fd_ostream ctor - FD is the file descriptor that this writes to. If
218 /// ShouldClose is true, this closes the file when the stream is destroyed.
219 raw_fd_ostream(int fd, bool shouldClose,
220 bool unbuffered=false) : raw_ostream(unbuffered), FD(fd),
221 ShouldClose(shouldClose) {}
225 /// close - Manually flush the stream and close the file.
228 /// tell - Return the current offset with the file.
229 uint64_t tell() { return pos + GetNumBytesInBuffer(); }
231 /// seek - Flushes the stream and repositions the underlying file descriptor
232 /// positition to the offset specified from the beginning of the file.
233 uint64_t seek(uint64_t off);
236 /// raw_stdout_ostream - This is a stream that always prints to stdout.
238 class raw_stdout_ostream : public raw_fd_ostream {
239 // An out of line virtual method to provide a home for the class vtable.
240 virtual void handle();
242 raw_stdout_ostream();
245 /// raw_stderr_ostream - This is a stream that always prints to stderr.
247 class raw_stderr_ostream : public raw_fd_ostream {
248 // An out of line virtual method to provide a home for the class vtable.
249 virtual void handle();
251 raw_stderr_ostream();
254 /// outs() - This returns a reference to a raw_ostream for standard output.
255 /// Use it like: outs() << "foo" << "bar";
258 /// errs() - This returns a reference to a raw_ostream for standard error.
259 /// Use it like: errs() << "foo" << "bar";
263 //===----------------------------------------------------------------------===//
264 // Output Stream Adaptors
265 //===----------------------------------------------------------------------===//
267 /// raw_os_ostream - A raw_ostream that writes to an std::ostream. This is a
268 /// simple adaptor class.
269 class raw_os_ostream : public raw_ostream {
272 /// write_impl - See raw_ostream::write_impl.
273 virtual void write_impl(const char *Ptr, unsigned Size);
275 raw_os_ostream(std::ostream &O) : OS(O) {}
279 /// raw_string_ostream - A raw_ostream that writes to an std::string. This is a
280 /// simple adaptor class.
281 class raw_string_ostream : public raw_ostream {
284 /// write_impl - See raw_ostream::write_impl.
285 virtual void write_impl(const char *Ptr, unsigned Size);
287 raw_string_ostream(std::string &O) : OS(O) {}
288 ~raw_string_ostream();
290 /// str - Flushes the stream contents to the target string and returns
291 /// the string's reference.
298 /// raw_svector_ostream - A raw_ostream that writes to an SmallVector or
299 /// SmallString. This is a simple adaptor class.
300 class raw_svector_ostream : public raw_ostream {
301 SmallVectorImpl<char> &OS;
303 /// write_impl - See raw_ostream::write_impl.
304 virtual void write_impl(const char *Ptr, unsigned Size);
306 raw_svector_ostream(SmallVectorImpl<char> &O) : OS(O) {}
307 ~raw_svector_ostream();
310 } // end llvm namespace