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 << (std::ios_base &(*Func)(std::ios_base&)) {
41 if (Stream) *Stream << Func;
45 inline BaseStream &operator << (StreamTy &(*Func)(StreamTy&)) {
46 if (Stream) *Stream << Func;
55 template <typename Ty>
56 BaseStream &operator << (const Ty &Thing) {
57 if (Stream) *Stream << Thing;
61 template <typename Ty>
62 BaseStream &operator >> (Ty &Thing) {
63 if (Stream) *Stream >> Thing;
67 template <typename Ty>
68 BaseStream &write(const Ty &A, unsigned N) {
69 if (Stream) Stream->write(A, N);
73 operator StreamTy* () { return Stream; }
75 bool operator == (const StreamTy &S) { return &S == Stream; }
76 bool operator != (const StreamTy &S) { return !(*this == S); }
77 bool operator == (const BaseStream &S) { return S.Stream == Stream; }
78 bool operator != (const BaseStream &S) { return !(*this == S); }
81 typedef BaseStream<std::ostream> OStream;
82 typedef BaseStream<std::istream> IStream;
83 typedef BaseStream<std::stringstream> StringStream;
89 } // End llvm namespace