Fold the useful features of alist and alist_node into ilist, and
[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   /// FlushStream - Function called by BaseStream to flush an ostream.
23   void FlushStream(std::ostream &S);
24
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).
29   /// 
30   template <typename StreamTy>
31   class BaseStream {
32     StreamTy *Stream;
33   public:
34     BaseStream() : Stream(0) {}
35     BaseStream(StreamTy &S) : Stream(&S) {}
36     BaseStream(StreamTy *S) : Stream(S) {}
37
38     StreamTy *stream() const { return Stream; }
39
40     inline BaseStream &operator << (StreamTy &(*Func)(StreamTy&)) {
41       if (Stream) *Stream << Func;
42       return *this;
43     }
44
45     void flush() {
46       if (Stream)
47         FlushStream(*Stream);
48     }
49
50     template <typename Ty>
51     BaseStream &operator << (const Ty &Thing) {
52       if (Stream) *Stream << Thing;
53       return *this;
54     }
55
56     template <typename Ty>
57     BaseStream &operator >> (Ty &Thing) {
58       if (Stream) *Stream >> Thing;
59       return *this;
60     }
61
62     operator StreamTy* () { return Stream; }
63
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); }
68   };
69
70   typedef BaseStream<std::ostream> OStream;
71   typedef BaseStream<std::istream> IStream;
72   typedef BaseStream<std::stringstream> StringStream;
73
74   extern OStream cout;
75   extern OStream cerr;
76   extern IStream cin;
77
78 } // End llvm namespace
79
80 #endif