raw_ostream.h revision 78a2812538d871a62d90f53ed66d26198876d011
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 class format_object_base; 25 template <typename T> 26 class SmallVectorImpl; 27 28/// raw_ostream - This class implements an extremely fast bulk output stream 29/// that can *only* output to a stream. It does not support seeking, reopening, 30/// rewinding, line buffered disciplines etc. It is a simple buffer that outputs 31/// a chunk at a time. 32class raw_ostream { 33protected: 34 char *OutBufStart, *OutBufEnd, *OutBufCur; 35public: 36 raw_ostream() { 37 // Start out ready to flush. 38 OutBufStart = OutBufEnd = OutBufCur = 0; 39 } 40 virtual ~raw_ostream() {} 41 42 //===--------------------------------------------------------------------===// 43 // Configuration Interface 44 //===--------------------------------------------------------------------===// 45 46 /// SetBufferSize - Set the internal buffer size to the specified amount 47 /// instead of the default. 48 void SetBufferSize(unsigned Size) { 49 assert(Size >= 64 && 50 "Buffer size must be somewhat large for invariants to hold"); 51 flush(); 52 53 delete [] OutBufStart; 54 OutBufStart = new char[Size]; 55 OutBufEnd = OutBufStart+Size; 56 OutBufCur = OutBufStart; 57 } 58 59 //===--------------------------------------------------------------------===// 60 // Data Output Interface 61 //===--------------------------------------------------------------------===// 62 63 void flush() { 64 if (OutBufCur != OutBufStart) 65 flush_impl(); 66 } 67 68 raw_ostream &operator<<(char C) { 69 if (OutBufCur >= OutBufEnd) 70 flush_impl(); 71 *OutBufCur++ = C; 72 return *this; 73 } 74 75 raw_ostream &operator<<(unsigned char C) { 76 if (OutBufCur >= OutBufEnd) 77 flush_impl(); 78 *OutBufCur++ = C; 79 return *this; 80 } 81 82 raw_ostream &operator<<(signed char C) { 83 if (OutBufCur >= OutBufEnd) 84 flush_impl(); 85 *OutBufCur++ = C; 86 return *this; 87 } 88 89 raw_ostream &operator<<(const char *Str) { 90 return write(Str, strlen(Str)); 91 } 92 93 raw_ostream &operator<<(const std::string& Str) { 94 return write(Str.data(), Str.length()); 95 } 96 97 raw_ostream &operator<<(unsigned long N); 98 raw_ostream &operator<<(long N); 99 raw_ostream &operator<<(unsigned long long N); 100 raw_ostream &operator<<(long long N); 101 raw_ostream &operator<<(const void *P); 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 raw_ostream &write(const char *Ptr, unsigned Size); 115 116 // Formatted output, see the format() function in Support/Format.h. 117 raw_ostream &operator<<(const format_object_base &Fmt); 118 119 //===--------------------------------------------------------------------===// 120 // Subclass Interface 121 //===--------------------------------------------------------------------===// 122 123protected: 124 125 /// flush_impl - The is the piece of the class that is implemented by 126 /// subclasses. This outputs the currently buffered data and resets the 127 /// buffer to empty. 128 virtual void flush_impl() = 0; 129 130 /// HandleFlush - A stream's implementation of flush should call this after 131 /// emitting the bytes to the data sink. 132 void HandleFlush() { 133 if (OutBufStart == 0) 134 SetBufferSize(4096); 135 OutBufCur = OutBufStart; 136 } 137private: 138 // An out of line virtual method to provide a home for the class vtable. 139 virtual void handle(); 140}; 141 142//===----------------------------------------------------------------------===// 143// File Output Streams 144//===----------------------------------------------------------------------===// 145 146/// raw_fd_ostream - A raw_ostream that writes to a file descriptor. 147/// 148class raw_fd_ostream : public raw_ostream { 149 int FD; 150 bool ShouldClose; 151public: 152 /// raw_fd_ostream - Open the specified file for writing. If an error occurs, 153 /// information about the error is put into ErrorInfo, and the stream should 154 /// be immediately destroyed. 155 raw_fd_ostream(const char *Filename, std::string &ErrorInfo); 156 157 /// raw_fd_ostream ctor - FD is the file descriptor that this writes to. If 158 /// ShouldClose is true, this closes the file when 159 raw_fd_ostream(int fd, bool shouldClose) : FD(fd), ShouldClose(shouldClose) {} 160 161 ~raw_fd_ostream(); 162 163 /// flush_impl - The is the piece of the class that is implemented by 164 /// subclasses. This outputs the currently buffered data and resets the 165 /// buffer to empty. 166 virtual void flush_impl(); 167}; 168 169/// raw_stdout_ostream - This is a stream that always prints to stdout. 170/// 171class raw_stdout_ostream : public raw_fd_ostream { 172 // An out of line virtual method to provide a home for the class vtable. 173 virtual void handle(); 174public: 175 raw_stdout_ostream(); 176}; 177 178/// raw_stderr_ostream - This is a stream that always prints to stderr. 179/// 180class raw_stderr_ostream : public raw_fd_ostream { 181 // An out of line virtual method to provide a home for the class vtable. 182 virtual void handle(); 183public: 184 raw_stderr_ostream(); 185}; 186 187/// outs() - This returns a reference to a raw_ostream for standard output. 188/// Use it like: outs() << "foo" << "bar"; 189raw_ostream &outs(); 190 191/// errs() - This returns a reference to a raw_ostream for standard error. 192/// Use it like: errs() << "foo" << "bar"; 193raw_ostream &errs(); 194 195 196//===----------------------------------------------------------------------===// 197// Output Stream Adaptors 198//===----------------------------------------------------------------------===// 199 200/// raw_os_ostream - A raw_ostream that writes to an std::ostream. This is a 201/// simple adaptor class. 202class raw_os_ostream : public raw_ostream { 203 std::ostream &OS; 204public: 205 raw_os_ostream(std::ostream &O) : OS(O) {} 206 ~raw_os_ostream(); 207 208 /// flush_impl - The is the piece of the class that is implemented by 209 /// subclasses. This outputs the currently buffered data and resets the 210 /// buffer to empty. 211 virtual void flush_impl(); 212}; 213 214/// raw_string_ostream - A raw_ostream that writes to an std::string. This is a 215/// simple adaptor class. 216class raw_string_ostream : public raw_ostream { 217 std::string &OS; 218public: 219 raw_string_ostream(std::string &O) : OS(O) {} 220 ~raw_string_ostream(); 221 222 /// flush_impl - The is the piece of the class that is implemented by 223 /// subclasses. This outputs the currently buffered data and resets the 224 /// buffer to empty. 225 virtual void flush_impl(); 226}; 227 228/// raw_svector_ostream - A raw_ostream that writes to an SmallVector or 229/// SmallString. This is a simple adaptor class. 230class raw_svector_ostream : public raw_ostream { 231 SmallVectorImpl<char> &OS; 232public: 233 raw_svector_ostream(SmallVectorImpl<char> &O) : OS(O) {} 234 ~raw_svector_ostream(); 235 236 /// flush_impl - The is the piece of the class that is implemented by 237 /// subclasses. This outputs the currently buffered data and resets the 238 /// buffer to empty. 239 virtual void flush_impl(); 240}; 241 242} // end llvm namespace 243 244#endif 245