Add raw_pwrite_stream type.
[oota-llvm.git] / include / llvm / Support / raw_ostream.h
index 6aba60d5e8c812266d7e937135c846f624bb813b..ed79292bd5ed22f2cc0dda665b80203e4d1190a3 100644 (file)
@@ -313,13 +313,22 @@ private:
   void copy_to_buffer(const char *Ptr, size_t Size);
 };
 
+/// An abstract base class for streams implementations that also support a
+/// pwrite operation. This is usefull for code that can mostly stream out data,
+/// but needs to patch in a header that needs to know the output size.
+class raw_pwrite_stream : public raw_ostream {
+public:
+  using raw_ostream::raw_ostream;
+  virtual void pwrite(const char *Ptr, size_t Size, uint64_t Offset) = 0;
+};
+
 //===----------------------------------------------------------------------===//
 // File Output Streams
 //===----------------------------------------------------------------------===//
 
 /// A raw_ostream that writes to a file descriptor.
 ///
-class raw_fd_ostream : public raw_ostream {
+class raw_fd_ostream : public raw_pwrite_stream {
   int FD;
   bool ShouldClose;
 
@@ -378,6 +387,8 @@ public:
   /// to the offset specified from the beginning of the file.
   uint64_t seek(uint64_t off);
 
+  void pwrite(const char *Ptr, size_t Size, uint64_t Offset) override;
+
   /// Set the stream to attempt to use atomic writes for individual output
   /// routines where possible.
   ///
@@ -460,7 +471,7 @@ public:
 
 /// A raw_ostream that writes to an SmallVector or SmallString.  This is a
 /// simple adaptor class. This class does not encounter output errors.
-class raw_svector_ostream : public raw_ostream {
+class raw_svector_ostream : public raw_pwrite_stream {
   SmallVectorImpl<char> &OS;
 
   /// See raw_ostream::write_impl.
@@ -469,6 +480,12 @@ class raw_svector_ostream : public raw_ostream {
   /// Return the current position within the stream, not counting the bytes
   /// currently in the buffer.
   uint64_t current_pos() const override;
+
+protected:
+  // Like the regular constructor, but doesn't call init.
+  explicit raw_svector_ostream(SmallVectorImpl<char> &O, unsigned);
+  void init();
+
 public:
   /// Construct a new raw_svector_ostream.
   ///
@@ -477,6 +494,8 @@ public:
   explicit raw_svector_ostream(SmallVectorImpl<char> &O);
   ~raw_svector_ostream() override;
 
+  void pwrite(const char *Ptr, size_t Size, uint64_t Offset) override;
+
   /// This is called when the SmallVector we're appending to is changed outside
   /// of the raw_svector_ostream's control.  It is only safe to do this if the
   /// raw_svector_ostream has previously been flushed.
@@ -488,7 +507,7 @@ public:
 };
 
 /// A raw_ostream that discards all output.
-class raw_null_ostream : public raw_ostream {
+class raw_null_ostream : public raw_pwrite_stream {
   /// See raw_ostream::write_impl.
   void write_impl(const char *Ptr, size_t size) override;
 
@@ -499,6 +518,18 @@ class raw_null_ostream : public raw_ostream {
 public:
   explicit raw_null_ostream() {}
   ~raw_null_ostream() override;
+  void pwrite(const char *Ptr, size_t Size, uint64_t Offset) override;
+};
+
+class buffer_ostream : public raw_svector_ostream {
+  raw_ostream &OS;
+  SmallVector<char, 0> Buffer;
+
+public:
+  buffer_ostream(raw_ostream &OS) : raw_svector_ostream(Buffer, 0), OS(OS) {
+    init();
+  }
+  ~buffer_ostream() { OS << str(); }
 };
 
 } // end llvm namespace