WPILibC++  2019.3.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
raw_ostream.h
1 //===--- raw_ostream.h - Raw output stream ----------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the raw_ostream class.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef WPIUTIL_WPI_RAW_OSTREAM_H
15 #define WPIUTIL_WPI_RAW_OSTREAM_H
16 
17 #include "wpi/ArrayRef.h"
18 #include "wpi/SmallVector.h"
19 #include "wpi/StringRef.h"
20 #include <cassert>
21 #include <cstddef>
22 #include <cstdint>
23 #include <cstring>
24 #include <string>
25 #include <vector>
26 #include <system_error>
27 
28 namespace wpi {
29 
30 class format_object_base;
31 class FormattedString;
32 class FormattedNumber;
33 class FormattedBytes;
34 
35 namespace sys {
36 namespace fs {
37 enum OpenFlags : unsigned;
38 } // end namespace fs
39 } // end namespace sys
40 
45 class raw_ostream {
46 private:
65  char *OutBufStart, *OutBufEnd, *OutBufCur;
66 
67  enum BufferKind {
68  Unbuffered = 0,
69  InternalBuffer,
70  ExternalBuffer
71  } BufferMode;
72 
73 public:
74  // color order matches ANSI escape sequence, don't change
75  enum Colors {
76  BLACK = 0,
77  RED,
78  GREEN,
79  YELLOW,
80  BLUE,
81  MAGENTA,
82  CYAN,
83  WHITE,
84  SAVEDCOLOR
85  };
86 
87  explicit raw_ostream(bool unbuffered = false)
88  : BufferMode(unbuffered ? Unbuffered : InternalBuffer) {
89  // Start out ready to flush.
90  OutBufStart = OutBufEnd = OutBufCur = nullptr;
91  }
92 
93  raw_ostream(const raw_ostream &) = delete;
94  void operator=(const raw_ostream &) = delete;
95 
96  virtual ~raw_ostream();
97 
99  uint64_t tell() const { return current_pos() + GetNumBytesInBuffer(); }
100 
101  //===--------------------------------------------------------------------===//
102  // Configuration Interface
103  //===--------------------------------------------------------------------===//
104 
107  void SetBuffered();
108 
110  void SetBufferSize(size_t Size) {
111  flush();
112  SetBufferAndMode(new char[Size], Size, InternalBuffer);
113  }
114 
115  size_t GetBufferSize() const {
116  // If we're supposed to be buffered but haven't actually gotten around
117  // to allocating the buffer yet, return the value that would be used.
118  if (BufferMode != Unbuffered && OutBufStart == nullptr)
119  return preferred_buffer_size();
120 
121  // Otherwise just return the size of the allocated buffer.
122  return OutBufEnd - OutBufStart;
123  }
124 
128  void SetUnbuffered() {
129  flush();
130  SetBufferAndMode(nullptr, 0, Unbuffered);
131  }
132 
133  size_t GetNumBytesInBuffer() const {
134  return OutBufCur - OutBufStart;
135  }
136 
137  //===--------------------------------------------------------------------===//
138  // Data Output Interface
139  //===--------------------------------------------------------------------===//
140 
141  void flush() {
142  if (OutBufCur != OutBufStart)
143  flush_nonempty();
144  }
145 
146  raw_ostream &operator<<(char C) {
147  if (OutBufCur >= OutBufEnd)
148  return write(C);
149  *OutBufCur++ = C;
150  return *this;
151  }
152 
153  raw_ostream &operator<<(unsigned char C) {
154  if (OutBufCur >= OutBufEnd)
155  return write(C);
156  *OutBufCur++ = C;
157  return *this;
158  }
159 
160  raw_ostream &operator<<(signed char C) {
161  if (OutBufCur >= OutBufEnd)
162  return write(C);
163  *OutBufCur++ = C;
164  return *this;
165  }
166 
167  raw_ostream &operator<<(ArrayRef<uint8_t> Arr) {
168  // Inline fast path, particularly for arrays with a known length.
169  size_t Size = Arr.size();
170 
171  // Make sure we can use the fast path.
172  if (Size > (size_t)(OutBufEnd - OutBufCur))
173  return write(Arr.data(), Size);
174 
175  if (Size) {
176  memcpy(OutBufCur, Arr.data(), Size);
177  OutBufCur += Size;
178  }
179  return *this;
180  }
181 
182  raw_ostream &operator<<(StringRef Str) {
183  // Inline fast path, particularly for strings with a known length.
184  size_t Size = Str.size();
185 
186  // Make sure we can use the fast path.
187  if (Size > (size_t)(OutBufEnd - OutBufCur))
188  return write(Str.data(), Size);
189 
190  if (Size) {
191  memcpy(OutBufCur, Str.data(), Size);
192  OutBufCur += Size;
193  }
194  return *this;
195  }
196 
197  raw_ostream &operator<<(const char *Str) {
198  // Inline fast path, particularly for constant strings where a sufficiently
199  // smart compiler will simplify strlen.
200 
201  return this->operator<<(StringRef(Str));
202  }
203 
204  raw_ostream &operator<<(const std::string &Str) {
205  // Avoid the fast path, it would only increase code size for a marginal win.
206  return write(Str.data(), Str.length());
207  }
208 
209  raw_ostream &operator<<(const SmallVectorImpl<char> &Str) {
210  return write(Str.data(), Str.size());
211  }
212 
213  raw_ostream &operator<<(const std::vector<uint8_t> &Arr) {
214  // Avoid the fast path, it would only increase code size for a marginal win.
215  return write(Arr.data(), Arr.size());
216  }
217 
218  raw_ostream &operator<<(const SmallVectorImpl<uint8_t> &Arr) {
219  return write(Arr.data(), Arr.size());
220  }
221 
222  raw_ostream &operator<<(unsigned long N);
223  raw_ostream &operator<<(long N);
224  raw_ostream &operator<<(unsigned long long N);
225  raw_ostream &operator<<(long long N);
226  raw_ostream &operator<<(const void *P);
227 
228  raw_ostream &operator<<(unsigned int N) {
229  return this->operator<<(static_cast<unsigned long>(N));
230  }
231 
232  raw_ostream &operator<<(int N) {
233  return this->operator<<(static_cast<long>(N));
234  }
235 
236  raw_ostream &operator<<(double N);
237 
239  raw_ostream &write_hex(unsigned long long N);
240 
243  raw_ostream &write_escaped(StringRef Str, bool UseHexEscapes = false);
244 
245  raw_ostream &write(unsigned char C);
246  raw_ostream &write(const char *Ptr, size_t Size);
247  raw_ostream &write(const uint8_t *Ptr, size_t Size) {
248  return write(reinterpret_cast<const char *>(Ptr), Size);
249  }
250 
251  // Formatted output, see the format() function in Support/Format.h.
252  raw_ostream &operator<<(const format_object_base &Fmt);
253 
254  // Formatted output, see the leftJustify() function in Support/Format.h.
255  raw_ostream &operator<<(const FormattedString &);
256 
257  // Formatted output, see the formatHex() function in Support/Format.h.
258  raw_ostream &operator<<(const FormattedNumber &);
259 
260  // Formatted output, see the format_bytes() function in Support/Format.h.
261  raw_ostream &operator<<(const FormattedBytes &);
262 
264  raw_ostream &indent(unsigned NumSpaces);
265 
267  raw_ostream &write_zeros(unsigned NumZeros);
268 
276  virtual raw_ostream &changeColor(enum Colors Color,
277  bool Bold = false,
278  bool BG = false) {
279  (void)Color;
280  (void)Bold;
281  (void)BG;
282  return *this;
283  }
284 
287  virtual raw_ostream &resetColor() { return *this; }
288 
290  virtual raw_ostream &reverseColor() { return *this; }
291 
295  virtual bool is_displayed() const { return false; }
296 
298  virtual bool has_colors() const { return is_displayed(); }
299 
300  //===--------------------------------------------------------------------===//
301  // Subclass Interface
302  //===--------------------------------------------------------------------===//
303 
304 private:
318  virtual void write_impl(const char *Ptr, size_t Size) = 0;
319 
320  // An out of line virtual method to provide a home for the class vtable.
321  virtual void handle();
322 
325  virtual uint64_t current_pos() const = 0;
326 
327 protected:
331  void SetBuffer(char *BufferStart, size_t Size) {
332  SetBufferAndMode(BufferStart, Size, ExternalBuffer);
333  }
334 
336  virtual size_t preferred_buffer_size() const;
337 
340  const char *getBufferStart() const { return OutBufStart; }
341 
342  //===--------------------------------------------------------------------===//
343  // Private Interface
344  //===--------------------------------------------------------------------===//
345 private:
347  void SetBufferAndMode(char *BufferStart, size_t Size, BufferKind Mode);
348 
351  void flush_nonempty();
352 
355  void copy_to_buffer(const char *Ptr, size_t Size);
356 
357  virtual void anchor();
358 };
359 
364  virtual void pwrite_impl(const char *Ptr, size_t Size, uint64_t Offset) = 0;
365  void anchor() override;
366 
367 public:
368  explicit raw_pwrite_stream(bool Unbuffered = false)
369  : raw_ostream(Unbuffered) {}
370  void pwrite(const char *Ptr, size_t Size, uint64_t Offset) {
371 #ifndef NDBEBUG
372  uint64_t Pos = tell();
373  // /dev/null always reports a pos of 0, so we cannot perform this check
374  // in that case.
375  if (Pos)
376  assert(Size + Offset <= Pos && "We don't support extending the stream");
377 #endif
378  pwrite_impl(Ptr, Size, Offset);
379  }
380 };
381 
382 //===----------------------------------------------------------------------===//
383 // File Output Streams
384 //===----------------------------------------------------------------------===//
385 
389  int FD;
390  bool ShouldClose;
391 
392  std::error_code EC;
393 
394  uint64_t pos;
395 
396  bool SupportsSeeking;
397 
399  void write_impl(const char *Ptr, size_t Size) override;
400 
401  void pwrite_impl(const char *Ptr, size_t Size, uint64_t Offset) override;
402 
405  uint64_t current_pos() const override { return pos; }
406 
408  size_t preferred_buffer_size() const override;
409 
411  void error_detected(std::error_code EC) { this->EC = EC; }
412 
413  void anchor() override;
414 
415 public:
426  raw_fd_ostream(StringRef Filename, std::error_code &EC,
427  sys::fs::OpenFlags Flags);
428 
431  raw_fd_ostream(int fd, bool shouldClose, bool unbuffered=false);
432 
433  ~raw_fd_ostream() override;
434 
437  void close();
438 
439  bool supportsSeeking() { return SupportsSeeking; }
440 
443  uint64_t seek(uint64_t off);
444 
445  std::error_code error() const { return EC; }
446 
451  bool has_error() const { return bool(EC); }
452 
462  void clear_error() { EC = std::error_code(); }
463 };
464 
467 raw_ostream &outs();
468 
471 raw_ostream &errs();
472 
474 raw_ostream &nulls();
475 
476 //===----------------------------------------------------------------------===//
477 // Output Stream Adaptors
478 //===----------------------------------------------------------------------===//
479 
483  std::string &OS;
484 
486  void write_impl(const char *Ptr, size_t Size) override;
487 
490  uint64_t current_pos() const override { return OS.size(); }
491 
492 public:
493  explicit raw_string_ostream(std::string &O) : OS(O) {}
494  ~raw_string_ostream() override;
495 
498  std::string& str() {
499  flush();
500  return OS;
501  }
502 };
503 
511 
513  void write_impl(const char *Ptr, size_t Size) override;
514 
515  void pwrite_impl(const char *Ptr, size_t Size, uint64_t Offset) override;
516 
518  uint64_t current_pos() const override;
519 
520 public:
526  SetUnbuffered();
527  }
528 
529  ~raw_svector_ostream() override = default;
530 
531  void flush() = delete;
532 
534  StringRef str() { return StringRef(OS.data(), OS.size()); }
535 };
536 
543  std::vector<char> &OS;
544 
546  void write_impl(const char *Ptr, size_t Size) override;
547 
548  void pwrite_impl(const char *Ptr, size_t Size, uint64_t Offset) override;
549 
551  uint64_t current_pos() const override;
552 
553 public:
558  explicit raw_vector_ostream(std::vector<char> &O) : OS(O) {
559  SetUnbuffered();
560  }
561 
562  ~raw_vector_ostream() override = default;
563 
564  void flush() = delete;
565 
567  StringRef str() { return StringRef(OS.data(), OS.size()); }
568 };
569 
577 
579  void write_impl(const char *Ptr, size_t Size) override;
580 
581  void pwrite_impl(const char *Ptr, size_t Size, uint64_t Offset) override;
582 
584  uint64_t current_pos() const override;
585 
586 public:
592  SetUnbuffered();
593  }
594 
595  ~raw_usvector_ostream() override = default;
596 
597  void flush() = delete;
598 
600  ArrayRef<uint8_t> array() { return ArrayRef<uint8_t>(OS.data(), OS.size()); }
601 };
602 
609  std::vector<uint8_t> &OS;
610 
612  void write_impl(const char *Ptr, size_t Size) override;
613 
614  void pwrite_impl(const char *Ptr, size_t Size, uint64_t Offset) override;
615 
617  uint64_t current_pos() const override;
618 
619 public:
624  explicit raw_uvector_ostream(std::vector<uint8_t> &O) : OS(O) {
625  SetUnbuffered();
626  }
627 
628  ~raw_uvector_ostream() override = default;
629 
630  void flush() = delete;
631 
633  ArrayRef<uint8_t> array() { return ArrayRef<uint8_t>(OS.data(), OS.size()); }
634 };
635 
636 
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;
642 
645  uint64_t current_pos() const override;
646 
647 public:
648  explicit raw_null_ostream() = default;
649  ~raw_null_ostream() override;
650 };
651 
653  raw_ostream &OS;
654  SmallVector<char, 0> Buffer;
655 
656 public:
657  buffer_ostream(raw_ostream &OS) : raw_svector_ostream(Buffer), OS(OS) {}
658  ~buffer_ostream() override { OS << str(); }
659 };
660 
661 } // end namespace wpi
662 
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
void close()
Manually flush the stream and close the file.
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:575
raw_ostream & write_escaped(StringRef Str, bool UseHexEscapes=false)
Output Str, turning '\', '', ' ', '"', and anything that doesn't satisfy std::isprint into an escape ...
raw_ostream & nulls()
This returns a reference to a raw_ostream which simply discards output.
uint64_t seek(uint64_t off)
Flushes the stream and repositions the underlying file descriptor position to the offset specified fr...
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 SetBuffered()
Set the stream to be buffered, with an automatically determined buffer size.
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
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
raw_ostream & outs()
This returns a reference to a raw_ostream for standard output.
WPILib C++ utilities (wpiutil) namespace.
Definition: SmallString.h:21
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
raw_ostream & write_hex(unsigned long long N)
Output N in hexadecimal, without any prefix or padding.
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:482
raw_ostream & write_zeros(unsigned NumZeros)
write_zeros - Insert 'NumZeros' nulls.
ArrayRef< uint8_t > array()
Return a StringRef for the vector contents.
Definition: raw_ostream.h:633
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
raw_fd_ostream(StringRef Filename, std::error_code &EC, sys::fs::OpenFlags Flags)
Open the specified file for writing.
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
virtual size_t preferred_buffer_size() const
Return an efficient buffer size for the underlying output mechanism.
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
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
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
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