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 /// BaseStream - Acts like the STL streams. It's a wrapper for the std::cerr,
23 /// std::cout, std::cin, etc. streams. However, it doesn't require #including
24 /// @verbatim <iostream> @endverbatm in every file (doing so increases static
25 /// c'tors & d'tors in the object code).
27 template <typename StreamTy>
31 BaseStream() : Stream(0) {}
32 BaseStream(StreamTy &S) : Stream(&S) {}
33 BaseStream(StreamTy *S) : Stream(S) {}
35 StreamTy *stream() const { return Stream; }
37 inline BaseStream &operator << (StreamTy &(*Func)(StreamTy&)) {
38 if (Stream) *Stream << Func;
42 template <typename Ty>
43 BaseStream &operator << (const Ty &Thing) {
44 if (Stream) *Stream << Thing;
48 template <typename Ty>
49 BaseStream &operator >> (const Ty &Thing) {
50 if (Stream) *Stream >> Thing;
54 operator StreamTy* () { return Stream; }
56 bool operator == (const StreamTy &S) { return &S == Stream; }
57 bool operator != (const StreamTy &S) { return !(*this == S); }
58 bool operator == (const BaseStream &S) { return S.Stream == Stream; }
59 bool operator != (const BaseStream &S) { return !(*this == S); }
62 typedef BaseStream<std::ostream> OStream;
63 typedef BaseStream<std::istream> IStream;
64 typedef BaseStream<std::stringstream> StringStream;
70 } // End llvm namespace