[llvm] 7479a2e - [Support] Add raw_ostream_iterator: ostream_iterator for raw_ostream

Nicolas Guillemot via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 4 11:13:09 PST 2021


Author: Nicolas Guillemot
Date: 2021-03-04T11:12:48-08:00
New Revision: 7479a2e00bc41f399942e5106fbdf9b4b0c11506

URL: https://github.com/llvm/llvm-project/commit/7479a2e00bc41f399942e5106fbdf9b4b0c11506
DIFF: https://github.com/llvm/llvm-project/commit/7479a2e00bc41f399942e5106fbdf9b4b0c11506.diff

LOG: [Support] Add raw_ostream_iterator: ostream_iterator for raw_ostream

Adds a class `raw_ostream_iterator` that behaves like
std::ostream_iterator, but can be used with raw_ostream.
This is useful for using raw_ostream with std algorithms.

For example, it can be used to output std containers as follows:

```
std::vector<int> V = { 1, 2, 3 };
std::copy(V.begin(), V.end(), raw_ostream_iterator<int>(outs(), ", "));
// Output: "1, 2, 3, "
```

The API tries to follow std::ostream_iterator as closely as is
practically possible.

Reviewed By: dblaikie, mkitzan

Differential Revision: https://reviews.llvm.org/D78795

Added: 
    

Modified: 
    llvm/include/llvm/Support/raw_ostream.h
    llvm/unittests/Support/raw_ostream_test.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Support/raw_ostream.h b/llvm/include/llvm/Support/raw_ostream.h
index 201a4a857964..ef235482a673 100644
--- a/llvm/include/llvm/Support/raw_ostream.h
+++ b/llvm/include/llvm/Support/raw_ostream.h
@@ -21,6 +21,7 @@
 #include <cstddef>
 #include <cstdint>
 #include <cstring>
+#include <iterator>
 #include <string>
 #include <system_error>
 #include <type_traits>
@@ -714,6 +715,66 @@ class buffer_unique_ostream : public raw_svector_ostream {
   ~buffer_unique_ostream() override { *OS << str(); }
 };
 
+/// Wrapper to make a raw_ostream look like an output iterator.
+/// Designed to match the API of std::ostream_iterator.
+/// Enables reusing STL algorithms with raw_ostream. For example:
+///
+/// \code{.cpp}
+///
+/// std::vector<int> V = { 1, 2, 3 };
+/// std::copy(V.begin(), V.end(), raw_ostream_iterator<int>(outs(), ", "));
+///
+/// \endcode
+///
+/// The code above outputs: "1, 2, 3, "
+template <class T, class CharT = char> class raw_ostream_iterator {
+  raw_ostream &OutStream;
+  const CharT *Delim;
+
+public:
+  using iterator_category = std::output_iterator_tag;
+  using value_type = void;
+  using 
diff erence_type = void;
+  using pointer = void;
+  using reference = void;
+  using char_type = CharT;
+
+  /// Constructs the iterator with \p Stream as the associated stream and \p
+  /// Delim as the delimiter.
+  ///
+  /// \param Stream The output stream to be accessed by this iterator.
+  ///
+  /// \param Delim The null-terminated character string to be inserted into the
+  /// stream after each output.
+  raw_ostream_iterator(raw_ostream &Stream, const CharT *Delim)
+      : OutStream(Stream), Delim(Delim) {}
+
+  /// Constructs the iterator with \p Stream as the associated stream and a null
+  /// pointer as the delimiter.
+  ///
+  /// \param Stream The output stream to be accessed by this iterator.
+  raw_ostream_iterator(raw_ostream &Stream)
+      : OutStream(Stream), Delim(nullptr) {}
+
+  /// Inserts \p Value into the associated stream, then inserts the delimiter,
+  /// if one was specified at construction time.
+  ///
+  /// \param value The object to insert.
+  raw_ostream_iterator &operator=(const T &Value) {
+    OutStream << Value;
+    if (Delim != 0)
+      OutStream << Delim;
+    return *this;
+  }
+
+  /// No-op. Provided to satisfy the requirements of LegacyOutputIterator.
+  ///@{
+  raw_ostream_iterator &operator*() { return *this; }
+  raw_ostream_iterator &operator++() { return *this; }
+  raw_ostream_iterator &operator++(int) { return *this; }
+  ///@}
+};
+
 } // end namespace llvm
 
 #endif // LLVM_SUPPORT_RAW_OSTREAM_H

