Add slow path for single character write, and use exclusively for
[oota-llvm.git] / include / llvm / Support / raw_ostream.h
1 //===--- raw_ostream.h - Raw output stream ----------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file defines the raw_ostream class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_SUPPORT_RAW_OSTREAM_H
15 #define LLVM_SUPPORT_RAW_OSTREAM_H
16
17 #include "llvm/ADT/StringExtras.h"
18 #include <cassert>
19 #include <cstring>
20 #include <string>
21 #include <iosfwd>
22
23 namespace llvm {
24   class format_object_base;
25   template <typename T>
26   class SmallVectorImpl;
27
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.
32 class raw_ostream {
33 protected:
34   char *OutBufStart, *OutBufEnd, *OutBufCur;
35   bool Unbuffered;
36
37 public:
38   explicit raw_ostream(bool unbuffered=false) : Unbuffered(unbuffered) {
39     // Start out ready to flush.
40     OutBufStart = OutBufEnd = OutBufCur = 0;
41   }
42
43   virtual ~raw_ostream() {
44     delete [] OutBufStart;
45   }
46
47   //===--------------------------------------------------------------------===//
48   // Configuration Interface
49   //===--------------------------------------------------------------------===//
50
51   /// SetBufferSize - Set the internal buffer size to the specified amount
52   /// instead of the default.
53   void SetBufferSize(unsigned Size) {
54     assert(Size >= 64 &&
55            "Buffer size must be somewhat large for invariants to hold");
56     flush();
57
58     delete [] OutBufStart;
59     OutBufStart = new char[Size];
60     OutBufEnd = OutBufStart+Size;
61     OutBufCur = OutBufStart;
62   }
63
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;
70     if (Unbuffered)
71       flush();
72   }
73
74   //===--------------------------------------------------------------------===//
75   // Data Output Interface
76   //===--------------------------------------------------------------------===//
77
78   void flush() {
79     if (OutBufCur != OutBufStart)
80       flush_impl();
81   }
82
83   raw_ostream &operator<<(char C) {
84     if (OutBufCur >= OutBufEnd)
85       return write(C);
86     *OutBufCur++ = C;
87     if (Unbuffered)
88       flush_impl();
89     return *this;
90   }
91
92   raw_ostream &operator<<(unsigned char C) {
93     if (OutBufCur >= OutBufEnd)
94       return write(C);
95     *OutBufCur++ = C;
96     if (Unbuffered)
97       flush_impl();
98     return *this;
99   }
100
101   raw_ostream &operator<<(signed char C) {
102     if (OutBufCur >= OutBufEnd)
103       return write(C);
104     *OutBufCur++ = C;
105     if (Unbuffered)
106       flush_impl();
107     return *this;
108   }
109
110   raw_ostream &operator<<(const char *Str) {
111     return write(Str, strlen(Str));
112   }
113
114   raw_ostream &operator<<(const std::string& Str) {
115     return write(Str.data(), Str.length());
116   }
117
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));
125   }
126
127   raw_ostream &operator<<(int N) {
128     return this->operator<<(static_cast<long>(N));
129   }
130
131   raw_ostream &operator<<(double N) {
132     return this->operator<<(ftostr(N));
133   }
134
135   raw_ostream &write(unsigned char C);
136   raw_ostream &write(const char *Ptr, unsigned Size);
137
138   // Formatted output, see the format() function in Support/Format.h.
139   raw_ostream &operator<<(const format_object_base &Fmt);
140
141   //===--------------------------------------------------------------------===//
142   // Subclass Interface
143   //===--------------------------------------------------------------------===//
144
145 protected:
146
147   /// flush_impl - The is the piece of the class that is implemented by
148   /// subclasses.  This outputs the currently buffered data and resets the
149   /// buffer to empty.
150   virtual void flush_impl() = 0;
151
152   /// HandleFlush - A stream's implementation of flush should call this after
153   /// emitting the bytes to the data sink.
154   void HandleFlush() {
155     if (OutBufStart == 0)
156       SetBufferSize(4096);
157     OutBufCur = OutBufStart;
158   }
159 private:
160   // An out of line virtual method to provide a home for the class vtable.
161   virtual void handle();
162 };
163
164 //===----------------------------------------------------------------------===//
165 // File Output Streams
166 //===----------------------------------------------------------------------===//
167
168 /// raw_fd_ostream - A raw_ostream that writes to a file descriptor.
169 ///
170 class raw_fd_ostream : public raw_ostream {
171   int FD;
172   bool ShouldClose;
173   uint64_t pos;
174 public:
175   /// raw_fd_ostream - Open the specified file for writing. If an
176   /// error occurs, information about the error is put into ErrorInfo,
177   /// and the stream should be immediately destroyed; the string will
178   /// be empty if no error occurred.
179   ///
180   /// \param Filename - The file to open. If this is "-" then the
181   /// stream will use stdout instead.
182   /// \param Binary - The file should be opened in binary mode on
183   /// platforms that support this distinction.
184   raw_fd_ostream(const char *Filename, bool Binary, std::string &ErrorInfo);
185
186   /// raw_fd_ostream ctor - FD is the file descriptor that this writes to.  If
187   /// ShouldClose is true, this closes the file when the stream is destroyed.
188   raw_fd_ostream(int fd, bool shouldClose, 
189                  bool unbuffered=false) : raw_ostream(unbuffered), FD(fd), 
190                                           ShouldClose(shouldClose) {}
191   
192   ~raw_fd_ostream();
193
194   /// flush_impl - The is the piece of the class that is implemented by
195   /// subclasses.  This outputs the currently buffered data and resets the
196   /// buffer to empty.
197   virtual void flush_impl();
198
199   /// close - Manually flush the stream and close the file.
200   void close();
201
202   /// tell - Return the current offset with the file.
203   uint64_t tell() {
204     return pos + (OutBufCur - OutBufStart);
205   }
206
207   /// seek - Flushes the stream and repositions the underlying file descriptor
208   ///  positition to the offset specified from the beginning of the file.
209   uint64_t seek(uint64_t off);
210 };
211
212 /// raw_stdout_ostream - This is a stream that always prints to stdout.
213 ///
214 class raw_stdout_ostream : public raw_fd_ostream {
215   // An out of line virtual method to provide a home for the class vtable.
216   virtual void handle();
217 public:
218   raw_stdout_ostream();
219 };
220
221 /// raw_stderr_ostream - This is a stream that always prints to stderr.
222 ///
223 class raw_stderr_ostream : public raw_fd_ostream {
224   // An out of line virtual method to provide a home for the class vtable.
225   virtual void handle();
226 public:
227   raw_stderr_ostream();
228 };
229
230 /// outs() - This returns a reference to a raw_ostream for standard output.
231 /// Use it like: outs() << "foo" << "bar";
232 raw_ostream &outs();
233
234 /// errs() - This returns a reference to a raw_ostream for standard error.
235 /// Use it like: errs() << "foo" << "bar";
236 raw_ostream &errs();
237
238
239 //===----------------------------------------------------------------------===//
240 // Output Stream Adaptors
241 //===----------------------------------------------------------------------===//
242
243 /// raw_os_ostream - A raw_ostream that writes to an std::ostream.  This is a
244 /// simple adaptor class.
245 class raw_os_ostream : public raw_ostream {
246   std::ostream &OS;
247 public:
248   raw_os_ostream(std::ostream &O) : OS(O) {}
249   ~raw_os_ostream();
250
251   /// flush_impl - The is the piece of the class that is implemented by
252   /// subclasses.  This outputs the currently buffered data and resets the
253   /// buffer to empty.
254   virtual void flush_impl();
255 };
256
257 /// raw_string_ostream - A raw_ostream that writes to an std::string.  This is a
258 /// simple adaptor class.
259 class raw_string_ostream : public raw_ostream {
260   std::string &OS;
261 public:
262   raw_string_ostream(std::string &O) : OS(O) {}
263   ~raw_string_ostream();
264
265   /// str - Flushes the stream contents to the target string and returns
266   ///  the string's reference.
267   std::string& str() {
268     flush();
269     return OS;
270   }
271
272   /// flush_impl - The is the piece of the class that is implemented by
273   /// subclasses.  This outputs the currently buffered data and resets the
274   /// buffer to empty.
275   virtual void flush_impl();
276 };
277
278 /// raw_svector_ostream - A raw_ostream that writes to an SmallVector or
279 /// SmallString.  This is a simple adaptor class.
280 class raw_svector_ostream : public raw_ostream {
281   SmallVectorImpl<char> &OS;
282 public:
283   raw_svector_ostream(SmallVectorImpl<char> &O) : OS(O) {}
284   ~raw_svector_ostream();
285
286   /// flush_impl - The is the piece of the class that is implemented by
287   /// subclasses.  This outputs the currently buffered data and resets the
288   /// buffer to empty.
289   virtual void flush_impl();
290 };
291
292 } // end llvm namespace
293
294 #endif