formatted_raw_ostream both is-a raw_ostream and has-a raw_ostream. This
[oota-llvm.git] / include / llvm / Support / FormattedStream.h
1 //===-- llvm/CodeGen/FormattedStream.h - Formatted streams ------*- 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 contains raw_ostream implementations for streams to do
11 // things like pretty-print comments.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_SUPPORT_FORMATTEDSTREAM_H
16 #define LLVM_SUPPORT_FORMATTEDSTREAM_H
17
18 #include "llvm/Support/raw_ostream.h"
19
20 namespace llvm 
21 {
22   /// formatted_raw_ostream - Formatted raw_fd_ostream to handle
23   /// asm-specific constructs.
24   ///
25   class formatted_raw_ostream : public raw_ostream {
26   public:
27     /// DELETE_STREAM - Tell the destructor to delete the held stream.
28     ///
29     const static bool DELETE_STREAM = true;
30     /// PRESERVE_STREAM - Tell the destructor to not delete the held
31     /// stream.
32     ///
33     const static bool PRESERVE_STREAM = false;
34     
35   private:
36     /// TheStream - The real stream we output to. We set it to be
37     /// unbuffered, since we're already doing our own buffering.
38     ///
39     raw_ostream *TheStream;
40     /// DeleteStream - Do we need to delete TheStream in the
41     /// destructor?
42     ///
43     bool DeleteStream;
44
45     /// Column - The current output column of the stream.  The column
46     /// scheme is zero-based.
47     ///
48     unsigned Column;
49
50     virtual void write_impl(const char *Ptr, unsigned Size) {
51       ComputeColumn(Ptr, Size);
52       TheStream->write(Ptr, Size);
53     }
54
55     /// current_pos - Return the current position within the stream,
56     /// not counting the bytes currently in the buffer.
57     virtual uint64_t current_pos() { 
58       // This has the same effect as calling TheStream.current_pos(),
59       // but that interface is private.
60       return TheStream->tell() - TheStream->GetNumBytesInBuffer();
61     }
62
63     /// ComputeColumn - Examine the current output and figure out
64     /// which column we end up in after output.
65     ///
66     void ComputeColumn(const char *Ptr, unsigned Size);
67
68   public:
69     /// formatted_raw_ostream - Open the specified file for
70     /// writing. If an error occurs, information about the error is
71     /// put into ErrorInfo, and the stream should be immediately
72     /// destroyed; the string will be empty if no error occurred.
73     ///
74     /// As a side effect, the given Stream is set to be Unbuffered.
75     /// This is because formatted_raw_ostream does its own buffering,
76     /// so it doesn't want another layer of buffering to be happening
77     /// underneath it.
78     ///
79     /// \param Filename - The file to open. If this is "-" then the
80     /// stream will use stdout instead.
81     /// \param Binary - The file should be opened in binary mode on
82     /// platforms that support this distinction.
83     formatted_raw_ostream(raw_ostream &Stream, bool Delete = false) 
84       : raw_ostream(), TheStream(&Stream), DeleteStream(Delete), Column(0) {
85       // This formatted_raw_ostream inherits from raw_ostream, so it'll do its
86       // own buffering, and it doesn't need or want TheStream to do another
87       // layer of buffering underneath. Resize the buffer to what TheStream
88       // had been using, and tell TheStream not to do its own buffering.
89       TheStream->flush();
90       if (size_t BufferSize = TheStream->GetNumBytesInBuffer())
91         SetBufferSize(BufferSize);
92       TheStream->SetUnbuffered();
93     }
94     explicit formatted_raw_ostream()
95       : raw_ostream(), TheStream(0), DeleteStream(false), Column(0) {}
96
97     ~formatted_raw_ostream() {
98       if (DeleteStream)
99         delete TheStream;
100     }
101     
102     void setStream(raw_ostream &Stream, bool Delete = false) {
103       TheStream = &Stream;
104       DeleteStream = Delete;
105
106       // Avoid double-buffering, as above.
107       TheStream->flush();
108       if (size_t BufferSize = TheStream->GetNumBytesInBuffer())
109         SetBufferSize(BufferSize);
110       TheStream->SetUnbuffered();
111     }
112
113     /// PadToColumn - Align the output to some column number.
114     ///
115     /// \param NewCol - The column to move to.
116     /// \param MinPad - The minimum space to give after the most
117     /// recent I/O, even if the current column + minpad > newcol.
118     ///
119     void PadToColumn(unsigned NewCol, unsigned MinPad = 0);
120   };
121
122 /// fouts() - This returns a reference to a formatted_raw_ostream for
123 /// standard output.  Use it like: fouts() << "foo" << "bar";
124 formatted_raw_ostream &fouts();
125
126 /// ferrs() - This returns a reference to a formatted_raw_ostream for
127 /// standard error.  Use it like: ferrs() << "foo" << "bar";
128 formatted_raw_ostream &ferrs();
129
130 } // end llvm namespace
131
132
133 #endif