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);
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 below.
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 //===----------------------------------------------------------------------===//
142 //===----------------------------------------------------------------------===//
144 /// format_object_base - This is a helper class used for handling formatted
145 /// output. It is the abstract base class of a templated derived class.
146 class format_object_base {
149 virtual void home(); // Out of line virtual method.
151 format_object_base(const char *fmt) : Fmt(fmt) {}
152 virtual ~format_object_base() {}
154 /// print - Format the object into the specified buffer. On success, this
155 /// returns the length of the formatted string. If the buffer is too small,
156 /// this returns a length to retry with, which will be larger than BufferSize.
157 virtual unsigned print(char *Buffer, unsigned BufferSize) const = 0;
160 /// format_object - This is a templated helper class used by the format function
161 /// that captures the object to be formated and the format string. When
162 /// actually printed, this synthesizes the string into a temporary buffer
163 /// provided and returns whether or not it is big enough.
164 template <typename T>
165 class format_object : public format_object_base {
168 format_object(const char *fmt, const T &val)
169 : format_object_base(fmt), Val(val) {
172 /// print - Format the object into the specified buffer. On success, this
173 /// returns the length of the formatted string. If the buffer is too small,
174 /// this returns a length to retry with, which will be larger than BufferSize.
175 virtual unsigned print(char *Buffer, unsigned BufferSize) const {
176 int N = snprintf(Buffer, BufferSize-1, Fmt, Val);
177 if (N < 0) // VC++ and old GlibC return negative on overflow.
179 if (unsigned(N) >= BufferSize-1)// Other impls yield number of bytes needed.
181 // If N is positive and <= BufferSize-1, then the string fit, yay.
186 /// format - This is a helper function that is used to produce formatted output.
187 /// This is typically used like: OS << format("%0.4f", myfloat) << '\n';
188 template <typename T>
189 inline format_object<T> format(const char *Fmt, const T &Val) {
190 return format_object<T>(Fmt, Val);
193 //===----------------------------------------------------------------------===//
194 // File Output Streams
195 //===----------------------------------------------------------------------===//
197 /// raw_fd_ostream - A raw_ostream that writes to a file descriptor.
199 class raw_fd_ostream : public raw_ostream {
203 /// raw_fd_ostream - Open the specified file for writing. If an error occurs,
204 /// information about the error is put into ErrorInfo, and the stream should
205 /// be immediately destroyed.
206 raw_fd_ostream(const char *Filename, std::string &ErrorInfo);
208 /// raw_fd_ostream ctor - FD is the file descriptor that this writes to. If
209 /// ShouldClose is true, this closes the file when
210 raw_fd_ostream(int fd, bool shouldClose) : FD(fd), ShouldClose(shouldClose) {}
214 /// flush_impl - The is the piece of the class that is implemented by
215 /// subclasses. This outputs the currently buffered data and resets the
217 virtual void flush_impl();
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 // Bridge Output Streams
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) {}
258 /// flush_impl - The is the piece of the class that is implemented by
259 /// subclasses. This outputs the currently buffered data and resets the
261 virtual void flush_impl();
264 } // end llvm namespace