aa6a8b9f9fea20355710cbbc32814a075faa0d40
[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   /// \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   unsigned GetNumBytesInBuffer() const {
79     return OutBufCur - OutBufStart;
80   }
81
82   //===--------------------------------------------------------------------===//
83   // Data Output Interface
84   //===--------------------------------------------------------------------===//
85
86   void flush() {
87     if (OutBufCur != OutBufStart)
88       flush_nonempty();
89   }
90
91   raw_ostream &operator<<(char C) {
92     if (OutBufCur >= OutBufEnd)
93       return write(C);
94     *OutBufCur++ = C;
95     if (Unbuffered)
96       flush_nonempty();
97     return *this;
98   }
99
100   raw_ostream &operator<<(unsigned char C) {
101     if (OutBufCur >= OutBufEnd)
102       return write(C);
103     *OutBufCur++ = C;
104     if (Unbuffered)
105       flush_nonempty();
106     return *this;
107   }
108
109   raw_ostream &operator<<(signed char C) {
110     if (OutBufCur >= OutBufEnd)
111       return write(C);
112     *OutBufCur++ = C;
113     if (Unbuffered)
114       flush_nonempty();
115     return *this;
116   }
117
118   raw_ostream &operator<<(const char *Str) {
119     return write(Str, strlen(Str));
120   }
121
122   raw_ostream &operator<<(const std::string& Str) {
123     return write(Str.data(), Str.length());
124   }
125
126   raw_ostream &operator<<(unsigned long N);
127   raw_ostream &operator<<(long N);
128   raw_ostream &operator<<(unsigned long long N);
129   raw_ostream &operator<<(long long N);
130   raw_ostream &operator<<(const void *P);
131   raw_ostream &operator<<(unsigned int N) {
132     return this->operator<<(static_cast<unsigned long>(N));
133   }
134
135   raw_ostream &operator<<(int N) {
136     return this->operator<<(static_cast<long>(N));
137   }
138
139   raw_ostream &operator<<(double N) {
140     return this->operator<<(ftostr(N));
141   }
142
143   raw_ostream &write(unsigned char C);
144   raw_ostream &write(const char *Ptr, unsigned Size);
145
146   // Formatted output, see the format() function in Support/Format.h.
147   raw_ostream &operator<<(const format_object_base &Fmt);
148
149   //===--------------------------------------------------------------------===//
150   // Subclass Interface
151   //===--------------------------------------------------------------------===//
152
153 private:
154   /// write_impl - The is the piece of the class that is implemented
155   /// by subclasses.  This writes the \args Size bytes starting at
156   /// \arg Ptr to the underlying stream.
157   /// 
158   /// \invariant { Size > 0 }
159   virtual void write_impl(const char *Ptr, unsigned Size) = 0;
160
161   // An out of line virtual method to provide a home for the class vtable.
162   virtual void handle();
163
164   //===--------------------------------------------------------------------===//
165   // Private Interface
166   //===--------------------------------------------------------------------===//
167 private:
168   /// flush_nonempty - Flush the current buffer, which is known to be
169   /// non-empty. This outputs the currently buffered data and resets
170   /// the buffer to empty.
171   void flush_nonempty();
172 };
173
174 //===----------------------------------------------------------------------===//
175 // File Output Streams
176 //===----------------------------------------------------------------------===//
177
178 /// raw_fd_ostream - A raw_ostream that writes to a file descriptor.
179 ///
180 class raw_fd_ostream : public raw_ostream {
181   int FD;
182   bool ShouldClose;
183   uint64_t pos;
184
185   /// write_impl - See raw_ostream::write_impl.
186   virtual void write_impl(const char *Ptr, unsigned Size);
187 public:
188   /// raw_fd_ostream - Open the specified file for writing. If an
189   /// error occurs, information about the error is put into ErrorInfo,
190   /// and the stream should be immediately destroyed; the string will
191   /// be empty if no error occurred.
192   ///
193   /// \param Filename - The file to open. If this is "-" then the
194   /// stream will use stdout instead.
195   /// \param Binary - The file should be opened in binary mode on
196   /// platforms that support this distinction.
197   raw_fd_ostream(const char *Filename, bool Binary, std::string &ErrorInfo);
198
199   /// raw_fd_ostream ctor - FD is the file descriptor that this writes to.  If
200   /// ShouldClose is true, this closes the file when the stream is destroyed.
201   raw_fd_ostream(int fd, bool shouldClose, 
202                  bool unbuffered=false) : raw_ostream(unbuffered), FD(fd), 
203                                           ShouldClose(shouldClose) {}
204   
205   ~raw_fd_ostream();
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() { return pos + GetNumBytesInBuffer(); }
212
213   /// seek - Flushes the stream and repositions the underlying file descriptor
214   ///  positition to the offset specified from the beginning of the file.
215   uint64_t seek(uint64_t off);
216 };
217
218 /// raw_stdout_ostream - This is a stream that always prints to stdout.
219 ///
220 class raw_stdout_ostream : public raw_fd_ostream {
221   // An out of line virtual method to provide a home for the class vtable.
222   virtual void handle();
223 public:
224   raw_stdout_ostream();
225 };
226
227 /// raw_stderr_ostream - This is a stream that always prints to stderr.
228 ///
229 class raw_stderr_ostream : public raw_fd_ostream {
230   // An out of line virtual method to provide a home for the class vtable.
231   virtual void handle();
232 public:
233   raw_stderr_ostream();
234 };
235
236 /// outs() - This returns a reference to a raw_ostream for standard output.
237 /// Use it like: outs() << "foo" << "bar";
238 raw_ostream &outs();
239
240 /// errs() - This returns a reference to a raw_ostream for standard error.
241 /// Use it like: errs() << "foo" << "bar";
242 raw_ostream &errs();
243
244
245 //===----------------------------------------------------------------------===//
246 // Output Stream Adaptors
247 //===----------------------------------------------------------------------===//
248
249 /// raw_os_ostream - A raw_ostream that writes to an std::ostream.  This is a
250 /// simple adaptor class.
251 class raw_os_ostream : public raw_ostream {
252   std::ostream &OS;
253
254   /// write_impl - See raw_ostream::write_impl.
255   virtual void write_impl(const char *Ptr, unsigned Size);
256 public:
257   raw_os_ostream(std::ostream &O) : OS(O) {}
258   ~raw_os_ostream();
259 };
260
261 /// raw_string_ostream - A raw_ostream that writes to an std::string.  This is a
262 /// simple adaptor class.
263 class raw_string_ostream : public raw_ostream {
264   std::string &OS;
265
266   /// write_impl - See raw_ostream::write_impl.
267   virtual void write_impl(const char *Ptr, unsigned Size);
268 public:
269   raw_string_ostream(std::string &O) : OS(O) {}
270   ~raw_string_ostream();
271
272   /// str - Flushes the stream contents to the target string and returns
273   ///  the string's reference.
274   std::string& str() {
275     flush();
276     return OS;
277   }
278 };
279
280 /// raw_svector_ostream - A raw_ostream that writes to an SmallVector or
281 /// SmallString.  This is a simple adaptor class.
282 class raw_svector_ostream : public raw_ostream {
283   SmallVectorImpl<char> &OS;
284
285   /// write_impl - See raw_ostream::write_impl.
286   virtual void write_impl(const char *Ptr, unsigned Size);
287 public:
288   raw_svector_ostream(SmallVectorImpl<char> &O) : OS(O) {}
289   ~raw_svector_ostream();
290 };
291
292 } // end llvm namespace
293
294 #endif