LLVM API Documentation

Streams.h

Go to the documentation of this file.
00001 //===- llvm/Support/Streams.h - Wrappers for iostreams ----------*- 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 implements a wrapper for the STL I/O streams.  It prevents the need
00011 // to include <iostream> in a file just to get I/O.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_SUPPORT_STREAMS_H
00016 #define LLVM_SUPPORT_STREAMS_H
00017 
00018 #include <iosfwd>
00019 
00020 namespace llvm {
00021 
00022   /// FlushStream - Function called by BaseStream to flush an ostream.
00023   void FlushStream(std::ostream &S);
00024 
00025   /// BaseStream - Acts like the STL streams. It's a wrapper for the std::cerr,
00026   /// std::cout, std::cin, etc. streams. However, it doesn't require #including
00027   /// @verbatim <iostream> @endverbatm in every file (doing so increases static 
00028   /// c'tors & d'tors in the object code).
00029   /// 
00030   template <typename StreamTy>
00031   class BaseStream {
00032     StreamTy *Stream;
00033   public:
00034     BaseStream() : Stream(0) {}
00035     BaseStream(StreamTy &S) : Stream(&S) {}
00036     BaseStream(StreamTy *S) : Stream(S) {}
00037 
00038     StreamTy *stream() const { return Stream; }
00039 
00040     inline BaseStream &operator << (std::ios_base &(*Func)(std::ios_base&)) {
00041       if (Stream) *Stream << Func;
00042       return *this;
00043     }
00044 
00045     inline BaseStream &operator << (StreamTy &(*Func)(StreamTy&)) {
00046       if (Stream) *Stream << Func;
00047       return *this;
00048     }
00049 
00050     void flush() {
00051       if (Stream)
00052         FlushStream(*Stream);
00053     }
00054 
00055     template <typename Ty>
00056     BaseStream &operator << (const Ty &Thing) {
00057       if (Stream) *Stream << Thing;
00058       return *this;
00059     }
00060 
00061     template <typename Ty>
00062     BaseStream &operator >> (Ty &Thing) {
00063       if (Stream) *Stream >> Thing;
00064       return *this;
00065     }
00066 
00067     template <typename Ty>
00068     BaseStream &write(const Ty &A, unsigned N) {
00069       if (Stream) Stream->write(A, N);
00070       return *this;
00071     }
00072     
00073     operator StreamTy* () { return Stream; }
00074 
00075     bool operator == (const StreamTy &S) { return &S == Stream; }
00076     bool operator != (const StreamTy &S) { return !(*this == S); }
00077     bool operator == (const BaseStream &S) { return S.Stream == Stream; }
00078     bool operator != (const BaseStream &S) { return !(*this == S); }
00079   };
00080 
00081   typedef BaseStream<std::ostream> OStream;
00082   typedef BaseStream<std::istream> IStream;
00083   typedef BaseStream<std::stringstream> StringStream;
00084 
00085   extern OStream cout;
00086   extern OStream cerr;
00087   extern IStream cin;
00088 
00089 } // End llvm namespace
00090 
00091 #endif



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