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