<< operator for CodeWriter
Adding << operator to CodeWriter.
Test: m -j
Test: runtests.sh
Change-Id: Ieec617496251705c060f8f62d8b001dae325aa2b
diff --git a/Android.bp b/Android.bp
index 798ffba..38b2625 100644
--- a/Android.bp
+++ b/Android.bp
@@ -98,6 +98,7 @@
"aidl_unittest.cpp",
"ast_cpp_unittest.cpp",
"ast_java_unittest.cpp",
+ "code_writer_unittest.cpp",
"generate_cpp_unittest.cpp",
"io_delegate_unittest.cpp",
"options_unittest.cpp",
diff --git a/code_writer.cpp b/code_writer.cpp
index e75d7a5..9f6a632 100644
--- a/code_writer.cpp
+++ b/code_writer.cpp
@@ -78,6 +78,16 @@
return true;
}
+CodeWriter& CodeWriter::operator<<(const char* s) {
+ Write(s);
+ return *this;
+}
+
+CodeWriter& CodeWriter::operator<<(const std::string& str) {
+ Write(str.c_str());
+ return *this;
+}
+
CodeWriterPtr CodeWriter::ForFile(const std::string& filename) {
std::unique_ptr<std::ostream> stream;
if (filename == "-") {
diff --git a/code_writer.h b/code_writer.h
index 2a81dac..aea50ef 100644
--- a/code_writer.h
+++ b/code_writer.h
@@ -48,6 +48,9 @@
virtual ~CodeWriter() = default;
CodeWriter() = default;
+ CodeWriter& operator<<(const char* s);
+ CodeWriter& operator<<(const std::string& str);
+
private:
CodeWriter(std::unique_ptr<std::ostream> ostream);
std::string ApplyIndent(const std::string& str);
diff --git a/code_writer_unittest.cpp b/code_writer_unittest.cpp
new file mode 100644
index 0000000..9cf462c
--- /dev/null
+++ b/code_writer_unittest.cpp
@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) 2018, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "code_writer.h"
+
+#include <gtest/gtest.h>
+#include <string>
+
+using android::aidl::CodeWriter;
+using std::string;
+using std::unique_ptr;
+
+namespace android {
+namespace aidl {
+
+TEST(CodeWriterTest, AppendOperator) {
+ string str;
+ CodeWriterPtr ptr = CodeWriter::ForString(&str);
+ CodeWriter& writer = *ptr;
+ writer << "Write this";
+ writer.Close();
+ EXPECT_EQ(str, "Write this");
+}
+
+TEST(CodeWriterTest, AppendOperatorCascade) {
+ string str;
+ CodeWriterPtr ptr = CodeWriter::ForString(&str);
+ CodeWriter& writer = *ptr;
+ writer << "Write this " << "and that";
+ writer.Close();
+ EXPECT_EQ(str, "Write this and that");
+}
+
+} // namespace aidl
+} // namespace android