14 #ifndef WPIUTIL_WPI_RAW_OSTREAM_H 15 #define WPIUTIL_WPI_RAW_OSTREAM_H 17 #include "wpi/ArrayRef.h" 18 #include "wpi/SmallVector.h" 19 #include "wpi/StringRef.h" 26 #include <system_error> 30 class format_object_base;
31 class FormattedString;
32 class FormattedNumber;
37 enum OpenFlags : unsigned;
65 char *OutBufStart, *OutBufEnd, *OutBufCur;
88 : BufferMode(unbuffered ? Unbuffered : InternalBuffer) {
90 OutBufStart = OutBufEnd = OutBufCur =
nullptr;
99 uint64_t
tell()
const {
return current_pos() + GetNumBytesInBuffer(); }
112 SetBufferAndMode(
new char[Size], Size, InternalBuffer);
115 size_t GetBufferSize()
const {
118 if (BufferMode != Unbuffered && OutBufStart ==
nullptr)
119 return preferred_buffer_size();
122 return OutBufEnd - OutBufStart;
130 SetBufferAndMode(
nullptr, 0, Unbuffered);
133 size_t GetNumBytesInBuffer()
const {
134 return OutBufCur - OutBufStart;
142 if (OutBufCur != OutBufStart)
147 if (OutBufCur >= OutBufEnd)
154 if (OutBufCur >= OutBufEnd)
161 if (OutBufCur >= OutBufEnd)
169 size_t Size = Arr.size();
172 if (Size > (
size_t)(OutBufEnd - OutBufCur))
173 return write(Arr.data(), Size);
176 memcpy(OutBufCur, Arr.data(), Size);
184 size_t Size = Str.
size();
187 if (Size > (
size_t)(OutBufEnd - OutBufCur))
188 return write(Str.
data(), Size);
191 memcpy(OutBufCur, Str.
data(), Size);
206 return write(Str.data(), Str.length());
209 raw_ostream &operator<<(const SmallVectorImpl<char> &Str) {
210 return write(Str.data(), Str.size());
213 raw_ostream &operator<<(const std::vector<uint8_t> &Arr) {
215 return write(Arr.data(), Arr.size());
218 raw_ostream &operator<<(const SmallVectorImpl<uint8_t> &Arr) {
219 return write(Arr.data(), Arr.size());
229 return this->operator<<(static_cast<unsigned long>(N));
233 return this->operator<<(static_cast<long>(N));
247 raw_ostream &write(
const uint8_t *Ptr,
size_t Size) {
248 return write(reinterpret_cast<const char *>(Ptr), Size);
318 virtual void write_impl(
const char *Ptr,
size_t Size) = 0;
321 virtual void handle();
325 virtual uint64_t current_pos()
const = 0;
332 SetBufferAndMode(BufferStart, Size, ExternalBuffer);
336 virtual size_t preferred_buffer_size()
const;
347 void SetBufferAndMode(
char *BufferStart,
size_t Size, BufferKind Mode);
351 void flush_nonempty();
355 void copy_to_buffer(
const char *Ptr,
size_t Size);
357 virtual void anchor();
364 virtual void pwrite_impl(
const char *Ptr,
size_t Size, uint64_t Offset) = 0;
365 void anchor()
override;
370 void pwrite(
const char *Ptr,
size_t Size, uint64_t Offset) {
372 uint64_t Pos = tell();
376 assert(Size + Offset <= Pos &&
"We don't support extending the stream");
378 pwrite_impl(Ptr, Size, Offset);
396 bool SupportsSeeking;
399 void write_impl(
const char *Ptr,
size_t Size)
override;
401 void pwrite_impl(
const char *Ptr,
size_t Size, uint64_t Offset)
override;
405 uint64_t current_pos()
const override {
return pos; }
408 size_t preferred_buffer_size()
const override;
411 void error_detected(std::error_code EC) { this->EC = EC; }
413 void anchor()
override;
427 sys::fs::OpenFlags Flags);
439 bool supportsSeeking() {
return SupportsSeeking; }
443 uint64_t seek(uint64_t off);
445 std::error_code error()
const {
return EC; }
486 void write_impl(
const char *Ptr,
size_t Size)
override;
490 uint64_t current_pos()
const override {
return OS.size(); }
513 void write_impl(
const char *Ptr,
size_t Size)
override;
515 void pwrite_impl(
const char *Ptr,
size_t Size, uint64_t Offset)
override;
518 uint64_t current_pos()
const override;
531 void flush() =
delete;
543 std::vector<char> &OS;
546 void write_impl(
const char *Ptr,
size_t Size)
override;
548 void pwrite_impl(
const char *Ptr,
size_t Size, uint64_t Offset)
override;
551 uint64_t current_pos()
const override;
564 void flush() =
delete;
579 void write_impl(
const char *Ptr,
size_t Size)
override;
581 void pwrite_impl(
const char *Ptr,
size_t Size, uint64_t Offset)
override;
584 uint64_t current_pos()
const override;
597 void flush() =
delete;
609 std::vector<uint8_t> &OS;
612 void write_impl(
const char *Ptr,
size_t Size)
override;
614 void pwrite_impl(
const char *Ptr,
size_t Size, uint64_t Offset)
override;
617 uint64_t current_pos()
const override;
630 void flush() =
delete;
640 void write_impl(
const char *Ptr,
size_t size)
override;
641 void pwrite_impl(
const char *Ptr,
size_t Size, uint64_t Offset)
override;
645 uint64_t current_pos()
const override;
663 #endif // LLVM_SUPPORT_RAW_OSTREAM_H
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
raw_uvector_ostream(std::vector< uint8_t > &O)
Construct a new raw_svector_ostream.
Definition: raw_ostream.h:624
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:575
virtual bool is_displayed() const
This function determines if this stream is connected to a "tty" or "console" window.
Definition: raw_ostream.h:295
StringRef str()
Return a StringRef for the vector contents.
Definition: raw_ostream.h:534
raw_usvector_ostream(SmallVectorImpl< uint8_t > &O)
Construct a new raw_svector_ostream.
Definition: raw_ostream.h:591
void SetUnbuffered()
Set the stream to be unbuffered.
Definition: raw_ostream.h:128
virtual raw_ostream & reverseColor()
Reverses the foreground and background colors.
Definition: raw_ostream.h:290
raw_ostream & nulls()
This returns a reference to a raw_ostream which simply discards output.
Definition: raw_ostream.cpp:692
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:388
A raw_ostream that discards all output.
Definition: raw_ostream.h:638
ArrayRef< uint8_t > array()
Return an ArrayRef for the vector contents.
Definition: raw_ostream.h:600
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE const char * data() const noexcept
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:128
namespace to hold default to_json function
Definition: json_binary_writer.cpp:39
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:509
uint64_t tell() const
tell - Return the current offset with the file.
Definition: raw_ostream.h:99
A raw_ostream that writes to a vector.
Definition: raw_ostream.h:608
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:482
ArrayRef< uint8_t > array()
Return a StringRef for the vector contents.
Definition: raw_ostream.h:633
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
Definition: raw_ostream.cpp:685
void SetBuffer(char *BufferStart, size_t Size)
Use the provided buffer as the raw_ostream buffer.
Definition: raw_ostream.h:331
virtual raw_ostream & resetColor()
Resets the colors to terminal defaults.
Definition: raw_ostream.h:287
StringRef str()
Return a StringRef for the vector contents.
Definition: raw_ostream.h:567
virtual bool has_colors() const
This function determines if this stream is displayed and supports colors.
Definition: raw_ostream.h:298
Definition: raw_ostream.h:652
const char * getBufferStart() const
Return the beginning of the current stream buffer, or 0 if the stream is unbuffered.
Definition: raw_ostream.h:340
A raw_ostream that writes to a vector.
Definition: raw_ostream.h:542
void SetBufferSize(size_t Size)
Set the stream to be buffered, using the specified buffer size.
Definition: raw_ostream.h:110
virtual raw_ostream & changeColor(enum Colors Color, bool Bold=false, bool BG=false)
Changes the foreground color of text that will be output from this point forward. ...
Definition: raw_ostream.h:276
bool has_error() const
Return the value of the flag in this raw_fd_ostream indicating whether an output error has been encou...
Definition: raw_ostream.h:451
raw_ostream & outs()
This returns a reference to a raw_ostream for standard output.
Definition: raw_ostream.cpp:675
auto size(R &&Range, typename std::enable_if< std::is_same< typename std::iterator_traits< decltype(Range.begin())>::iterator_category, std::random_access_iterator_tag >::value, void >::type *=nullptr) -> decltype(std::distance(Range.begin(), Range.end()))
Get the size of a range.
Definition: STLExtras.h:999
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const noexcept
size - Get the string size.
Definition: StringRef.h:138
raw_ostream & operator<<(raw_ostream &o, const json &j)
Definition: json_serializer.cpp:1519
std::string & str()
Flushes the stream contents to the target string and returns the string's reference.
Definition: raw_ostream.h:498
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:363
pointer data()
Return a pointer to the vector's buffer, even if empty().
Definition: SmallVector.h:151
raw_svector_ostream(SmallVectorImpl< char > &O)
Construct a new raw_svector_ostream.
Definition: raw_ostream.h:525
raw_vector_ostream(std::vector< char > &O)
Construct a new raw_svector_ostream.
Definition: raw_ostream.h:558
void clear_error()
Set the flag read by has_error() to false.
Definition: raw_ostream.h:462