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
14
15namespace llvm {
16
17/// Adaptor to create a stream class that proxies another \a raw_ostream.
18///
19/// Use \a raw_ostream_proxy_adaptor<> directly to implement an abstract
20/// derived class of \a raw_ostream as a proxy. Otherwise use \a
21/// raw_ostream_proxy.
22///
23/// Most operations are forwarded to the proxied stream.
24///
25/// If the proxied stream is buffered, the buffer is dropped and moved to this
26/// stream. This allows \a flush() to work correctly, flushing immediately from
27/// the proxy through to the final stream, and avoids any wasteful
28/// double-buffering.
29///
30/// \a enable_colors() changes both the proxied stream and the proxy itself.
31/// \a is_displayed() and \a has_colors() are forwarded to the proxy. \a
32/// changeColor(), resetColor(), and \a reverseColor() are not forwarded, since
33/// they need to call \a flush() and the buffer lives in the proxy.
34template <class RawOstreamT = raw_ostream>
35class raw_ostream_proxy_adaptor : public RawOstreamT {
36 void write_impl(const char *Ptr, size_t Size) override {
38 }
39 uint64_t current_pos() const override { return getProxiedOS().tell(); }
40 size_t preferred_buffer_size() const override {
42 }
43
44public:
45 void reserveExtraSpace(uint64_t ExtraSize) override {
47 }
48 bool is_displayed() const override { return getProxiedOS().is_displayed(); }
49 bool has_colors() const override { return getProxiedOS().has_colors(); }
50 void enable_colors(bool enable) override {
51 RawOstreamT::enable_colors(enable);
53 }
54 bool hasProxiedOS() const { return OS; }
56 assert(OS && "raw_ostream_proxy_adaptor use after reset");
57 return *OS;
58 }
59 size_t getPreferredBufferSize() const { return PreferredBufferSize; }
60
62
63protected:
64 template <class... ArgsT>
65 explicit raw_ostream_proxy_adaptor(raw_ostream &OS, ArgsT &&...Args)
66 : RawOstreamT(std::forward<ArgsT>(Args)...), OS(&OS),
67 PreferredBufferSize(OS.GetBufferSize()) {
68 // Drop OS's buffer to make this->flush() forward. This proxy will add a
69 // buffer in its place.
70 OS.SetUnbuffered();
71 }
72
73 /// Stop proxying the stream. Flush and set up a crash for future writes.
74 ///
75 /// For example, this can simplify logic when a subclass might have a longer
76 /// lifetime than the stream it proxies.
78 this->SetUnbuffered();
79 OS = nullptr;
80 }
81
82private:
83 raw_ostream *OS;
84
85 /// Caches the value of OS->GetBufferSize() at construction time.
86 size_t PreferredBufferSize;
87};
88
89/// Adaptor for creating a stream that proxies a \a raw_pwrite_stream.
90template <class RawPwriteStreamT = raw_pwrite_stream>
92 : public raw_ostream_proxy_adaptor<RawPwriteStreamT> {
93 using RawOstreamAdaptorT = raw_ostream_proxy_adaptor<RawPwriteStreamT>;
94
95 void pwrite_impl(const char *Ptr, size_t Size, uint64_t Offset) override {
96 this->flush();
98 }
99
100protected:
102 template <class... ArgsT>
104 ArgsT &&...Args)
105 : RawOstreamAdaptorT(OS, std::forward<ArgsT>(Args)...) {}
106
110};
111
112/// Non-owning proxy for a \a raw_ostream. Enables passing a stream into an
113/// API that takes ownership.
115 LLVM_ABI_FOR_TEST void anchor() override;
116
117public:
119};
120
121/// Non-owning proxy for a \a raw_pwrite_stream. Enables passing a stream
122/// into an API that takes ownership.
130
131} // end namespace llvm
132
133#endif // LLVM_SUPPORT_RAW_OSTREAM_PROXY_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_ABI_FOR_TEST
Definition Compiler.h:218
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:867