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