LLVM 22.0.0git
raw_ostream_proxy.h
Go to the documentation of this file.
1//===- raw_ostream_proxy.h - Proxies for raw output streams -----*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_SUPPORT_RAW_OSTREAM_PROXY_H
10#define LLVM_SUPPORT_RAW_OSTREAM_PROXY_H
11
13
14namespace llvm {
15
16/// Adaptor to create a stream class that proxies another \a raw_ostream.
17///
18/// Use \a raw_ostream_proxy_adaptor<> directly to implement an abstract
19/// derived class of \a raw_ostream as a proxy. Otherwise use \a
20/// raw_ostream_proxy.
21///
22/// Most operations are forwarded to the proxied stream.
23///
24/// If the proxied stream is buffered, the buffer is dropped and moved to this
25/// stream. This allows \a flush() to work correctly, flushing immediately from
26/// the proxy through to the final stream, and avoids any wasteful
27/// double-buffering.
28///
29/// \a enable_colors() changes both the proxied stream and the proxy itself.
30/// \a is_displayed() and \a has_colors() are forwarded to the proxy. \a
31/// changeColor(), resetColor(), and \a reverseColor() are not forwarded, since
32/// they need to call \a flush() and the buffer lives in the proxy.
33template <class RawOstreamT = raw_ostream>
34class raw_ostream_proxy_adaptor : public RawOstreamT {
35 void write_impl(const char *Ptr, size_t Size) override {
37 }
38 uint64_t current_pos() const override { return getProxiedOS().tell(); }
39 size_t preferred_buffer_size() const override {
41 }
42
43public:
44 void reserveExtraSpace(uint64_t ExtraSize) override {
46 }
47 bool is_displayed() const override { return getProxiedOS().is_displayed(); }
48 bool has_colors() const override { return getProxiedOS().has_colors(); }
49 void enable_colors(bool enable) override {
50 RawOstreamT::enable_colors(enable);
52 }
53 bool hasProxiedOS() const { return OS; }
55 assert(OS && "raw_ostream_proxy_adaptor use after reset");
56 return *OS;
57 }
58 size_t getPreferredBufferSize() const { return PreferredBufferSize; }
59
61
62protected:
63 template <class... ArgsT>
64 explicit raw_ostream_proxy_adaptor(raw_ostream &OS, ArgsT &&...Args)
65 : RawOstreamT(std::forward<ArgsT>(Args)...), OS(&OS),
66 PreferredBufferSize(OS.GetBufferSize()) {
67 // Drop OS's buffer to make this->flush() forward. This proxy will add a
68 // buffer in its place.
69 OS.SetUnbuffered();
70 }
71
72 /// Stop proxying the stream. Flush and set up a crash for future writes.
73 ///
74 /// For example, this can simplify logic when a subclass might have a longer
75 /// lifetime than the stream it proxies.
77 this->SetUnbuffered();
78 OS = nullptr;
79 }
80
81private:
82 raw_ostream *OS;
83
84 /// Caches the value of OS->GetBufferSize() at construction time.
85 size_t PreferredBufferSize;
86};
87
88/// Adaptor for creating a stream that proxies a \a raw_pwrite_stream.
89template <class RawPwriteStreamT = raw_pwrite_stream>
91 : public raw_ostream_proxy_adaptor<RawPwriteStreamT> {
92 using RawOstreamAdaptorT = raw_ostream_proxy_adaptor<RawPwriteStreamT>;
93
94 void pwrite_impl(const char *Ptr, size_t Size, uint64_t Offset) override {
95 this->flush();
97 }
98
99protected:
101 template <class... ArgsT>
103 ArgsT &&...Args)
104 : RawOstreamAdaptorT(OS, std::forward<ArgsT>(Args)...) {}
105
109};
110
111/// Non-owning proxy for a \a raw_ostream. Enables passing a stream into an
112/// API that takes ownership.
114 void anchor() override;
115
116public:
118};
119
120/// Non-owning proxy for a \a raw_pwrite_stream. Enables passing a stream
121/// into an API that takes ownership.
123 void anchor() override;
124
125public:
128};
129
130} // end namespace llvm
131
132#endif // LLVM_SUPPORT_RAW_OSTREAM_PROXY_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
void enable_colors(bool enable) override
raw_ostream & getProxiedOS() const
bool is_displayed() const override
This function determines if this stream is connected to a "tty" or "console" window.
raw_ostream_proxy_adaptor(raw_ostream &OS, ArgsT &&...Args)
void resetProxiedOS()
Stop proxying the stream.
void reserveExtraSpace(uint64_t ExtraSize) override
If possible, pre-allocate ExtraSize bytes for stream data.
bool has_colors() const override
This function determines if this stream is displayed and supports colors.
raw_ostream_proxy(raw_ostream &OS)
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
uint64_t tell() const
tell - Return the current offset with the file.
size_t GetBufferSize() const
virtual size_t preferred_buffer_size() const
Return an efficient buffer size for the underlying output mechanism.
raw_ostream & write(unsigned char C)
void SetUnbuffered()
Set the stream to be unbuffered.
virtual bool is_displayed() const
This function determines if this stream is connected to a "tty" or "console" window.
virtual void enable_colors(bool enable)
virtual void reserveExtraSpace(uint64_t ExtraSize)
If possible, pre-allocate ExtraSize bytes for stream data.
virtual bool has_colors() const
This function determines if this stream is displayed and supports colors.
raw_pwrite_stream & getProxiedOS() const
raw_pwrite_stream_proxy_adaptor(raw_pwrite_stream &OS, ArgsT &&...Args)
raw_pwrite_stream_proxy(raw_pwrite_stream &OS)
An abstract base class for streams implementations that also support a pwrite operation.
void pwrite(const char *Ptr, size_t Size, uint64_t Offset)
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:477
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:851