1 //===- llvm/Support/Streams.h - Wrappers for iostreams ----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements a wrapper for the STL I/O streams. It prevents the need
11 // to include <iostream> in a file just to get I/O.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_SUPPORT_STREAMS_H
16 #define LLVM_SUPPORT_STREAMS_H
22 /// FlushStream - Function called by BaseStream to flush an ostream.
23 void FlushStream(std::ostream &S);
25 /// BaseStream - Acts like the STL streams. It's a wrapper for the std::cerr,
26 /// std::cout, std::cin, etc. streams. However, it doesn't require #including
27 /// @verbatim <iostream> @endverbatm in every file (doing so increases static
28 /// c'tors & d'tors in the object code).
30 template <typename StreamTy>
34 BaseStream() : Stream(0) {}
35 BaseStream(StreamTy &S) : Stream(&S) {}
36 BaseStream(StreamTy *S) : Stream(S) {}
38 StreamTy *stream() const { return Stream; }
40 inline BaseStream &operator << (StreamTy &(*Func)(StreamTy&)) {
41 if (Stream) *Stream << Func;
50 template <typename Ty>
51 BaseStream &operator << (const Ty &Thing) {
52 if (Stream) *Stream << Thing;
56 template <typename Ty>
57 BaseStream &operator >> (Ty &Thing) {
58 if (Stream) *Stream >> Thing;
62 operator StreamTy* () { return Stream; }
64 bool operator == (const StreamTy &S) { return &S == Stream; }
65 bool operator != (const StreamTy &S) { return !(*this == S); }
66 bool operator == (const BaseStream &S) { return S.Stream == Stream; }
67 bool operator != (const BaseStream &S) { return !(*this == S); }
70 typedef BaseStream<std::ostream> OStream;
71 typedef BaseStream<std::istream> IStream;
72 typedef BaseStream<std::stringstream> StringStream;
78 } // End llvm namespace