Overload for both signed and unsigned char.
[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<<(unsigned char C) {
66     if (OutBufCur >= OutBufEnd)
67       flush_impl();
68     *OutBufCur++ = C;
69     return *this;
70   }
71   
72   raw_ostream &operator<<(signed char C) {
73     if (OutBufCur >= OutBufEnd)
74       flush_impl();
75     *OutBufCur++ = C;
76     return *this;
77   }
78   
79   raw_ostream &operator<<(const char *Str) {
80     return write(Str, strlen(Str));
81   }
82   
83   raw_ostream &operator<<(const std::string& Str) {
84     return write(Str.data(), Str.length());
85   }
86   
87   raw_ostream &operator<<(unsigned long N);
88   
89   raw_ostream &operator<<(long N);
90   
91   raw_ostream &operator<<(unsigned long long N);
92   
93   raw_ostream &operator<<(long long N);
94   
95   raw_ostream &operator<<(unsigned int N) {
96     return this->operator<<(static_cast<unsigned long>(N));
97   }
98   
99   raw_ostream &operator<<(int N) {
100     return this->operator<<(static_cast<long>(N));
101   }
102
103   raw_ostream &operator<<(double N) {
104     return this->operator<<(ftostr(N));
105   }
106   
107   
108   raw_ostream &write(const char *Ptr, unsigned Size);
109   
110   //===--------------------------------------------------------------------===//
111   // Subclass Interface
112   //===--------------------------------------------------------------------===//
113
114 protected:
115   
116   /// flush_impl - The is the piece of the class that is implemented by
117   /// subclasses.  This outputs the currently buffered data and resets the
118   /// buffer to empty.
119   virtual void flush_impl() = 0;
120   
121   /// HandleFlush - A stream's implementation of flush should call this after
122   /// emitting the bytes to the data sink.
123   void HandleFlush() {
124     if (OutBufStart == 0)
125       SetBufferSize(4096);
126     OutBufCur = OutBufStart;
127   }
128 private:
129   // An out of line virtual method to provide a home for the class vtable.
130   virtual void handle();
131 };
132   
133 /// raw_fd_ostream - A raw_ostream that writes to a file descriptor.
134 ///
135 class raw_fd_ostream : public raw_ostream {
136   int FD;
137   bool ShouldClose;
138 public:
139   /// raw_fd_ostream - Open the specified file for writing.  If an error occurs,
140   /// information about the error is put into ErrorInfo, and the stream should
141   /// be immediately destroyed.
142   raw_fd_ostream(const char *Filename, std::string &ErrorInfo);
143   
144   /// raw_fd_ostream ctor - FD is the file descriptor that this writes to.  If
145   /// ShouldClose is true, this closes the file when 
146   raw_fd_ostream(int fd, bool shouldClose) : FD(fd), ShouldClose(shouldClose) {}
147   
148   ~raw_fd_ostream();
149     
150   /// flush_impl - The is the piece of the class that is implemented by
151   /// subclasses.  This outputs the currently buffered data and resets the
152   /// buffer to empty.
153   virtual void flush_impl();
154 };
155   
156 /// raw_stdout_ostream - This is a stream that always prints to stdout.
157 ///
158 class raw_stdout_ostream : public raw_fd_ostream {
159   // An out of line virtual method to provide a home for the class vtable.
160   virtual void handle();
161 public:
162   raw_stdout_ostream();
163 };
164
165 /// raw_stderr_ostream - This is a stream that always prints to stderr.
166 ///
167 class raw_stderr_ostream : public raw_fd_ostream {
168   // An out of line virtual method to provide a home for the class vtable.
169   virtual void handle();
170 public:
171   raw_stderr_ostream();
172 };
173   
174 /// outs() - This returns a reference to a raw_ostream for standard output.
175 /// Use it like: outs() << "foo" << "bar";
176 raw_ostream &outs();
177
178 /// errs() - This returns a reference to a raw_ostream for standard error.
179 /// Use it like: errs() << "foo" << "bar";
180 raw_ostream &errs();
181   
182   
183 /// raw_os_ostream - A raw_ostream that writes to an std::ostream.  This is a
184 /// simple adaptor class.
185 class raw_os_ostream : public raw_ostream {
186   std::ostream &OS;
187 public:
188   raw_os_ostream(std::ostream &O) : OS(O) {}
189   
190   /// flush_impl - The is the piece of the class that is implemented by
191   /// subclasses.  This outputs the currently buffered data and resets the
192   /// buffer to empty.
193   virtual void flush_impl();
194 };
195   
196 } // end llvm namespace
197
198 #endif