2006-11-17 09:51:22 +00:00
|
|
|
//===- llvm/Support/Streams.h - Wrappers for iostreams ----------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 19:59:42 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2006-11-17 09:51:22 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2006-12-07 01:30:32 +00:00
|
|
|
// This file implements a wrapper for the STL I/O streams. It prevents the need
|
|
|
|
// to include <iostream> in a file just to get I/O.
|
2006-11-17 09:51:22 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_SUPPORT_STREAMS_H
|
|
|
|
#define LLVM_SUPPORT_STREAMS_H
|
|
|
|
|
2006-12-07 23:41:45 +00:00
|
|
|
#include <iosfwd>
|
2006-11-17 09:51:22 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2008-05-29 21:46:33 +00:00
|
|
|
/// FlushStream - Function called by BaseStream to flush an ostream.
|
|
|
|
void FlushStream(std::ostream &S);
|
|
|
|
|
2006-12-07 01:30:32 +00:00
|
|
|
/// BaseStream - Acts like the STL streams. It's a wrapper for the std::cerr,
|
|
|
|
/// std::cout, std::cin, etc. streams. However, it doesn't require #including
|
2009-02-20 22:51:36 +00:00
|
|
|
/// @verbatim <iostream> @endverbatm in every file (doing so increases static
|
2007-08-05 20:06:04 +00:00
|
|
|
/// c'tors & d'tors in the object code).
|
2009-02-20 22:51:36 +00:00
|
|
|
///
|
2006-12-07 01:30:32 +00:00
|
|
|
template <typename StreamTy>
|
|
|
|
class BaseStream {
|
|
|
|
StreamTy *Stream;
|
2006-11-17 09:51:22 +00:00
|
|
|
public:
|
2006-12-07 01:30:32 +00:00
|
|
|
BaseStream() : Stream(0) {}
|
|
|
|
BaseStream(StreamTy &S) : Stream(&S) {}
|
|
|
|
BaseStream(StreamTy *S) : Stream(S) {}
|
2006-11-17 09:51:22 +00:00
|
|
|
|
2006-12-07 01:30:32 +00:00
|
|
|
StreamTy *stream() const { return Stream; }
|
2006-11-26 10:51:51 +00:00
|
|
|
|
2008-08-17 18:24:26 +00:00
|
|
|
inline BaseStream &operator << (std::ios_base &(*Func)(std::ios_base&)) {
|
|
|
|
if (Stream) *Stream << Func;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2006-12-07 01:30:32 +00:00
|
|
|
inline BaseStream &operator << (StreamTy &(*Func)(StreamTy&)) {
|
2006-11-27 10:45:49 +00:00
|
|
|
if (Stream) *Stream << Func;
|
2006-11-26 10:51:51 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2006-12-07 01:30:32 +00:00
|
|
|
|
2008-05-29 21:46:33 +00:00
|
|
|
void flush() {
|
|
|
|
if (Stream)
|
|
|
|
FlushStream(*Stream);
|
|
|
|
}
|
|
|
|
|
2006-11-17 09:51:22 +00:00
|
|
|
template <typename Ty>
|
2006-12-07 01:30:32 +00:00
|
|
|
BaseStream &operator << (const Ty &Thing) {
|
2006-11-17 09:51:22 +00:00
|
|
|
if (Stream) *Stream << Thing;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2006-12-07 01:30:32 +00:00
|
|
|
template <typename Ty>
|
2008-03-25 18:16:52 +00:00
|
|
|
BaseStream &operator >> (Ty &Thing) {
|
2006-12-07 01:30:32 +00:00
|
|
|
if (Stream) *Stream >> Thing;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2008-11-19 06:45:06 +00:00
|
|
|
template <typename Ty>
|
|
|
|
BaseStream &write(const Ty &A, unsigned N) {
|
|
|
|
if (Stream) Stream->write(A, N);
|
|
|
|
return *this;
|
|
|
|
}
|
2009-02-20 22:51:36 +00:00
|
|
|
|
2006-12-17 05:15:13 +00:00
|
|
|
operator StreamTy* () { return Stream; }
|
|
|
|
|
2006-12-07 01:30:32 +00:00
|
|
|
bool operator == (const StreamTy &S) { return &S == Stream; }
|
|
|
|
bool operator != (const StreamTy &S) { return !(*this == S); }
|
|
|
|
bool operator == (const BaseStream &S) { return S.Stream == Stream; }
|
|
|
|
bool operator != (const BaseStream &S) { return !(*this == S); }
|
2006-11-17 09:51:22 +00:00
|
|
|
};
|
|
|
|
|
2006-12-07 01:30:32 +00:00
|
|
|
typedef BaseStream<std::ostream> OStream;
|
|
|
|
typedef BaseStream<std::istream> IStream;
|
|
|
|
typedef BaseStream<std::stringstream> StringStream;
|
|
|
|
|
|
|
|
extern OStream cout;
|
|
|
|
extern OStream cerr;
|
|
|
|
extern IStream cin;
|
2006-11-17 09:51:22 +00:00
|
|
|
|
|
|
|
} // End llvm namespace
|
|
|
|
|
|
|
|
#endif
|