Add explicit keywords.
[oota-llvm.git] / include / llvm / Support / Streams.h
1 //===- llvm/Support/Streams.h - Wrappers for iostreams ----------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_SUPPORT_STREAMS_H
16 #define LLVM_SUPPORT_STREAMS_H
17
18 #include <iosfwd>
19
20 namespace llvm {
21
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).
26   /// 
27   template <typename StreamTy>
28   class BaseStream {
29     StreamTy *Stream;
30   public:
31     BaseStream() : Stream(0) {}
32     BaseStream(StreamTy &S) : Stream(&S) {}
33     BaseStream(StreamTy *S) : Stream(S) {}
34
35     StreamTy *stream() const { return Stream; }
36
37     inline BaseStream &operator << (StreamTy &(*Func)(StreamTy&)) {
38       if (Stream) *Stream << Func;
39       return *this;
40     }
41
42     template <typename Ty>
43     BaseStream &operator << (const Ty &Thing) {
44       if (Stream) *Stream << Thing;
45       return *this;
46     }
47
48     template <typename Ty>
49     BaseStream &operator >> (const Ty &Thing) {
50       if (Stream) *Stream >> Thing;
51       return *this;
52     }
53
54     operator StreamTy* () { return Stream; }
55
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); }
60   };
61
62   typedef BaseStream<std::ostream> OStream;
63   typedef BaseStream<std::istream> IStream;
64   typedef BaseStream<std::stringstream> StringStream;
65
66   extern OStream cout;
67   extern OStream cerr;
68   extern IStream cin;
69
70 } // End llvm namespace
71
72 #endif