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