LLVM API Documentation

raw_ostream.h

Go to the documentation of this file.
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 public:
00155   /// raw_fd_ostream - Open the specified file for writing. If an
00156   /// error occurs, information about the error is put into ErrorInfo,
00157   /// and the stream should be immediately destroyed; the string will
00158   /// be empty if no error occurred.
00159   ///
00160   /// \param Filename - The file to open. If this is "-" then the
00161   /// stream will use stdout instead.
00162   /// \param Binary - The file should be opened in binary mode on
00163   /// platforms that support this distinction.
00164   raw_fd_ostream(const char *Filename, bool Binary, std::string &ErrorInfo);
00165   
00166   /// raw_fd_ostream ctor - FD is the file descriptor that this writes to.  If
00167   /// ShouldClose is true, this closes the file when 
00168   raw_fd_ostream(int fd, bool shouldClose) : FD(fd), ShouldClose(shouldClose) {}
00169   
00170   ~raw_fd_ostream();
00171     
00172   /// flush_impl - The is the piece of the class that is implemented by
00173   /// subclasses.  This outputs the currently buffered data and resets the
00174   /// buffer to empty.
00175   virtual void flush_impl();
00176   
00177   /// close - Manually flush the stream and close the file.
00178   void close();
00179   
00180   /// tell - Return the current offset with the file.
00181   uint64_t tell();
00182 };
00183   
00184 /// raw_stdout_ostream - This is a stream that always prints to stdout.
00185 ///
00186 class raw_stdout_ostream : public raw_fd_ostream {
00187   // An out of line virtual method to provide a home for the class vtable.
00188   virtual void handle();
00189 public:
00190   raw_stdout_ostream();
00191 };
00192 
00193 /// raw_stderr_ostream - This is a stream that always prints to stderr.
00194 ///
00195 class raw_stderr_ostream : public raw_fd_ostream {
00196   // An out of line virtual method to provide a home for the class vtable.
00197   virtual void handle();
00198 public:
00199   raw_stderr_ostream();
00200 };
00201   
00202 /// outs() - This returns a reference to a raw_ostream for standard output.
00203 /// Use it like: outs() << "foo" << "bar";
00204 raw_ostream &outs();
00205 
00206 /// errs() - This returns a reference to a raw_ostream for standard error.
00207 /// Use it like: errs() << "foo" << "bar";
00208 raw_ostream &errs();
00209   
00210   
00211 //===----------------------------------------------------------------------===//
00212 // Output Stream Adaptors
00213 //===----------------------------------------------------------------------===//
00214   
00215 /// raw_os_ostream - A raw_ostream that writes to an std::ostream.  This is a
00216 /// simple adaptor class.
00217 class raw_os_ostream : public raw_ostream {
00218   std::ostream &OS;
00219 public:
00220   raw_os_ostream(std::ostream &O) : OS(O) {}
00221   ~raw_os_ostream();
00222 
00223   /// flush_impl - The is the piece of the class that is implemented by
00224   /// subclasses.  This outputs the currently buffered data and resets the
00225   /// buffer to empty.
00226   virtual void flush_impl();
00227 };
00228 
00229 /// raw_string_ostream - A raw_ostream that writes to an std::string.  This is a
00230 /// simple adaptor class.
00231 class raw_string_ostream : public raw_ostream {
00232   std::string &OS;
00233 public:
00234   raw_string_ostream(std::string &O) : OS(O) {}
00235   ~raw_string_ostream();
00236   
00237   /// str - Flushes the stream contents to the target string and returns
00238   ///  the string's reference.
00239   std::string& str() {
00240     flush();
00241     return OS;
00242   }
00243   
00244   /// flush_impl - The is the piece of the class that is implemented by
00245   /// subclasses.  This outputs the currently buffered data and resets the
00246   /// buffer to empty.
00247   virtual void flush_impl();
00248 };
00249   
00250 /// raw_svector_ostream - A raw_ostream that writes to an SmallVector or
00251 /// SmallString.  This is a simple adaptor class.
00252 class raw_svector_ostream : public raw_ostream {
00253   SmallVectorImpl<char> &OS;
00254 public:
00255   raw_svector_ostream(SmallVectorImpl<char> &O) : OS(O) {}
00256   ~raw_svector_ostream();
00257   
00258   /// flush_impl - The is the piece of the class that is implemented by
00259   /// subclasses.  This outputs the currently buffered data and resets the
00260   /// buffer to empty.
00261   virtual void flush_impl();
00262 };
00263   
00264 } // end llvm namespace
00265 
00266 #endif



This web site is hosted by the Computer Science Department at the University of Illinois at Urbana-Champaign.