5ab0988d3eea107831ca562a44e95fb862132fb7
[oota-llvm.git] / include / llvm / Support / raw_ostream.h
1 //===--- raw_ostream.h - Raw output stream --------------------------------===//
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 public:
36   raw_ostream() {
37     // Start out ready to flush.
38     OutBufStart = OutBufEnd = OutBufCur = 0;
39   }
40
41   virtual ~raw_ostream() {
42     delete [] OutBufStart;
43   }
44   
45   //===--------------------------------------------------------------------===//
46   // Configuration Interface
47   //===--------------------------------------------------------------------===//
48   
49   /// SetBufferSize - Set the internal buffer size to the specified amount
50   /// instead of the default.
51   void SetBufferSize(unsigned Size) {
52     assert(Size >= 64 &&
53            "Buffer size must be somewhat large for invariants to hold");
54     flush();
55     
56     delete [] OutBufStart;
57     OutBufStart = new char[Size];
58     OutBufEnd = OutBufStart+Size;
59     OutBufCur = OutBufStart;
60   }
61   
62   //===--------------------------------------------------------------------===//
63   // Data Output Interface
64   //===--------------------------------------------------------------------===//
65   
66   void flush() {
67     if (OutBufCur != OutBufStart)
68       flush_impl();
69   }
70   
71   raw_ostream &operator<<(char C) {
72     if (OutBufCur >= OutBufEnd)
73       flush_impl();
74     *OutBufCur++ = C;
75     return *this;
76   }
77   
78   raw_ostream &operator<<(unsigned char C) {
79     if (OutBufCur >= OutBufEnd)
80       flush_impl();
81     *OutBufCur++ = C;
82     return *this;
83   }
84   
85   raw_ostream &operator<<(signed char C) {
86     if (OutBufCur >= OutBufEnd)
87       flush_impl();
88     *OutBufCur++ = C;
89     return *this;
90   }
91   
92   raw_ostream &operator<<(const char *Str) {
93     return write(Str, strlen(Str));
94   }
95   
96   raw_ostream &operator<<(const std::string& Str) {
97     return write(Str.data(), Str.length());
98   }
99   
100   raw_ostream &operator<<(unsigned long N);
101   raw_ostream &operator<<(long N);
102   raw_ostream &operator<<(unsigned long long N);
103   raw_ostream &operator<<(long long N);
104   raw_ostream &operator<<(const void *P);
105   raw_ostream &operator<<(unsigned int N) {
106     return this->operator<<(static_cast<unsigned long>(N));
107   }
108   
109   raw_ostream &operator<<(int N) {
110     return this->operator<<(static_cast<long>(N));
111   }
112
113   raw_ostream &operator<<(double N) {
114     return this->operator<<(ftostr(N));
115   }
116   
117   raw_ostream &write(const char *Ptr, unsigned Size);
118   
119   // Formatted output, see the format() function in Support/Format.h.
120   raw_ostream &operator<<(const format_object_base &Fmt);
121   
122   //===--------------------------------------------------------------------===//
123   // Subclass Interface
124   //===--------------------------------------------------------------------===//
125
126 protected:
127   
128   /// flush_impl - The is the piece of the class that is implemented by
129   /// subclasses.  This outputs the currently buffered data and resets the
130   /// buffer to empty.
131   virtual void flush_impl() = 0;
132   
133   /// HandleFlush - A stream's implementation of flush should call this after
134   /// emitting the bytes to the data sink.
135   void HandleFlush() {
136     if (OutBufStart == 0)
137       SetBufferSize(4096);
138     OutBufCur = OutBufStart;
139   }
140 private:
141   // An out of line virtual method to provide a home for the class vtable.
142   virtual void handle();
143 };
144   
145 //===----------------------------------------------------------------------===//
146 // File Output Streams
147 //===----------------------------------------------------------------------===//
148   
149 /// raw_fd_ostream - A raw_ostream that writes to a file descriptor.
150 ///
151 class raw_fd_ostream : public raw_ostream {
152   int FD;
153   bool ShouldClose;
154 public:
155   /// raw_fd_ostream - Open the specified file for writing.  If an error occurs,
156   /// information about the error is put into ErrorInfo, and the stream should
157   /// be immediately destroyed.
158   raw_fd_ostream(const char *Filename, std::string &ErrorInfo);
159   
160   /// raw_fd_ostream ctor - FD is the file descriptor that this writes to.  If
161   /// ShouldClose is true, this closes the file when 
162   raw_fd_ostream(int fd, bool shouldClose) : FD(fd), ShouldClose(shouldClose) {}
163   
164   ~raw_fd_ostream();
165     
166   /// flush_impl - The is the piece of the class that is implemented by
167   /// subclasses.  This outputs the currently buffered data and resets the
168   /// buffer to empty.
169   virtual void flush_impl();
170 };
171   
172 /// raw_stdout_ostream - This is a stream that always prints to stdout.
173 ///
174 class raw_stdout_ostream : public raw_fd_ostream {
175   // An out of line virtual method to provide a home for the class vtable.
176   virtual void handle();
177 public:
178   raw_stdout_ostream();
179 };
180
181 /// raw_stderr_ostream - This is a stream that always prints to stderr.
182 ///
183 class raw_stderr_ostream : public raw_fd_ostream {
184   // An out of line virtual method to provide a home for the class vtable.
185   virtual void handle();
186 public:
187   raw_stderr_ostream();
188 };
189   
190 /// outs() - This returns a reference to a raw_ostream for standard output.
191 /// Use it like: outs() << "foo" << "bar";
192 raw_ostream &outs();
193
194 /// errs() - This returns a reference to a raw_ostream for standard error.
195 /// Use it like: errs() << "foo" << "bar";
196 raw_ostream &errs();
197   
198   
199 //===----------------------------------------------------------------------===//
200 // Output Stream Adaptors
201 //===----------------------------------------------------------------------===//
202   
203 /// raw_os_ostream - A raw_ostream that writes to an std::ostream.  This is a
204 /// simple adaptor class.
205 class raw_os_ostream : public raw_ostream {
206   std::ostream &OS;
207 public:
208   raw_os_ostream(std::ostream &O) : OS(O) {}
209   ~raw_os_ostream();
210
211   /// flush_impl - The is the piece of the class that is implemented by
212   /// subclasses.  This outputs the currently buffered data and resets the
213   /// buffer to empty.
214   virtual void flush_impl();
215 };
216
217 /// raw_string_ostream - A raw_ostream that writes to an std::string.  This is a
218 /// simple adaptor class.
219 class raw_string_ostream : public raw_ostream {
220   std::string &OS;
221 public:
222   raw_string_ostream(std::string &O) : OS(O) {}
223   ~raw_string_ostream();
224   
225   /// str - Flushes the stream contents to the target string and returns
226   ///  the string's reference.
227   std::string& str() {
228     flush();
229     return OS;
230   }
231   
232   /// flush_impl - The is the piece of the class that is implemented by
233   /// subclasses.  This outputs the currently buffered data and resets the
234   /// buffer to empty.
235   virtual void flush_impl();
236 };
237   
238 /// raw_svector_ostream - A raw_ostream that writes to an SmallVector or
239 /// SmallString.  This is a simple adaptor class.
240 class raw_svector_ostream : public raw_ostream {
241   SmallVectorImpl<char> &OS;
242 public:
243   raw_svector_ostream(SmallVectorImpl<char> &O) : OS(O) {}
244   ~raw_svector_ostream();
245   
246   /// flush_impl - The is the piece of the class that is implemented by
247   /// subclasses.  This outputs the currently buffered data and resets the
248   /// buffer to empty.
249   virtual void flush_impl();
250 };
251   
252 } // end llvm namespace
253
254 #endif