raw_ostream: Lift out flush_nonempty.
[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   /// \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. }
37
38   char *OutBufStart, *OutBufEnd, *OutBufCur;
39   bool Unbuffered;
40
41 public:
42   explicit raw_ostream(bool unbuffered=false) : Unbuffered(unbuffered) {
43     // Start out ready to flush.
44     OutBufStart = OutBufEnd = OutBufCur = 0;
45   }
46
47   virtual ~raw_ostream() {
48     delete [] OutBufStart;
49   }
50
51   //===--------------------------------------------------------------------===//
52   // Configuration Interface
53   //===--------------------------------------------------------------------===//
54
55   /// SetBufferSize - Set the internal buffer size to the specified amount
56   /// instead of the default.
57   void SetBufferSize(unsigned Size) {
58     assert(Size >= 64 &&
59            "Buffer size must be somewhat large for invariants to hold");
60     flush();
61
62     delete [] OutBufStart;
63     OutBufStart = new char[Size];
64     OutBufEnd = OutBufStart+Size;
65     OutBufCur = OutBufStart;
66   }
67
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;
74     if (Unbuffered)
75       flush();
76   }
77
78   //===--------------------------------------------------------------------===//
79   // Data Output Interface
80   //===--------------------------------------------------------------------===//
81
82   void flush() {
83     if (OutBufCur != OutBufStart)
84       flush_nonempty();
85   }
86
87   raw_ostream &operator<<(char C) {
88     if (OutBufCur >= OutBufEnd)
89       return write(C);
90     *OutBufCur++ = C;
91     if (Unbuffered)
92       flush_nonempty();
93     return *this;
94   }
95
96   raw_ostream &operator<<(unsigned char C) {
97     if (OutBufCur >= OutBufEnd)
98       return write(C);
99     *OutBufCur++ = C;
100     if (Unbuffered)
101       flush_nonempty();
102     return *this;
103   }
104
105   raw_ostream &operator<<(signed char C) {
106     if (OutBufCur >= OutBufEnd)
107       return write(C);
108     *OutBufCur++ = C;
109     if (Unbuffered)
110       flush_nonempty();
111     return *this;
112   }
113
114   raw_ostream &operator<<(const char *Str) {
115     return write(Str, strlen(Str));
116   }
117
118   raw_ostream &operator<<(const std::string& Str) {
119     return write(Str.data(), Str.length());
120   }
121
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));
129   }
130
131   raw_ostream &operator<<(int N) {
132     return this->operator<<(static_cast<long>(N));
133   }
134
135   raw_ostream &operator<<(double N) {
136     return this->operator<<(ftostr(N));
137   }
138
139   raw_ostream &write(unsigned char C);
140   raw_ostream &write(const char *Ptr, unsigned Size);
141
142   // Formatted output, see the format() function in Support/Format.h.
143   raw_ostream &operator<<(const format_object_base &Fmt);
144
145   //===--------------------------------------------------------------------===//
146   // Subclass Interface
147   //===--------------------------------------------------------------------===//
148
149 private:
150   /// flush_impl - The is the piece of the class that is implemented by
151   /// subclasses.  This only outputs the currently buffered data.
152   ///
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;
156
157   // An out of line virtual method to provide a home for the class vtable.
158   virtual void handle();
159
160   //===--------------------------------------------------------------------===//
161   // Private Interface
162   //===--------------------------------------------------------------------===//
163 private:
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();
168 };
169
170 //===----------------------------------------------------------------------===//
171 // File Output Streams
172 //===----------------------------------------------------------------------===//
173
174 /// raw_fd_ostream - A raw_ostream that writes to a file descriptor.
175 ///
176 class raw_fd_ostream : public raw_ostream {
177   int FD;
178   bool ShouldClose;
179   uint64_t pos;
180 public:
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.
185   ///
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);
191
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) {}
197   
198   ~raw_fd_ostream();
199
200   /// flush_impl - The is the piece of the class that is implemented by
201   /// subclasses.  This only outputs the currently buffered data.
202   ///
203   /// raw_ostream guarantees to only call this routine when there is
204   /// buffered data, i.e. OutBufStart != OutBufCur.
205   virtual void flush_impl();
206
207   /// close - Manually flush the stream and close the file.
208   void close();
209
210   /// tell - Return the current offset with the file.
211   uint64_t tell() {
212     return pos + (OutBufCur - OutBufStart);
213   }
214
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);
218 };
219
220 /// raw_stdout_ostream - This is a stream that always prints to stdout.
221 ///
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();
225 public:
226   raw_stdout_ostream();
227 };
228
229 /// raw_stderr_ostream - This is a stream that always prints to stderr.
230 ///
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();
234 public:
235   raw_stderr_ostream();
236 };
237
238 /// outs() - This returns a reference to a raw_ostream for standard output.
239 /// Use it like: outs() << "foo" << "bar";
240 raw_ostream &outs();
241
242 /// errs() - This returns a reference to a raw_ostream for standard error.
243 /// Use it like: errs() << "foo" << "bar";
244 raw_ostream &errs();
245
246
247 //===----------------------------------------------------------------------===//
248 // Output Stream Adaptors
249 //===----------------------------------------------------------------------===//
250
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 {
254   std::ostream &OS;
255 public:
256   raw_os_ostream(std::ostream &O) : OS(O) {}
257   ~raw_os_ostream();
258
259   /// flush_impl - The is the piece of the class that is implemented by
260   /// subclasses.  This only outputs the currently buffered data.
261   ///
262   /// raw_ostream guarantees to only call this routine when there is
263   /// buffered data, i.e. OutBufStart != OutBufCur.
264   virtual void flush_impl();
265 };
266
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 {
270   std::string &OS;
271 public:
272   raw_string_ostream(std::string &O) : OS(O) {}
273   ~raw_string_ostream();
274
275   /// str - Flushes the stream contents to the target string and returns
276   ///  the string's reference.
277   std::string& str() {
278     flush();
279     return OS;
280   }
281
282   /// flush_impl - The is the piece of the class that is implemented by
283   /// subclasses.  This only outputs the currently buffered data.
284   ///
285   /// raw_ostream guarantees to only call this routine when there is
286   /// buffered data, i.e. OutBufStart != OutBufCur.
287   virtual void flush_impl();
288 };
289
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;
294 public:
295   raw_svector_ostream(SmallVectorImpl<char> &O) : OS(O) {}
296   ~raw_svector_ostream();
297
298   /// flush_impl - The is the piece of the class that is implemented by
299   /// subclasses.  This only outputs the currently buffered data.
300   ///
301   /// raw_ostream guarantees to only call this routine when there is
302   /// buffered data, i.e. OutBufStart != OutBufCur.
303   virtual void flush_impl();
304 };
305
306 } // end llvm namespace
307
308 #endif