Fix build on GCC 4.3.
[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 <cassert>
18 #include <cstring>
19 #include <string>
20 #include <iosfwd>
21
22 namespace llvm {
23
24 /// raw_ostream - This class implements an extremely fast bulk output stream
25 /// that can *only* output to a stream.  It does not support seeking, reopening,
26 /// rewinding, line buffered disciplines etc. It is a simple buffer that outputs
27 /// a chunk at a time.
28 class raw_ostream {
29 protected:
30   char *OutBufStart, *OutBufEnd, *OutBufCur;
31 public:
32   raw_ostream() {
33     // Start out ready to flush.
34     OutBufStart = OutBufEnd = OutBufCur = 0;
35   }
36   virtual ~raw_ostream() {}
37   
38   //===--------------------------------------------------------------------===//
39   // Configuration Interface
40   //===--------------------------------------------------------------------===//
41   
42   /// SetBufferSize - Set the internal buffer size to the specified amount
43   /// instead of the default.
44   void SetBufferSize(unsigned Size) {
45     assert(Size >= 64 &&
46            "Buffer size must be somewhat large for invariants to hold");
47     flush();
48     
49     delete [] OutBufStart;
50     OutBufStart = new char[Size];
51     OutBufEnd = OutBufStart+Size;
52     OutBufCur = OutBufStart;
53   }
54   
55   //===--------------------------------------------------------------------===//
56   // Data Output Interface
57   //===--------------------------------------------------------------------===//
58   
59   void flush() {
60     if (OutBufCur != OutBufStart)
61       flush_impl();
62   }
63   
64   raw_ostream &operator<<(char C) {
65     if (OutBufCur >= OutBufEnd)
66       flush_impl();
67     *OutBufCur++ = C;
68     return *this;
69   }
70   
71   raw_ostream &operator<<(const char *Str) {
72     return write(Str, strlen(Str));
73   }
74   
75   raw_ostream &write(const char *Ptr, unsigned Size) {
76     if (OutBufCur+Size > OutBufEnd)
77       flush_impl();
78     
79     // Handle short strings specially, memcpy isn't very good at very short
80     // strings.
81     switch (Size) {
82     case 4: OutBufCur[3] = Ptr[3]; // FALL THROUGH
83     case 3: OutBufCur[2] = Ptr[2]; // FALL THROUGH
84     case 2: OutBufCur[1] = Ptr[1]; // FALL THROUGH
85     case 1: OutBufCur[0] = Ptr[0]; // FALL THROUGH
86     case 0: break;
87     default:
88       // Normally the string to emit is shorter than the buffer.
89       if (Size <= unsigned(OutBufEnd-OutBufStart)) {
90         memcpy(OutBufCur, Ptr, Size);
91         break;
92       }
93
94       // If emitting a string larger than our buffer, emit in chunks.  In this
95       // case we know that we just flushed the buffer.
96       while (Size) {
97         unsigned NumToEmit = OutBufEnd-OutBufStart;
98         if (Size < NumToEmit) NumToEmit = Size;
99         assert(OutBufCur == OutBufStart);
100         memcpy(OutBufStart, Ptr, NumToEmit);
101         Ptr += NumToEmit;
102         OutBufCur = OutBufStart + NumToEmit;
103         flush_impl();
104       }
105       break;
106     }
107     OutBufCur += Size;
108     return *this;
109   }
110   
111   //===--------------------------------------------------------------------===//
112   // Subclass Interface
113   //===--------------------------------------------------------------------===//
114
115 protected:
116   
117   /// flush_impl - The is the piece of the class that is implemented by
118   /// subclasses.  This outputs the currently buffered data and resets the
119   /// buffer to empty.
120   virtual void flush_impl() = 0;
121   
122   /// HandleFlush - A stream's implementation of flush should call this after
123   /// emitting the bytes to the data sink.
124   void HandleFlush() {
125     if (OutBufStart == 0)
126       SetBufferSize(4096);
127     OutBufCur = OutBufStart;
128   }
129 private:
130   // An out of line virtual method to provide a home for the class vtable.
131   virtual void handle();
132 };
133   
134 /// raw_fd_ostream - A raw_ostream that writes to a file descriptor.
135 ///
136 class raw_fd_ostream : public raw_ostream {
137   int FD;
138   bool ShouldClose;
139 public:
140   /// raw_fd_ostream - Open the specified file for writing.  If an error occurs,
141   /// information about the error is put into ErrorInfo, and the stream should
142   /// be immediately destroyed.
143   raw_fd_ostream(const char *Filename, std::string &ErrorInfo);
144   
145   /// raw_fd_ostream ctor - FD is the file descriptor that this writes to.  If
146   /// ShouldClose is true, this closes the file when 
147   raw_fd_ostream(int fd, bool shouldClose) : FD(fd), ShouldClose(shouldClose) {}
148   
149   ~raw_fd_ostream();
150     
151   /// flush_impl - The is the piece of the class that is implemented by
152   /// subclasses.  This outputs the currently buffered data and resets the
153   /// buffer to empty.
154   virtual void flush_impl();
155 };
156   
157 /// raw_stdout_ostream - This is a stream that always prints to stdout.
158 ///
159 class raw_stdout_ostream : public raw_fd_ostream {
160   // An out of line virtual method to provide a home for the class vtable.
161   virtual void handle();
162 public:
163   raw_stdout_ostream();
164 };
165
166 /// raw_stderr_ostream - This is a stream that always prints to stderr.
167 ///
168 class raw_stderr_ostream : public raw_fd_ostream {
169   // An out of line virtual method to provide a home for the class vtable.
170   virtual void handle();
171 public:
172   raw_stderr_ostream();
173 };
174   
175 /// outs() - This returns a reference to a raw_ostream for standard output.
176 /// Use it like: outs() << "foo" << "bar";
177 raw_ostream &outs();
178
179 /// errs() - This returns a reference to a raw_ostream for standard error.
180 /// Use it like: errs() << "foo" << "bar";
181 raw_ostream &errs();
182   
183   
184 /// raw_os_ostream - A raw_ostream that writes to an std::ostream.  This is a
185 /// simple adaptor class.
186 class raw_os_ostream : public raw_ostream {
187   std::ostream &OS;
188 public:
189   raw_os_ostream(std::ostream &O) : OS(O) {}
190   
191   /// flush_impl - The is the piece of the class that is implemented by
192   /// subclasses.  This outputs the currently buffered data and resets the
193   /// buffer to empty.
194   virtual void flush_impl();
195 };
196   
197 } // end llvm namespace
198
199 #endif