add a simple mechanism for formatted output. This gives raw_ostream's
[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   
26 /// raw_ostream - This class implements an extremely fast bulk output stream
27 /// that can *only* output to a stream.  It does not support seeking, reopening,
28 /// rewinding, line buffered disciplines etc. It is a simple buffer that outputs
29 /// a chunk at a time.
30 class raw_ostream {
31 protected:
32   char *OutBufStart, *OutBufEnd, *OutBufCur;
33 public:
34   raw_ostream() {
35     // Start out ready to flush.
36     OutBufStart = OutBufEnd = OutBufCur = 0;
37   }
38   virtual ~raw_ostream() {}
39   
40   //===--------------------------------------------------------------------===//
41   // Configuration Interface
42   //===--------------------------------------------------------------------===//
43   
44   /// SetBufferSize - Set the internal buffer size to the specified amount
45   /// instead of the default.
46   void SetBufferSize(unsigned Size) {
47     assert(Size >= 64 &&
48            "Buffer size must be somewhat large for invariants to hold");
49     flush();
50     
51     delete [] OutBufStart;
52     OutBufStart = new char[Size];
53     OutBufEnd = OutBufStart+Size;
54     OutBufCur = OutBufStart;
55   }
56   
57   //===--------------------------------------------------------------------===//
58   // Data Output Interface
59   //===--------------------------------------------------------------------===//
60   
61   void flush() {
62     if (OutBufCur != OutBufStart)
63       flush_impl();
64   }
65   
66   raw_ostream &operator<<(char C) {
67     if (OutBufCur >= OutBufEnd)
68       flush_impl();
69     *OutBufCur++ = C;
70     return *this;
71   }
72   
73   raw_ostream &operator<<(unsigned char C) {
74     if (OutBufCur >= OutBufEnd)
75       flush_impl();
76     *OutBufCur++ = C;
77     return *this;
78   }
79   
80   raw_ostream &operator<<(signed char C) {
81     if (OutBufCur >= OutBufEnd)
82       flush_impl();
83     *OutBufCur++ = C;
84     return *this;
85   }
86   
87   raw_ostream &operator<<(const char *Str) {
88     return write(Str, strlen(Str));
89   }
90   
91   raw_ostream &operator<<(const std::string& Str) {
92     return write(Str.data(), Str.length());
93   }
94   
95   raw_ostream &operator<<(unsigned long N);
96   raw_ostream &operator<<(long N);
97   raw_ostream &operator<<(unsigned long long N);
98   raw_ostream &operator<<(long long N);
99   
100   raw_ostream &operator<<(unsigned int N) {
101     return this->operator<<(static_cast<unsigned long>(N));
102   }
103   
104   raw_ostream &operator<<(int N) {
105     return this->operator<<(static_cast<long>(N));
106   }
107
108   raw_ostream &operator<<(double N) {
109     return this->operator<<(ftostr(N));
110   }
111   
112   raw_ostream &write(const char *Ptr, unsigned Size);
113   
114   // Formatted output, see the format() function below.
115   raw_ostream &operator<<(const format_object_base &Fmt);
116   
117   //===--------------------------------------------------------------------===//
118   // Subclass Interface
119   //===--------------------------------------------------------------------===//
120
121 protected:
122   
123   /// flush_impl - The is the piece of the class that is implemented by
124   /// subclasses.  This outputs the currently buffered data and resets the
125   /// buffer to empty.
126   virtual void flush_impl() = 0;
127   
128   /// HandleFlush - A stream's implementation of flush should call this after
129   /// emitting the bytes to the data sink.
130   void HandleFlush() {
131     if (OutBufStart == 0)
132       SetBufferSize(4096);
133     OutBufCur = OutBufStart;
134   }
135 private:
136   // An out of line virtual method to provide a home for the class vtable.
137   virtual void handle();
138 };
139   
140 //===----------------------------------------------------------------------===//
141 // Formatted Output
142 //===----------------------------------------------------------------------===//
143
144 /// format_object_base - This is a helper class used for handling formatted
145 /// output.  It is the abstract base class of a templated derived class.
146 class format_object_base {
147 protected:
148   const char *Fmt;
149   virtual void home(); // Out of line virtual method.
150 public:
151   format_object_base(const char *fmt) : Fmt(fmt) {}
152   virtual ~format_object_base() {}
153   
154   /// print - Format the object into the specified buffer.  On success, this
155   /// returns the length of the formatted string.  If the buffer is too small,
156   /// this returns a length to retry with, which will be larger than BufferSize.
157   virtual unsigned print(char *Buffer, unsigned BufferSize) const = 0;
158 };
159   
160 /// format_object - This is a templated helper class used by the format function
161 /// that captures the object to be formated and the format string.  When
162 /// actually printed, this synthesizes the string into a temporary buffer
163 /// provided and returns whether or not it is big enough.
164 template <typename T>
165   class format_object : public format_object_base {
166   T Val;
167 public:
168   format_object(const char *fmt, const T &val)
169     : format_object_base(fmt), Val(val) {
170   }
171   
172   /// print - Format the object into the specified buffer.  On success, this
173   /// returns the length of the formatted string.  If the buffer is too small,
174   /// this returns a length to retry with, which will be larger than BufferSize.
175   virtual unsigned print(char *Buffer, unsigned BufferSize) const {
176     int N = snprintf(Buffer, BufferSize-1, Fmt, Val);
177     if (N < 0)             // VC++ and old GlibC return negative on overflow.
178       return BufferSize*2;
179     if (unsigned(N) >= BufferSize-1)// Other impls yield number of bytes needed.
180       return N+1;
181     // If N is positive and <= BufferSize-1, then the string fit, yay.
182     return N;
183   }
184 };
185
186 /// format - This is a helper function that is used to produce formatted output.
187 /// This is typically used like:  OS << format("%0.4f", myfloat) << '\n';
188 template <typename T>
189 inline format_object<T> format(const char *Fmt, const T &Val) {
190   return format_object<T>(Fmt, Val);
191 }
192   
193 //===----------------------------------------------------------------------===//
194 // File Output Streams
195 //===----------------------------------------------------------------------===//
196   
197 /// raw_fd_ostream - A raw_ostream that writes to a file descriptor.
198 ///
199 class raw_fd_ostream : public raw_ostream {
200   int FD;
201   bool ShouldClose;
202 public:
203   /// raw_fd_ostream - Open the specified file for writing.  If an error occurs,
204   /// information about the error is put into ErrorInfo, and the stream should
205   /// be immediately destroyed.
206   raw_fd_ostream(const char *Filename, std::string &ErrorInfo);
207   
208   /// raw_fd_ostream ctor - FD is the file descriptor that this writes to.  If
209   /// ShouldClose is true, this closes the file when 
210   raw_fd_ostream(int fd, bool shouldClose) : FD(fd), ShouldClose(shouldClose) {}
211   
212   ~raw_fd_ostream();
213     
214   /// flush_impl - The is the piece of the class that is implemented by
215   /// subclasses.  This outputs the currently buffered data and resets the
216   /// buffer to empty.
217   virtual void flush_impl();
218 };
219   
220 /// raw_stdout_ostream - This is a stream that always prints to stdout.
221 ///
222 class raw_stdout_ostream : public raw_fd_ostream {
223   // An out of line virtual method to provide a home for the class vtable.
224   virtual void handle();
225 public:
226   raw_stdout_ostream();
227 };
228
229 /// raw_stderr_ostream - This is a stream that always prints to stderr.
230 ///
231 class raw_stderr_ostream : public raw_fd_ostream {
232   // An out of line virtual method to provide a home for the class vtable.
233   virtual void handle();
234 public:
235   raw_stderr_ostream();
236 };
237   
238 /// outs() - This returns a reference to a raw_ostream for standard output.
239 /// Use it like: outs() << "foo" << "bar";
240 raw_ostream &outs();
241
242 /// errs() - This returns a reference to a raw_ostream for standard error.
243 /// Use it like: errs() << "foo" << "bar";
244 raw_ostream &errs();
245   
246   
247 //===----------------------------------------------------------------------===//
248 // Bridge Output Streams
249 //===----------------------------------------------------------------------===//
250   
251 /// raw_os_ostream - A raw_ostream that writes to an std::ostream.  This is a
252 /// simple adaptor class.
253 class raw_os_ostream : public raw_ostream {
254   std::ostream &OS;
255 public:
256   raw_os_ostream(std::ostream &O) : OS(O) {}
257   
258   /// flush_impl - The is the piece of the class that is implemented by
259   /// subclasses.  This outputs the currently buffered data and resets the
260   /// buffer to empty.
261   virtual void flush_impl();
262 };
263   
264 } // end llvm namespace
265
266 #endif