summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2008-08-23 22:43:04 +0000
committerChris Lattner <sabre@nondot.org>2008-08-23 22:43:04 +0000
commit78a2812538d871a62d90f53ed66d26198876d011 (patch)
tree297ba4550a141c420c6656e4c0c6580de7ad1892
parent944fac71e082cc2664cc71b4d3f6c72bab7143fb (diff)
downloadllvm-78a2812538d871a62d90f53ed66d26198876d011.tar.gz
llvm-78a2812538d871a62d90f53ed66d26198876d011.tar.bz2
llvm-78a2812538d871a62d90f53ed66d26198876d011.tar.xz
Add raw_stream adaptors that write into an std::string and SmallVector/SmallString.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@55265 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--include/llvm/Support/raw_ostream.h32
-rw-r--r--lib/Support/raw_ostream.cpp35
2 files changed, 66 insertions, 1 deletions
diff --git a/include/llvm/Support/raw_ostream.h b/include/llvm/Support/raw_ostream.h
index 9fb5f6cb38..824d209189 100644
--- a/include/llvm/Support/raw_ostream.h
+++ b/include/llvm/Support/raw_ostream.h
@@ -22,6 +22,8 @@
namespace llvm {
class format_object_base;
+ template <typename T>
+ class SmallVectorImpl;
/// raw_ostream - This class implements an extremely fast bulk output stream
/// that can *only* output to a stream. It does not support seeking, reopening,
@@ -192,7 +194,7 @@ raw_ostream &errs();
//===----------------------------------------------------------------------===//
-// Bridge Output Streams
+// Output Stream Adaptors
//===----------------------------------------------------------------------===//
/// raw_os_ostream - A raw_ostream that writes to an std::ostream. This is a
@@ -208,6 +210,34 @@ public:
/// buffer to empty.
virtual void flush_impl();
};
+
+/// raw_string_ostream - A raw_ostream that writes to an std::string. This is a
+/// simple adaptor class.
+class raw_string_ostream : public raw_ostream {
+ std::string &OS;
+public:
+ raw_string_ostream(std::string &O) : OS(O) {}
+ ~raw_string_ostream();
+
+ /// flush_impl - The is the piece of the class that is implemented by
+ /// subclasses. This outputs the currently buffered data and resets the
+ /// buffer to empty.
+ virtual void flush_impl();
+};
+
+/// raw_svector_ostream - A raw_ostream that writes to an SmallVector or
+/// SmallString. This is a simple adaptor class.
+class raw_svector_ostream : public raw_ostream {
+ SmallVectorImpl<char> &OS;
+public:
+ raw_svector_ostream(SmallVectorImpl<char> &O) : OS(O) {}
+ ~raw_svector_ostream();
+
+ /// flush_impl - The is the piece of the class that is implemented by
+ /// subclasses. This outputs the currently buffered data and resets the
+ /// buffer to empty.
+ virtual void flush_impl();
+};
} // end llvm namespace
diff --git a/lib/Support/raw_ostream.cpp b/lib/Support/raw_ostream.cpp
index 9827ca776a..b9beac2869 100644
--- a/lib/Support/raw_ostream.cpp
+++ b/lib/Support/raw_ostream.cpp
@@ -268,3 +268,38 @@ void raw_os_ostream::flush_impl() {
OS.write(OutBufStart, OutBufCur-OutBufStart);
HandleFlush();
}
+
+//===----------------------------------------------------------------------===//
+// raw_string_ostream
+//===----------------------------------------------------------------------===//
+
+raw_string_ostream::~raw_string_ostream() {
+ flush();
+}
+
+/// flush_impl - The is the piece of the class that is implemented by
+/// subclasses. This outputs the currently buffered data and resets the
+/// buffer to empty.
+void raw_string_ostream::flush_impl() {
+ if (OutBufCur-OutBufStart)
+ OS.append(OutBufStart, OutBufCur-OutBufStart);
+ HandleFlush();
+}
+
+//===----------------------------------------------------------------------===//
+// raw_svector_ostream
+//===----------------------------------------------------------------------===//
+
+raw_svector_ostream::~raw_svector_ostream() {
+ flush();
+}
+
+/// flush_impl - The is the piece of the class that is implemented by
+/// subclasses. This outputs the currently buffered data and resets the
+/// buffer to empty.
+void raw_svector_ostream::flush_impl() {
+ if (OutBufCur-OutBufStart)
+ OS.append(OutBufStart, OutBufCur);
+ HandleFlush();
+}
+