Add fast path for raw_ostream output of strings.
[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 private:
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.
38   ///
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;
45   bool Unbuffered;
46
47 public:
48   explicit raw_ostream(bool unbuffered=false) : Unbuffered(unbuffered) {
49     // Start out ready to flush.
50     OutBufStart = OutBufEnd = OutBufCur = 0;
51   }
52
53   virtual ~raw_ostream() {
54     delete [] OutBufStart;
55   }
56
57   //===--------------------------------------------------------------------===//
58   // Configuration Interface
59   //===--------------------------------------------------------------------===//
60
61   /// SetBufferSize - Set the internal buffer size to the specified amount
62   /// instead of the default.
63   void SetBufferSize(unsigned Size=4096) {
64     assert(Size >= 64 &&
65            "Buffer size must be somewhat large for invariants to hold");
66     flush();
67
68     delete [] OutBufStart;
69     OutBufStart = new char[Size];
70     OutBufEnd = OutBufStart+Size;
71     OutBufCur = OutBufStart;
72     Unbuffered = false;
73   }
74
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() {
80     flush();
81     
82     delete [] OutBufStart;
83     OutBufStart = OutBufEnd = OutBufCur = 0;
84     Unbuffered = true;
85   }
86
87   unsigned GetNumBytesInBuffer() const {
88     return OutBufCur - OutBufStart;
89   }
90
91   //===--------------------------------------------------------------------===//
92   // Data Output Interface
93   //===--------------------------------------------------------------------===//
94
95   void flush() {
96     if (OutBufCur != OutBufStart)
97       flush_nonempty();
98   }
99
100   raw_ostream &operator<<(char C) {
101     if (OutBufCur >= OutBufEnd)
102       return write(C);
103     *OutBufCur++ = C;
104     return *this;
105   }
106
107   raw_ostream &operator<<(unsigned char C) {
108     if (OutBufCur >= OutBufEnd)
109       return write(C);
110     *OutBufCur++ = C;
111     return *this;
112   }
113
114   raw_ostream &operator<<(signed char C) {
115     if (OutBufCur >= OutBufEnd)
116       return write(C);
117     *OutBufCur++ = C;
118     return *this;
119   }
120
121   raw_ostream &operator<<(const char *Str) {
122     // Inline fast path, particulary for constant strings where a
123     // sufficiently smart compiler will simplify strlen.
124
125     unsigned Size = strlen(Str);
126
127     // Make sure we can use the fast path.
128     if (OutBufCur+Size > OutBufEnd)
129       return write(Str, Size);
130
131     memcpy(OutBufCur, Str, Size);
132     OutBufCur += Size;
133     return *this;
134   }
135
136   raw_ostream &operator<<(const std::string& Str) {
137     write(Str.data(), Str.length());
138     return *this;
139   }
140
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));
148     return *this;
149   }
150
151   raw_ostream &operator<<(int N) {
152     this->operator<<(static_cast<long>(N));
153     return *this;
154   }
155
156   raw_ostream &operator<<(double N) {
157     this->operator<<(ftostr(N));
158     return *this;
159   }
160
161   raw_ostream &write(unsigned char C);
162   raw_ostream &write(const char *Ptr, unsigned Size);
163
164   // Formatted output, see the format() function in Support/Format.h.
165   raw_ostream &operator<<(const format_object_base &Fmt);
166
167   //===--------------------------------------------------------------------===//
168   // Subclass Interface
169   //===--------------------------------------------------------------------===//
170
171 private:
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.
175   /// 
176   /// \invariant { Size > 0 }
177   virtual void write_impl(const char *Ptr, unsigned Size) = 0;
178
179   // An out of line virtual method to provide a home for the class vtable.
180   virtual void handle();
181
182   //===--------------------------------------------------------------------===//
183   // Private Interface
184   //===--------------------------------------------------------------------===//
185 private:
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();
190 };
191
192 //===----------------------------------------------------------------------===//
193 // File Output Streams
194 //===----------------------------------------------------------------------===//
195
196 /// raw_fd_ostream - A raw_ostream that writes to a file descriptor.
197 ///
198 class raw_fd_ostream : public raw_ostream {
199   int FD;
200   bool ShouldClose;
201   uint64_t pos;
202
203   /// write_impl - See raw_ostream::write_impl.
204   virtual void write_impl(const char *Ptr, unsigned Size);
205 public:
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.
210   ///
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);
216
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) {}
222   
223   ~raw_fd_ostream();
224
225   /// close - Manually flush the stream and close the file.
226   void close();
227
228   /// tell - Return the current offset with the file.
229   uint64_t tell() { return pos + GetNumBytesInBuffer(); }
230
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);
234 };
235
236 /// raw_stdout_ostream - This is a stream that always prints to stdout.
237 ///
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();
241 public:
242   raw_stdout_ostream();
243 };
244
245 /// raw_stderr_ostream - This is a stream that always prints to stderr.
246 ///
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();
250 public:
251   raw_stderr_ostream();
252 };
253
254 /// outs() - This returns a reference to a raw_ostream for standard output.
255 /// Use it like: outs() << "foo" << "bar";
256 raw_ostream &outs();
257
258 /// errs() - This returns a reference to a raw_ostream for standard error.
259 /// Use it like: errs() << "foo" << "bar";
260 raw_ostream &errs();
261
262
263 //===----------------------------------------------------------------------===//
264 // Output Stream Adaptors
265 //===----------------------------------------------------------------------===//
266
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 {
270   std::ostream &OS;
271
272   /// write_impl - See raw_ostream::write_impl.
273   virtual void write_impl(const char *Ptr, unsigned Size);
274 public:
275   raw_os_ostream(std::ostream &O) : OS(O) {}
276   ~raw_os_ostream();
277 };
278
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 {
282   std::string &OS;
283
284   /// write_impl - See raw_ostream::write_impl.
285   virtual void write_impl(const char *Ptr, unsigned Size);
286 public:
287   raw_string_ostream(std::string &O) : OS(O) {}
288   ~raw_string_ostream();
289
290   /// str - Flushes the stream contents to the target string and returns
291   ///  the string's reference.
292   std::string& str() {
293     flush();
294     return OS;
295   }
296 };
297
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;
302
303   /// write_impl - See raw_ostream::write_impl.
304   virtual void write_impl(const char *Ptr, unsigned Size);
305 public:
306   raw_svector_ostream(SmallVectorImpl<char> &O) : OS(O) {}
307   ~raw_svector_ostream();
308 };
309
310 } // end llvm namespace
311
312 #endif