WPILibC++ 2023.4.3
raw_ostream.h
Go to the documentation of this file.
1//===--- raw_ostream.h - Raw output stream ----------------------*- 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// This file defines the raw_ostream class.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef WPIUTIL_WPI_RAW_OSTREAM_H
14#define WPIUTIL_WPI_RAW_OSTREAM_H
15
16#include "wpi/SmallVector.h"
17#include <span>
18#include <cassert>
19#include <cstddef>
20#include <cstdint>
21#include <cstring>
22#include <string>
23#if __cplusplus > 201402L
24#include <string_view>
25#endif
26#include <system_error>
27#include <type_traits>
28#include <vector>
29
30
31namespace fs {
32enum FileAccess : unsigned;
33enum OpenFlags : unsigned;
34enum CreationDisposition : unsigned;
35class FileLocker;
36} // end namespace fs
37
38namespace wpi {
39
40/// This class implements an extremely fast bulk output stream that can *only*
41/// output to a stream. It does not support seeking, reopening, rewinding, line
42/// buffered disciplines etc. It is a simple buffer that outputs
43/// a chunk at a time.
45public:
46 // Class kinds to support LLVM-style RTTI.
47 enum class OStreamKind {
48 OK_OStream,
49 OK_FDStream,
50 };
51
52private:
53 OStreamKind Kind;
54
55 /// The buffer is handled in such a way that the buffer is
56 /// uninitialized, unbuffered, or out of space when OutBufCur >=
57 /// OutBufEnd. Thus a single comparison suffices to determine if we
58 /// need to take the slow path to write a single character.
59 ///
60 /// The buffer is in one of three states:
61 /// 1. Unbuffered (BufferMode == Unbuffered)
62 /// 1. Uninitialized (BufferMode != Unbuffered && OutBufStart == 0).
63 /// 2. Buffered (BufferMode != Unbuffered && OutBufStart != 0 &&
64 /// OutBufEnd - OutBufStart >= 1).
65 ///
66 /// If buffered, then the raw_ostream owns the buffer if (BufferMode ==
67 /// InternalBuffer); otherwise the buffer has been set via SetBuffer and is
68 /// managed by the subclass.
69 ///
70 /// If a subclass installs an external buffer using SetBuffer then it can wait
71 /// for a \see write_impl() call to handle the data which has been put into
72 /// this buffer.
73 char *OutBufStart, *OutBufEnd, *OutBufCur;
74
75 /// Optional stream this stream is tied to. If this stream is written to, the
76 /// tied-to stream will be flushed first.
77 raw_ostream *TiedStream = nullptr;
78
79 enum class BufferKind {
80 Unbuffered = 0,
81 InternalBuffer,
82 ExternalBuffer
83 } BufferMode;
84
85public:
86 // color order matches ANSI escape sequence, don't change
87 enum class Colors {
88 BLACK = 0,
89 RED,
90 GREEN,
91 YELLOW,
92 BLUE,
93 MAGENTA,
94 CYAN,
95 WHITE,
97 RESET,
98 };
99
100 static constexpr Colors BLACK = Colors::BLACK;
101 static constexpr Colors RED = Colors::RED;
102 static constexpr Colors GREEN = Colors::GREEN;
103 static constexpr Colors YELLOW = Colors::YELLOW;
104 static constexpr Colors BLUE = Colors::BLUE;
105 static constexpr Colors MAGENTA = Colors::MAGENTA;
106 static constexpr Colors CYAN = Colors::CYAN;
107 static constexpr Colors WHITE = Colors::WHITE;
109 static constexpr Colors RESET = Colors::RESET;
110
111 explicit raw_ostream(bool unbuffered = false,
113 : Kind(K), BufferMode(unbuffered ? BufferKind::Unbuffered
114 : BufferKind::InternalBuffer) {
115 // Start out ready to flush.
116 OutBufStart = OutBufEnd = OutBufCur = nullptr;
117 }
118
119 raw_ostream(const raw_ostream &) = delete;
120 void operator=(const raw_ostream &) = delete;
121
122 virtual ~raw_ostream();
123
124 /// tell - Return the current offset with the file.
125 uint64_t tell() const { return current_pos() + GetNumBytesInBuffer(); }
126
127 OStreamKind get_kind() const { return Kind; }
128
129 //===--------------------------------------------------------------------===//
130 // Configuration Interface
131 //===--------------------------------------------------------------------===//
132
133 /// If possible, pre-allocate \p ExtraSize bytes for stream data.
134 /// i.e. it extends internal buffers to keep additional ExtraSize bytes.
135 /// So that the stream could keep at least tell() + ExtraSize bytes
136 /// without re-allocations. reserveExtraSpace() does not change
137 /// the size/data of the stream.
138 virtual void reserveExtraSpace(uint64_t ExtraSize) {}
139
140 /// Set the stream to be buffered, with an automatically determined buffer
141 /// size.
143
144 /// Set the stream to be buffered, using the specified buffer size.
145 void SetBufferSize(size_t Size) {
146 flush();
147 SetBufferAndMode(new char[Size], Size, BufferKind::InternalBuffer);
148 }
149
150 size_t GetBufferSize() const {
151 // If we're supposed to be buffered but haven't actually gotten around
152 // to allocating the buffer yet, return the value that would be used.
153 if (BufferMode != BufferKind::Unbuffered && OutBufStart == nullptr)
154 return preferred_buffer_size();
155
156 // Otherwise just return the size of the allocated buffer.
157 return OutBufEnd - OutBufStart;
158 }
159
160 /// Set the stream to be unbuffered. When unbuffered, the stream will flush
161 /// after every write. This routine will also flush the buffer immediately
162 /// when the stream is being set to unbuffered.
164 flush();
165 SetBufferAndMode(nullptr, 0, BufferKind::Unbuffered);
166 }
167
168 size_t GetNumBytesInBuffer() const {
169 return OutBufCur - OutBufStart;
170 }
171
172 //===--------------------------------------------------------------------===//
173 // Data Output Interface
174 //===--------------------------------------------------------------------===//
175
176 void flush() {
177 if (OutBufCur != OutBufStart)
178 flush_nonempty();
179 }
180
182 if (OutBufCur >= OutBufEnd)
183 return write(C);
184 *OutBufCur++ = C;
185 return *this;
186 }
187
188 raw_ostream &operator<<(unsigned char C) {
189 if (OutBufCur >= OutBufEnd)
190 return write(C);
191 *OutBufCur++ = C;
192 return *this;
193 }
194
195 raw_ostream &operator<<(signed char C) {
196 if (OutBufCur >= OutBufEnd)
197 return write(C);
198 *OutBufCur++ = C;
199 return *this;
200 }
201
202 raw_ostream &operator<<(std::span<const uint8_t> Arr) {
203 // Inline fast path, particularly for arrays with a known length.
204 size_t Size = Arr.size();
205
206 // Make sure we can use the fast path.
207 if (Size > (size_t)(OutBufEnd - OutBufCur))
208 return write(Arr.data(), Size);
209
210 if (Size) {
211 memcpy(OutBufCur, Arr.data(), Size);
212 OutBufCur += Size;
213 }
214 return *this;
215 }
216
218 // Inline fast path, particularly for strings with a known length.
219 size_t Size = Str.size();
220
221 // Make sure we can use the fast path.
222 if (Size > (size_t)(OutBufEnd - OutBufCur))
223 return write(Str.data(), Size);
224
225 if (Size) {
226 memcpy(OutBufCur, Str.data(), Size);
227 OutBufCur += Size;
228 }
229 return *this;
230 }
231
232 raw_ostream &operator<<(const char *Str) {
233 // Inline fast path, particularly for constant strings where a sufficiently
234 // smart compiler will simplify strlen.
235
236 return this->operator<<(std::string_view(Str));
237 }
238
239 raw_ostream &operator<<(const std::string &Str) {
240 // Avoid the fast path, it would only increase code size for a marginal win.
241 return write(Str.data(), Str.length());
242 }
243
245 return write(Str.data(), Str.size());
246 }
247
248 raw_ostream &operator<<(const std::vector<uint8_t> &Arr) {
249 // Avoid the fast path, it would only increase code size for a marginal win.
250 return write(Arr.data(), Arr.size());
251 }
252
254 return write(Arr.data(), Arr.size());
255 }
256
257 /// Output \p Str, turning '\\', '\t', '\n', '"', and anything that doesn't
258 /// satisfy wpi::isPrint into an escape sequence.
259 raw_ostream &write_escaped(std::string_view Str, bool UseHexEscapes = false);
260
261 raw_ostream &write(unsigned char C);
262 raw_ostream &write(const char *Ptr, size_t Size);
263 raw_ostream &write(const uint8_t *Ptr, size_t Size) {
264 return write(reinterpret_cast<const char *>(Ptr), Size);
265 }
266
267 /// indent - Insert 'NumSpaces' spaces.
268 raw_ostream &indent(unsigned NumSpaces);
269
270 /// write_zeros - Insert 'NumZeros' nulls.
271 raw_ostream &write_zeros(unsigned NumZeros);
272
273 /// Changes the foreground color of text that will be output from this point
274 /// forward.
275 /// @param Color ANSI color to use, the special SAVEDCOLOR can be used to
276 /// change only the bold attribute, and keep colors untouched
277 /// @param Bold bold/brighter text, default false
278 /// @param BG if true change the background, default: change foreground
279 /// @returns itself so it can be used within << invocations
280 virtual raw_ostream &changeColor(enum Colors Color, bool Bold = false,
281 bool BG = false) {
282 (void)Color;
283 (void)Bold;
284 (void)BG;
285 return *this;
286 }
287
288 /// Resets the colors to terminal defaults. Call this when you are done
289 /// outputting colored text, or before program exit.
290 virtual raw_ostream &resetColor() { return *this; }
291
292 /// Reverses the foreground and background colors.
293 virtual raw_ostream &reverseColor() { return *this; }
294
295 /// This function determines if this stream is connected to a "tty" or
296 /// "console" window. That is, the output would be displayed to the user
297 /// rather than being put on a pipe or stored in a file.
298 virtual bool is_displayed() const { return false; }
299
300 /// This function determines if this stream is displayed and supports colors.
301 /// The result is unaffected by calls to enable_color().
302 virtual bool has_colors() const { return is_displayed(); }
303
304 // Enable or disable colors. Once enable_colors(false) is called,
305 // changeColor() has no effect until enable_colors(true) is called.
306 virtual void enable_colors(bool /*enable*/) {}
307
308 bool colors_enabled() const { return false; }
309
310 /// Tie this stream to the specified stream. Replaces any existing tied-to
311 /// stream. Specifying a nullptr unties the stream.
312 void tie(raw_ostream *TieTo) { TiedStream = TieTo; }
313
314 //===--------------------------------------------------------------------===//
315 // Subclass Interface
316 //===--------------------------------------------------------------------===//
317
318private:
319 /// The is the piece of the class that is implemented by subclasses. This
320 /// writes the \p Size bytes starting at
321 /// \p Ptr to the underlying stream.
322 ///
323 /// This function is guaranteed to only be called at a point at which it is
324 /// safe for the subclass to install a new buffer via SetBuffer.
325 ///
326 /// \param Ptr The start of the data to be written. For buffered streams this
327 /// is guaranteed to be the start of the buffer.
328 ///
329 /// \param Size The number of bytes to be written.
330 ///
331 /// \invariant { Size > 0 }
332 virtual void write_impl(const char *Ptr, size_t Size) = 0;
333
334 /// Return the current position within the stream, not counting the bytes
335 /// currently in the buffer.
336 virtual uint64_t current_pos() const = 0;
337
338protected:
339 /// Use the provided buffer as the raw_ostream buffer. This is intended for
340 /// use only by subclasses which can arrange for the output to go directly
341 /// into the desired output buffer, instead of being copied on each flush.
342 void SetBuffer(char *BufferStart, size_t Size) {
343 SetBufferAndMode(BufferStart, Size, BufferKind::ExternalBuffer);
344 }
345
346 /// Return an efficient buffer size for the underlying output mechanism.
347 virtual size_t preferred_buffer_size() const;
348
349 /// Return the beginning of the current stream buffer, or 0 if the stream is
350 /// unbuffered.
351 const char *getBufferStart() const { return OutBufStart; }
352
353 //===--------------------------------------------------------------------===//
354 // Private Interface
355 //===--------------------------------------------------------------------===//
356private:
357 /// Install the given buffer and mode.
358 void SetBufferAndMode(char *BufferStart, size_t Size, BufferKind Mode);
359
360 /// Flush the current buffer, which is known to be non-empty. This outputs the
361 /// currently buffered data and resets the buffer to empty.
362 void flush_nonempty();
363
364 /// Copy data into the buffer. Size must not be greater than the number of
365 /// unused bytes in the buffer.
366 void copy_to_buffer(const char *Ptr, size_t Size);
367
368 /// Flush the tied-to stream (if present) and then write the required data.
369 void flush_tied_then_write(const char *Ptr, size_t Size);
370
371 virtual void anchor();
372};
373
374/// Call the appropriate insertion operator, given an rvalue reference to a
375/// raw_ostream object and return a stream of the same type as the argument.
376template <typename OStream, typename T>
377std::enable_if_t<!std::is_reference<OStream>::value &&
378 std::is_base_of<raw_ostream, OStream>::value,
379 OStream &&>
380operator<<(OStream &&OS, const T &Value) {
381 OS << Value;
382 return std::move(OS);
383}
384
385/// An abstract base class for streams implementations that also support a
386/// pwrite operation. This is useful for code that can mostly stream out data,
387/// but needs to patch in a header that needs to know the output size.
389 virtual void pwrite_impl(const char *Ptr, size_t Size, uint64_t Offset) = 0;
390 void anchor() override;
391
392public:
393 explicit raw_pwrite_stream(bool Unbuffered = false,
395 : raw_ostream(Unbuffered, K) {}
396 void pwrite(const char *Ptr, size_t Size, uint64_t Offset) {
397#ifndef NDEBUG
398 uint64_t Pos = tell();
399 // /dev/null always reports a pos of 0, so we cannot perform this check
400 // in that case.
401 if (Pos)
402 assert(Size + Offset <= Pos && "We don't support extending the stream");
403#endif
404 pwrite_impl(Ptr, Size, Offset);
405 }
406};
407
408//===----------------------------------------------------------------------===//
409// File Output Streams
410//===----------------------------------------------------------------------===//
411
412/// A raw_ostream that writes to a file descriptor.
413///
415 int FD;
416 bool ShouldClose;
417 bool SupportsSeeking = false;
418 bool IsRegularFile = false;
419
420#ifdef _WIN32
421 /// True if this fd refers to a Windows console device. Mintty and other
422 /// terminal emulators are TTYs, but they are not consoles.
423 bool IsWindowsConsole = false;
424#endif
425
426 std::error_code EC;
427
428 uint64_t pos = 0;
429
430 /// See raw_ostream::write_impl.
431 void write_impl(const char *Ptr, size_t Size) override;
432
433 void pwrite_impl(const char *Ptr, size_t Size, uint64_t Offset) override;
434
435 /// Return the current position within the stream, not counting the bytes
436 /// currently in the buffer.
437 uint64_t current_pos() const override { return pos; }
438
439 /// Determine an efficient buffer size.
440 size_t preferred_buffer_size() const override;
441
442 void anchor() override;
443
444protected:
445 /// Set the flag indicating that an output error has been encountered.
446 void error_detected(std::error_code EC) { this->EC = EC; }
447
448 /// Return the file descriptor.
449 int get_fd() const { return FD; }
450
451 // Update the file position by increasing \p Delta.
452 void inc_pos(uint64_t Delta) { pos += Delta; }
453
454public:
455 /// Open the specified file for writing. If an error occurs, information
456 /// about the error is put into EC, and the stream should be immediately
457 /// destroyed;
458 /// \p Flags allows optional flags to control how the file will be opened.
459 ///
460 /// As a special case, if Filename is "-", then the stream will use
461 /// STDOUT_FILENO instead of opening a file. This will not close the stdout
462 /// descriptor.
463 raw_fd_ostream(std::string_view Filename, std::error_code &EC);
464 raw_fd_ostream(std::string_view Filename, std::error_code &EC,
466 raw_fd_ostream(std::string_view Filename, std::error_code &EC,
467 fs::FileAccess Access);
468 raw_fd_ostream(std::string_view Filename, std::error_code &EC,
469 fs::OpenFlags Flags);
470 raw_fd_ostream(std::string_view Filename, std::error_code &EC,
472 fs::OpenFlags Flags);
473
474 /// FD is the file descriptor that this writes to. If ShouldClose is true,
475 /// this closes the file when the stream is destroyed. If FD is for stdout or
476 /// stderr, it will not be closed.
477 raw_fd_ostream(int fd, bool shouldClose, bool unbuffered = false,
479
480 ~raw_fd_ostream() override;
481
482 /// Manually flush the stream and close the file. Note that this does not call
483 /// fsync.
484 void close();
485
486 bool supportsSeeking() const { return SupportsSeeking; }
487
488 bool isRegularFile() const { return IsRegularFile; }
489
490 /// Flushes the stream and repositions the underlying file descriptor position
491 /// to the offset specified from the beginning of the file.
493
494 std::error_code error() const { return EC; }
495
496 /// Return the value of the flag in this raw_fd_ostream indicating whether an
497 /// output error has been encountered.
498 /// This doesn't implicitly flush any pending output. Also, it doesn't
499 /// guarantee to detect all errors unless the stream has been closed.
500 bool has_error() const { return bool(EC); }
501
502 /// Set the flag read by has_error() to false. If the error flag is set at the
503 /// time when this raw_ostream's destructor is called, report_fatal_error is
504 /// called to report the error. Use clear_error() after handling the error to
505 /// avoid this behavior.
506 ///
507 /// "Errors should never pass silently.
508 /// Unless explicitly silenced."
509 /// - from The Zen of Python, by Tim Peters
510 ///
511 void clear_error() { EC = std::error_code(); }
512};
513
514/// This returns a reference to a raw_fd_ostream for standard output. Use it
515/// like: outs() << "foo" << "bar";
517
518/// This returns a reference to a raw_ostream for standard error.
519/// Use it like: errs() << "foo" << "bar";
520/// By default, the stream is tied to stdout to ensure stdout is flushed before
521/// stderr is written, to ensure the error messages are written in their
522/// expected place.
524
525/// This returns a reference to a raw_ostream which simply discards output.
527
528//===----------------------------------------------------------------------===//
529// File Streams
530//===----------------------------------------------------------------------===//
531
532/// A raw_ostream of a file for reading/writing/seeking.
533///
535public:
536 /// Open the specified file for reading/writing/seeking. If an error occurs,
537 /// information about the error is put into EC, and the stream should be
538 /// immediately destroyed.
539 raw_fd_stream(std::string_view Filename, std::error_code &EC);
540
541 /// Check if \p OS is a pointer of type raw_fd_stream*.
542 static bool classof(const raw_ostream *OS);
543};
544
545//===----------------------------------------------------------------------===//
546// Output Stream Adaptors
547//===----------------------------------------------------------------------===//
548
549/// A raw_ostream that writes to an std::string. This is a simple adaptor
550/// class. This class does not encounter output errors.
551/// raw_string_ostream operates without a buffer, delegating all memory
552/// management to the std::string. Thus the std::string is always up-to-date,
553/// may be used directly and there is no need to call flush().
555 std::string &OS;
556
557 /// See raw_ostream::write_impl.
558 void write_impl(const char *Ptr, size_t Size) override;
559
560 /// Return the current position within the stream, not counting the bytes
561 /// currently in the buffer.
562 uint64_t current_pos() const override { return OS.size(); }
563
564public:
565 explicit raw_string_ostream(std::string &O) : OS(O) {
567 }
568
569 /// Returns the string's reference. In most cases it is better to simply use
570 /// the underlying std::string directly.
571 /// TODO: Consider removing this API.
572 std::string &str() { return OS; }
573
574 void reserveExtraSpace(uint64_t ExtraSize) override {
575 OS.reserve(tell() + ExtraSize);
576 }
577};
578
579/// A raw_ostream that writes to an SmallVector or SmallString. This is a
580/// simple adaptor class. This class does not encounter output errors.
581/// raw_svector_ostream operates without a buffer, delegating all memory
582/// management to the SmallString. Thus the SmallString is always up-to-date,
583/// may be used directly and there is no need to call flush().
586
587 /// See raw_ostream::write_impl.
588 void write_impl(const char *Ptr, size_t Size) override;
589
590 void pwrite_impl(const char *Ptr, size_t Size, uint64_t Offset) override;
591
592 /// Return the current position within the stream.
593 uint64_t current_pos() const override;
594
595public:
596 /// Construct a new raw_svector_ostream.
597 ///
598 /// \param O The vector to write to; this should generally have at least 128
599 /// bytes free to avoid any extraneous memory overhead.
602 }
603
604 ~raw_svector_ostream() override = default;
605
606 void flush() = delete;
607
608 /// Return a std::string_view for the vector contents.
609 std::string_view str() const { return std::string_view(OS.data(), OS.size()); }
610
611 void reserveExtraSpace(uint64_t ExtraSize) override {
612 OS.reserve(tell() + ExtraSize);
613 }
614};
615
616/// A raw_ostream that writes to a vector. This is a
617/// simple adaptor class. This class does not encounter output errors.
618/// raw_vector_ostream operates without a buffer, delegating all memory
619/// management to the vector. Thus the vector is always up-to-date,
620/// may be used directly and there is no need to call flush().
622 std::vector<char> &OS;
623
624 /// See raw_ostream::write_impl.
625 void write_impl(const char *Ptr, size_t Size) override;
626
627 void pwrite_impl(const char *Ptr, size_t Size, uint64_t Offset) override;
628
629 /// Return the current position within the stream.
630 uint64_t current_pos() const override;
631
632public:
633 /// Construct a new raw_svector_ostream.
634 ///
635 /// \param O The vector to write to; this should generally have at least 128
636 /// bytes free to avoid any extraneous memory overhead.
637 explicit raw_vector_ostream(std::vector<char> &O) : OS(O) {
639 }
640
641 ~raw_vector_ostream() override = default;
642
643 void flush() = delete;
644
645 /// Return a std::string_view for the vector contents.
646 std::string_view str() { return std::string_view(OS.data(), OS.size()); }
647};
648
649/// A raw_ostream that writes to an SmallVector or SmallString. This is a
650/// simple adaptor class. This class does not encounter output errors.
651/// raw_svector_ostream operates without a buffer, delegating all memory
652/// management to the SmallString. Thus the SmallString is always up-to-date,
653/// may be used directly and there is no need to call flush().
656
657 /// See raw_ostream::write_impl.
658 void write_impl(const char *Ptr, size_t Size) override;
659
660 void pwrite_impl(const char *Ptr, size_t Size, uint64_t Offset) override;
661
662 /// Return the current position within the stream.
663 uint64_t current_pos() const override;
664
665public:
666 /// Construct a new raw_svector_ostream.
667 ///
668 /// \param O The vector to write to; this should generally have at least 128
669 /// bytes free to avoid any extraneous memory overhead.
672 }
673
674 ~raw_usvector_ostream() override = default;
675
676 void flush() = delete;
677
678 /// Return an std::span for the vector contents.
679 std::span<uint8_t> array() { return {OS.data(), OS.size()}; }
680 std::span<const uint8_t> array() const { return {OS.data(), OS.size()}; }
681};
682
683/// A raw_ostream that writes to a vector. This is a
684/// simple adaptor class. This class does not encounter output errors.
685/// raw_vector_ostream operates without a buffer, delegating all memory
686/// management to the vector. Thus the vector is always up-to-date,
687/// may be used directly and there is no need to call flush().
689 std::vector<uint8_t> &OS;
690
691 /// See raw_ostream::write_impl.
692 void write_impl(const char *Ptr, size_t Size) override;
693
694 void pwrite_impl(const char *Ptr, size_t Size, uint64_t Offset) override;
695
696 /// Return the current position within the stream.
697 uint64_t current_pos() const override;
698
699public:
700 /// Construct a new raw_svector_ostream.
701 ///
702 /// \param O The vector to write to; this should generally have at least 128
703 /// bytes free to avoid any extraneous memory overhead.
704 explicit raw_uvector_ostream(std::vector<uint8_t> &O) : OS(O) {
706 }
707
708 ~raw_uvector_ostream() override = default;
709
710 void flush() = delete;
711
712 /// Return a std::span for the vector contents.
713 std::span<uint8_t> array() { return {OS.data(), OS.size()}; }
714 std::span<const uint8_t> array() const { return {OS.data(), OS.size()}; }
715};
716
717
718/// A raw_ostream that discards all output.
720 /// See raw_ostream::write_impl.
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;
723
724 /// Return the current position within the stream, not counting the bytes
725 /// currently in the buffer.
726 uint64_t current_pos() const override;
727
728public:
729 explicit raw_null_ostream() = default;
731};
732
734 raw_ostream &OS;
736
737 virtual void anchor() override;
738
739public:
741 ~buffer_ostream() override { OS << str(); }
742};
743
745 std::unique_ptr<raw_ostream> OS;
747
748 virtual void anchor() override;
749
750public:
751 buffer_unique_ostream(std::unique_ptr<raw_ostream> OS)
752 : raw_svector_ostream(Buffer), OS(std::move(OS)) {
753 // Turn off buffering on OS, which we now own, to avoid allocating a buffer
754 // when the destructor writes only to be immediately flushed again.
755 this->OS->SetUnbuffered();
756 }
757 ~buffer_unique_ostream() override { *OS << str(); }
758};
759
760} // end namespace wpi
761
762#endif // WPIUTIL_WPI_RAW_OSTREAM_H
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
virtual ~raw_ostream()
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
Definition: fs.h:44
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.