LLVM API Documentation
00001 //===--- raw_ostream.h - Raw output stream ----------------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines the raw_ostream class. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #ifndef LLVM_SUPPORT_RAW_OSTREAM_H 00015 #define LLVM_SUPPORT_RAW_OSTREAM_H 00016 00017 #include "llvm/ADT/StringExtras.h" 00018 #include <cassert> 00019 #include <cstring> 00020 #include <string> 00021 #include <iosfwd> 00022 00023 namespace llvm { 00024 class format_object_base; 00025 template <typename T> 00026 class SmallVectorImpl; 00027 00028 /// raw_ostream - This class implements an extremely fast bulk output stream 00029 /// that can *only* output to a stream. It does not support seeking, reopening, 00030 /// rewinding, line buffered disciplines etc. It is a simple buffer that outputs 00031 /// a chunk at a time. 00032 class raw_ostream { 00033 protected: 00034 char *OutBufStart, *OutBufEnd, *OutBufCur; 00035 public: 00036 raw_ostream() { 00037 // Start out ready to flush. 00038 OutBufStart = OutBufEnd = OutBufCur = 0; 00039 } 00040 00041 virtual ~raw_ostream() { 00042 delete [] OutBufStart; 00043 } 00044 00045 //===--------------------------------------------------------------------===// 00046 // Configuration Interface 00047 //===--------------------------------------------------------------------===// 00048 00049 /// SetBufferSize - Set the internal buffer size to the specified amount 00050 /// instead of the default. 00051 void SetBufferSize(unsigned Size) { 00052 assert(Size >= 64 && 00053 "Buffer size must be somewhat large for invariants to hold"); 00054 flush(); 00055 00056 delete [] OutBufStart; 00057 OutBufStart = new char[Size]; 00058 OutBufEnd = OutBufStart+Size; 00059 OutBufCur = OutBufStart; 00060 } 00061 00062 //===--------------------------------------------------------------------===// 00063 // Data Output Interface 00064 //===--------------------------------------------------------------------===// 00065 00066 void flush() { 00067 if (OutBufCur != OutBufStart) 00068 flush_impl(); 00069 } 00070 00071 raw_ostream &operator<<(char C) { 00072 if (OutBufCur >= OutBufEnd) 00073 flush_impl(); 00074 *OutBufCur++ = C; 00075 return *this; 00076 } 00077 00078 raw_ostream &operator<<(unsigned char C) { 00079 if (OutBufCur >= OutBufEnd) 00080 flush_impl(); 00081 *OutBufCur++ = C; 00082 return *this; 00083 } 00084 00085 raw_ostream &operator<<(signed char C) { 00086 if (OutBufCur >= OutBufEnd) 00087 flush_impl(); 00088 *OutBufCur++ = C; 00089 return *this; 00090 } 00091 00092 raw_ostream &operator<<(const char *Str) { 00093 return write(Str, strlen(Str)); 00094 } 00095 00096 raw_ostream &operator<<(const std::string& Str) { 00097 return write(Str.data(), Str.length()); 00098 } 00099 00100 raw_ostream &operator<<(unsigned long N); 00101 raw_ostream &operator<<(long N); 00102 raw_ostream &operator<<(unsigned long long N); 00103 raw_ostream &operator<<(long long N); 00104 raw_ostream &operator<<(const void *P); 00105 raw_ostream &operator<<(unsigned int N) { 00106 return this->operator<<(static_cast<unsigned long>(N)); 00107 } 00108 00109 raw_ostream &operator<<(int N) { 00110 return this->operator<<(static_cast<long>(N)); 00111 } 00112 00113 raw_ostream &operator<<(double N) { 00114 return this->operator<<(ftostr(N)); 00115 } 00116 00117 raw_ostream &write(const char *Ptr, unsigned Size); 00118 00119 // Formatted output, see the format() function in Support/Format.h. 00120 raw_ostream &operator<<(const format_object_base &Fmt); 00121 00122 //===--------------------------------------------------------------------===// 00123 // Subclass Interface 00124 //===--------------------------------------------------------------------===// 00125 00126 protected: 00127 00128 /// flush_impl - The is the piece of the class that is implemented by 00129 /// subclasses. This outputs the currently buffered data and resets the 00130 /// buffer to empty. 00131 virtual void flush_impl() = 0; 00132 00133 /// HandleFlush - A stream's implementation of flush should call this after 00134 /// emitting the bytes to the data sink. 00135 void HandleFlush() { 00136 if (OutBufStart == 0) 00137 SetBufferSize(4096); 00138 OutBufCur = OutBufStart; 00139 } 00140 private: 00141 // An out of line virtual method to provide a home for the class vtable. 00142 virtual void handle(); 00143 }; 00144 00145 //===----------------------------------------------------------------------===// 00146 // File Output Streams 00147 //===----------------------------------------------------------------------===// 00148 00149 /// raw_fd_ostream - A raw_ostream that writes to a file descriptor. 00150 /// 00151 class raw_fd_ostream : public raw_ostream { 00152 int FD; 00153 bool ShouldClose; 00154 uint64_t pos; 00155 public: 00156 /// raw_fd_ostream - Open the specified file for writing. If an 00157 /// error occurs, information about the error is put into ErrorInfo, 00158 /// and the stream should be immediately destroyed; the string will 00159 /// be empty if no error occurred. 00160 /// 00161 /// \param Filename - The file to open. If this is "-" then the 00162 /// stream will use stdout instead. 00163 /// \param Binary - The file should be opened in binary mode on 00164 /// platforms that support this distinction. 00165 raw_fd_ostream(const char *Filename, bool Binary, std::string &ErrorInfo); 00166 00167 /// raw_fd_ostream ctor - FD is the file descriptor that this writes to. If 00168 /// ShouldClose is true, this closes the file when 00169 raw_fd_ostream(int fd, bool shouldClose) : FD(fd), ShouldClose(shouldClose) {} 00170 00171 ~raw_fd_ostream(); 00172 00173 /// flush_impl - The is the piece of the class that is implemented by 00174 /// subclasses. This outputs the currently buffered data and resets the 00175 /// buffer to empty. 00176 virtual void flush_impl(); 00177 00178 /// close - Manually flush the stream and close the file. 00179 void close(); 00180 00181 /// tell - Return the current offset with the file. 00182 uint64_t tell() { 00183 return pos + (OutBufCur - OutBufStart); 00184 } 00185 00186 /// seek - Flushes the stream and repositions the underlying file descriptor 00187 /// positition to the offset specified from the beginning of the file. 00188 uint64_t seek(uint64_t off); 00189 }; 00190 00191 /// raw_stdout_ostream - This is a stream that always prints to stdout. 00192 /// 00193 class raw_stdout_ostream : public raw_fd_ostream { 00194 // An out of line virtual method to provide a home for the class vtable. 00195 virtual void handle(); 00196 public: 00197 raw_stdout_ostream(); 00198 }; 00199 00200 /// raw_stderr_ostream - This is a stream that always prints to stderr. 00201 /// 00202 class raw_stderr_ostream : public raw_fd_ostream { 00203 // An out of line virtual method to provide a home for the class vtable. 00204 virtual void handle(); 00205 public: 00206 raw_stderr_ostream(); 00207 }; 00208 00209 /// outs() - This returns a reference to a raw_ostream for standard output. 00210 /// Use it like: outs() << "foo" << "bar"; 00211 raw_ostream &outs(); 00212 00213 /// errs() - This returns a reference to a raw_ostream for standard error. 00214 /// Use it like: errs() << "foo" << "bar"; 00215 raw_ostream &errs(); 00216 00217 00218 //===----------------------------------------------------------------------===// 00219 // Output Stream Adaptors 00220 //===----------------------------------------------------------------------===// 00221 00222 /// raw_os_ostream - A raw_ostream that writes to an std::ostream. This is a 00223 /// simple adaptor class. 00224 class raw_os_ostream : public raw_ostream { 00225 std::ostream &OS; 00226 public: 00227 raw_os_ostream(std::ostream &O) : OS(O) {} 00228 ~raw_os_ostream(); 00229 00230 /// flush_impl - The is the piece of the class that is implemented by 00231 /// subclasses. This outputs the currently buffered data and resets the 00232 /// buffer to empty. 00233 virtual void flush_impl(); 00234 }; 00235 00236 /// raw_string_ostream - A raw_ostream that writes to an std::string. This is a 00237 /// simple adaptor class. 00238 class raw_string_ostream : public raw_ostream { 00239 std::string &OS; 00240 public: 00241 raw_string_ostream(std::string &O) : OS(O) {} 00242 ~raw_string_ostream(); 00243 00244 /// str - Flushes the stream contents to the target string and returns 00245 /// the string's reference. 00246 std::string& str() { 00247 flush(); 00248 return OS; 00249 } 00250 00251 /// flush_impl - The is the piece of the class that is implemented by 00252 /// subclasses. This outputs the currently buffered data and resets the 00253 /// buffer to empty. 00254 virtual void flush_impl(); 00255 }; 00256 00257 /// raw_svector_ostream - A raw_ostream that writes to an SmallVector or 00258 /// SmallString. This is a simple adaptor class. 00259 class raw_svector_ostream : public raw_ostream { 00260 SmallVectorImpl<char> &OS; 00261 public: 00262 raw_svector_ostream(SmallVectorImpl<char> &O) : OS(O) {} 00263 ~raw_svector_ostream(); 00264 00265 /// flush_impl - The is the piece of the class that is implemented by 00266 /// subclasses. This outputs the currently buffered data and resets the 00267 /// buffer to empty. 00268 virtual void flush_impl(); 00269 }; 00270 00271 } // end llvm namespace 00272 00273 #endif