diff  --git a/llvm/unittests/Support/raw_ostream_test.cpp b/llvm/unittests/Support/raw_ostream_test.cpp
index 78fdb04bcdaa..2c0d37bbcabb 100644
--- a/llvm/unittests/Support/raw_ostream_test.cpp
+++ b/llvm/unittests/Support/raw_ostream_test.cpp
@@ -377,6 +377,114 @@ TEST(raw_fd_ostreamTest, multiple_raw_fd_ostream_to_stdout) {
   { raw_fd_ostream("-", EC, sys::fs::OpenFlags::OF_None); }
 }
 
+// Basic functionality tests for raw_ostream_iterator.
+TEST(raw_ostream_iteratorTest, raw_ostream_iterator_basic) {
+  std::string S;
+
+  // Expect no output if nothing is written to the iterator.
+  {
+    S = "";
+    raw_string_ostream Str(S);
+    raw_ostream_iterator<char> It(Str);
+  }
+  EXPECT_EQ(S, "");
+  {
+    S = "";
+    raw_string_ostream Str(S);
+    raw_ostream_iterator<char> It(Str, ",");
+  }
+  EXPECT_EQ(S, "");
+
+  // Output one char.
+  {
+    S = "";
+    raw_string_ostream Str(S);
+    raw_ostream_iterator<char> It(Str);
+    *It = 'x';
+  }
+  EXPECT_EQ(S, "x");
+  {
+    S = "";
+    raw_string_ostream Str(S);
+    raw_ostream_iterator<char> It(Str, ",");
+    *It = 'x';
+  }
+  EXPECT_EQ(S, "x,");
+
+  // Output one int.
+  {
+    S = "";
+    raw_string_ostream Str(S);
+    raw_ostream_iterator<int> It(Str);
+    *It = 5;
+  }
+  EXPECT_EQ(S, "5");
+  {
+    S = "";
+    raw_string_ostream Str(S);
+    raw_ostream_iterator<int> It(Str, ",");
+    *It = 5;
+  }
+  EXPECT_EQ(S, "5,");
+
+  // Output a few ints.
+  {
+    S = "";
+    raw_string_ostream Str(S);
+    raw_ostream_iterator<int> It(Str);
+    *It = 5;
+    *It = 3;
+    *It = 2;
+  }
+  EXPECT_EQ(S, "532");
+  {
+    S = "";
+    raw_string_ostream Str(S);
+    raw_ostream_iterator<int> It(Str, ",");
+    *It = 5;
+    *It = 3;
+    *It = 2;
+  }
+  EXPECT_EQ(S, "5,3,2,");
+}
+
+template <class T, class InputIt>
+std::string iterator_str(InputIt First, InputIt Last) {
+  std::string S;
+  {
+    raw_string_ostream Str(S);
+    std::copy(First, Last, raw_ostream_iterator<T>(Str));
+  }
+  return S;
+}
+
+template <class T, class InputIt>
+std::string iterator_str(InputIt First, InputIt Last, const char *Delim) {
+  std::string S;
+  {
+    raw_string_ostream Str(S);
+    std::copy(First, Last, raw_ostream_iterator<T>(Str, Delim));
+  }
+  return S;
+}
+
+// Test using raw_ostream_iterator as an output iterator with a std algorithm.
+TEST(raw_ostream_iteratorTest, raw_ostream_iterator_std_copy) {
+  // Test the empty case.
+  std::vector<char> Empty = {};
+  EXPECT_EQ("", iterator_str<char>(Empty.begin(), Empty.end()));
+  EXPECT_EQ("", iterator_str<char>(Empty.begin(), Empty.end(), ", "));
+
+  // Test without a delimiter.
+  std::vector<char> V1 = {'a', 'b', 'c'};
+  EXPECT_EQ("abc", iterator_str<char>(V1.begin(), V1.end()));
+
+  // Test with a delimiter.
+  std::vector<int> V2 = {1, 2, 3};
+  EXPECT_EQ("1, 2, 3, ", iterator_str<int>(V2.begin(), V2.end(), ", "));
+}
+
+
 TEST(raw_ostreamTest, flush_tied_to_stream_on_write) {
   std::string TiedToBuffer;
   raw_string_ostream TiedTo(TiedToBuffer);


        


More information about the llvm-commits mailing list