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