13#ifndef WPIUTIL_WPI_RAW_OSTREAM_H
14#define WPIUTIL_WPI_RAW_OSTREAM_H
23#if __cplusplus > 201402L
26#include <system_error>
73 char *OutBufStart, *OutBufEnd, *OutBufCur;
79 enum class BufferKind {
113 : Kind(K), BufferMode(unbuffered ? BufferKind::Unbuffered
114 : BufferKind::InternalBuffer) {
116 OutBufStart = OutBufEnd = OutBufCur =
nullptr;
147 SetBufferAndMode(
new char[Size], Size, BufferKind::InternalBuffer);
153 if (BufferMode != BufferKind::Unbuffered && OutBufStart ==
nullptr)
157 return OutBufEnd - OutBufStart;
165 SetBufferAndMode(
nullptr, 0, BufferKind::Unbuffered);
169 return OutBufCur - OutBufStart;
177 if (OutBufCur != OutBufStart)
182 if (OutBufCur >= OutBufEnd)
189 if (OutBufCur >= OutBufEnd)
196 if (OutBufCur >= OutBufEnd)
204 size_t Size = Arr.size();
207 if (Size > (
size_t)(OutBufEnd - OutBufCur))
208 return write(Arr.data(), Size);
211 memcpy(OutBufCur, Arr.data(), Size);
219 size_t Size = Str.size();
222 if (Size > (
size_t)(OutBufEnd - OutBufCur))
223 return write(Str.data(), Size);
226 memcpy(OutBufCur, Str.data(), Size);
241 return write(Str.data(), Str.length());
250 return write(Arr.data(), Arr.size());
264 return write(
reinterpret_cast<const char *
>(Ptr), Size);
332 virtual void write_impl(
const char *Ptr,
size_t Size) = 0;
336 virtual uint64_t current_pos()
const = 0;
343 SetBufferAndMode(BufferStart, Size, BufferKind::ExternalBuffer);
358 void SetBufferAndMode(
char *BufferStart,
size_t Size, BufferKind Mode);
362 void flush_nonempty();
366 void copy_to_buffer(
const char *Ptr,
size_t Size);
369 void flush_tied_then_write(
const char *Ptr,
size_t Size);
371 virtual void anchor();
376template <
typename OStream,
typename T>
377std::enable_if_t<!std::is_reference<OStream>::value &&
378 std::is_base_of<raw_ostream, OStream>::value,
382 return std::move(OS);
389 virtual void pwrite_impl(
const char *Ptr,
size_t Size,
uint64_t Offset) = 0;
390 void anchor()
override;
402 assert(Size + Offset <= Pos &&
"We don't support extending the stream");
404 pwrite_impl(Ptr, Size, Offset);
417 bool SupportsSeeking =
false;
418 bool IsRegularFile =
false;
423 bool IsWindowsConsole =
false;
431 void write_impl(
const char *Ptr,
size_t Size)
override;
433 void pwrite_impl(
const char *Ptr,
size_t Size,
uint64_t Offset)
override;
437 uint64_t current_pos()
const override {
return pos; }
440 size_t preferred_buffer_size()
const override;
442 void anchor()
override;
494 std::error_code
error()
const {
return EC; }
558 void write_impl(
const char *Ptr,
size_t Size)
override;
562 uint64_t current_pos()
const override {
return OS.size(); }
572 std::string &
str() {
return OS; }
575 OS.reserve(
tell() + ExtraSize);
588 void write_impl(
const char *Ptr,
size_t Size)
override;
590 void pwrite_impl(
const char *Ptr,
size_t Size,
uint64_t Offset)
override;
593 uint64_t current_pos()
const override;
622 std::vector<char> &OS;
625 void write_impl(
const char *Ptr,
size_t Size)
override;
627 void pwrite_impl(
const char *Ptr,
size_t Size,
uint64_t Offset)
override;
630 uint64_t current_pos()
const override;
658 void write_impl(
const char *Ptr,
size_t Size)
override;
660 void pwrite_impl(
const char *Ptr,
size_t Size,
uint64_t Offset)
override;
663 uint64_t current_pos()
const override;
689 std::vector<uint8_t> &OS;
692 void write_impl(
const char *Ptr,
size_t Size)
override;
694 void pwrite_impl(
const char *Ptr,
size_t Size,
uint64_t Offset)
override;
697 uint64_t current_pos()
const override;
713 std::span<uint8_t>
array() {
return {OS.data(), OS.size()}; }
714 std::span<const uint8_t>
array()
const {
return {OS.data(), OS.size()}; }
721 void write_impl(
const char *Ptr,
size_t size)
override;
722 void pwrite_impl(
const char *Ptr,
size_t Size,
uint64_t Offset)
override;
726 uint64_t current_pos()
const override;
737 virtual void anchor()
override;
745 std::unique_ptr<raw_ostream> OS;
748 virtual void anchor()
override;
void reserve(size_type N)
Definition: SmallVector.h:647
size_t size() const
Definition: SmallVector.h:78
pointer data()
Return a pointer to the vector's buffer, even if empty().
Definition: SmallVector.h:271
Definition: raw_ostream.h:733
~buffer_ostream() override
Definition: raw_ostream.h:741
buffer_ostream(raw_ostream &OS)
Definition: raw_ostream.h:740
Definition: raw_ostream.h:744
buffer_unique_ostream(std::unique_ptr< raw_ostream > OS)
Definition: raw_ostream.h:751
~buffer_unique_ostream() override
Definition: raw_ostream.h:757
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:414
std::error_code error() const
Definition: raw_ostream.h:494
bool isRegularFile() const
Definition: raw_ostream.h:488
raw_fd_ostream(std::string_view Filename, std::error_code &EC)
Open the specified file for writing.
void inc_pos(uint64_t Delta)
Definition: raw_ostream.h:452
void close()
Manually flush the stream and close the file.
~raw_fd_ostream() override
raw_fd_ostream(std::string_view Filename, std::error_code &EC, fs::OpenFlags Flags)
int get_fd() const
Return the file descriptor.
Definition: raw_ostream.h:449
uint64_t seek(uint64_t off)
Flushes the stream and repositions the underlying file descriptor position to the offset specified fr...
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:500
raw_fd_ostream(std::string_view Filename, std::error_code &EC, fs::FileAccess Access)
raw_fd_ostream(std::string_view Filename, std::error_code &EC, fs::CreationDisposition Disp)
raw_fd_ostream(int fd, bool shouldClose, bool unbuffered=false, OStreamKind K=OStreamKind::OK_OStream)
FD is the file descriptor that this writes to.
bool supportsSeeking() const
Definition: raw_ostream.h:486
raw_fd_ostream(std::string_view Filename, std::error_code &EC, fs::CreationDisposition Disp, fs::FileAccess Access, fs::OpenFlags Flags)
void clear_error()
Set the flag read by has_error() to false.
Definition: raw_ostream.h:511
void error_detected(std::error_code EC)
Set the flag indicating that an output error has been encountered.
Definition: raw_ostream.h:446
A raw_ostream of a file for reading/writing/seeking.
Definition: raw_ostream.h:534
raw_fd_stream(std::string_view Filename, std::error_code &EC)
Open the specified file for reading/writing/seeking.
static bool classof(const raw_ostream *OS)
Check if OS is a pointer of type raw_fd_stream*.
A raw_ostream that discards all output.
Definition: raw_ostream.h:719
raw_null_ostream()=default
~raw_null_ostream() override
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:44
static constexpr Colors BLACK
Definition: raw_ostream.h:100
OStreamKind get_kind() const
Definition: raw_ostream.h:127
static constexpr Colors MAGENTA
Definition: raw_ostream.h:105
static constexpr Colors CYAN
Definition: raw_ostream.h:106
virtual void enable_colors(bool)
Definition: raw_ostream.h:306
raw_ostream & operator<<(const char *Str)
Definition: raw_ostream.h:232
void flush()
Definition: raw_ostream.h:176
raw_ostream & write(const char *Ptr, size_t Size)
const char * getBufferStart() const
Return the beginning of the current stream buffer, or 0 if the stream is unbuffered.
Definition: raw_ostream.h:351
void SetBufferSize(size_t Size)
Set the stream to be buffered, using the specified buffer size.
Definition: raw_ostream.h:145
virtual void reserveExtraSpace(uint64_t ExtraSize)
If possible, pre-allocate ExtraSize bytes for stream data.
Definition: raw_ostream.h:138
bool colors_enabled() const
Definition: raw_ostream.h:308
raw_ostream & operator<<(unsigned char C)
Definition: raw_ostream.h:188
raw_ostream & operator<<(std::span< const uint8_t > Arr)
Definition: raw_ostream.h:202
raw_ostream & operator<<(signed char C)
Definition: raw_ostream.h:195
raw_ostream(const raw_ostream &)=delete
virtual bool has_colors() const
This function determines if this stream is displayed and supports colors.
Definition: raw_ostream.h:302
size_t GetNumBytesInBuffer() const
Definition: raw_ostream.h:168
raw_ostream & write(const uint8_t *Ptr, size_t Size)
Definition: raw_ostream.h:263
static constexpr Colors RESET
Definition: raw_ostream.h:109
void SetBuffered()
Set the stream to be buffered, with an automatically determined buffer size.
void SetBuffer(char *BufferStart, size_t Size)
Use the provided buffer as the raw_ostream buffer.
Definition: raw_ostream.h:342
raw_ostream & write_escaped(std::string_view Str, bool UseHexEscapes=false)
Output Str, turning '\', '\t', ' ', '"', and anything that doesn't satisfy wpi::isPrint into an escap...
virtual size_t preferred_buffer_size() const
Return an efficient buffer size for the underlying output mechanism.
static constexpr Colors WHITE
Definition: raw_ostream.h:107
raw_ostream & operator<<(const SmallVectorImpl< uint8_t > &Arr)
Definition: raw_ostream.h:253
void tie(raw_ostream *TieTo)
Tie this stream to the specified stream.
Definition: raw_ostream.h:312
raw_ostream & operator<<(const std::vector< uint8_t > &Arr)
Definition: raw_ostream.h:248
raw_ostream & write_zeros(unsigned NumZeros)
write_zeros - Insert 'NumZeros' nulls.
raw_ostream(bool unbuffered=false, OStreamKind K=OStreamKind::OK_OStream)
Definition: raw_ostream.h:111
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
void SetUnbuffered()
Set the stream to be unbuffered.
Definition: raw_ostream.h:163
static constexpr Colors GREEN
Definition: raw_ostream.h:102
static constexpr Colors BLUE
Definition: raw_ostream.h:104
static constexpr Colors YELLOW
Definition: raw_ostream.h:103
OStreamKind
Definition: raw_ostream.h:47
static constexpr Colors SAVEDCOLOR
Definition: raw_ostream.h:108
virtual bool is_displayed() const
This function determines if this stream is connected to a "tty" or "console" window.
Definition: raw_ostream.h:298
size_t GetBufferSize() const
Definition: raw_ostream.h:150
raw_ostream & operator<<(std::string_view Str)
Definition: raw_ostream.h:217
Colors
Definition: raw_ostream.h:87
raw_ostream & operator<<(const std::string &Str)
Definition: raw_ostream.h:239
raw_ostream & operator<<(char C)
Definition: raw_ostream.h:181
virtual raw_ostream & resetColor()
Resets the colors to terminal defaults.
Definition: raw_ostream.h:290
raw_ostream & write(unsigned char C)
uint64_t tell() const
tell - Return the current offset with the file.
Definition: raw_ostream.h:125
void operator=(const raw_ostream &)=delete
virtual raw_ostream & reverseColor()
Reverses the foreground and background colors.
Definition: raw_ostream.h:293
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:280
raw_ostream & operator<<(const SmallVectorImpl< char > &Str)
Definition: raw_ostream.h:244
static constexpr Colors RED
Definition: raw_ostream.h:101
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:388
void pwrite(const char *Ptr, size_t Size, uint64_t Offset)
Definition: raw_ostream.h:396
raw_pwrite_stream(bool Unbuffered=false, OStreamKind K=OStreamKind::OK_OStream)
Definition: raw_ostream.h:393
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:554
std::string & str()
Returns the string's reference.
Definition: raw_ostream.h:572
void reserveExtraSpace(uint64_t ExtraSize) override
If possible, pre-allocate ExtraSize bytes for stream data.
Definition: raw_ostream.h:574
raw_string_ostream(std::string &O)
Definition: raw_ostream.h:565
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:584
raw_svector_ostream(SmallVectorImpl< char > &O)
Construct a new raw_svector_ostream.
Definition: raw_ostream.h:600
~raw_svector_ostream() override=default
void reserveExtraSpace(uint64_t ExtraSize) override
If possible, pre-allocate ExtraSize bytes for stream data.
Definition: raw_ostream.h:611
std::string_view str() const
Return a std::string_view for the vector contents.
Definition: raw_ostream.h:609
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:654
raw_usvector_ostream(SmallVectorImpl< uint8_t > &O)
Construct a new raw_svector_ostream.
Definition: raw_ostream.h:670
std::span< uint8_t > array()
Return an std::span for the vector contents.
Definition: raw_ostream.h:679
std::span< const uint8_t > array() const
Definition: raw_ostream.h:680
~raw_usvector_ostream() override=default
A raw_ostream that writes to a vector.
Definition: raw_ostream.h:688
~raw_uvector_ostream() override=default
std::span< const uint8_t > array() const
Definition: raw_ostream.h:714
raw_uvector_ostream(std::vector< uint8_t > &O)
Construct a new raw_svector_ostream.
Definition: raw_ostream.h:704
std::span< uint8_t > array()
Return a std::span for the vector contents.
Definition: raw_ostream.h:713
A raw_ostream that writes to a vector.
Definition: raw_ostream.h:621
std::string_view str()
Return a std::string_view for the vector contents.
Definition: raw_ostream.h:646
~raw_vector_ostream() override=default
raw_vector_ostream(std::vector< char > &O)
Construct a new raw_svector_ostream.
Definition: raw_ostream.h:637
basic_string_view< char > string_view
Definition: core.h:520
EIGEN_CONSTEXPR Index size(const T &x)
Definition: Meta.h:479
::uint64_t uint64_t
Definition: Meta.h:58
::uint8_t uint8_t
Definition: Meta.h:52
OpenFlags
Definition: fs.h:90
FileAccess
Definition: fs.h:85
CreationDisposition
Definition: fs.h:63
Definition: StdDeque.h:50
/file This file defines the SmallVector class.
Definition: AprilTagFieldLayout.h:18
raw_ostream & nulls()
This returns a reference to a raw_ostream which simply discards output.
raw_fd_ostream & outs()
This returns a reference to a raw_fd_ostream for standard output.
raw_ostream & operator<<(raw_ostream &OS, sys::TimePoint<> TP)
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.