#include <cstring>
#include <string>
#include <system_error>
+#include <type_traits>
namespace llvm {
virtual void anchor();
};
+/// Call the appropriate insertion operator, given an rvalue reference to a
+/// raw_ostream object and return a stream of the same type as the argument.
+template <typename OStream, typename T>
+typename std::enable_if<!std::is_reference<OStream>::value &&
+ std::is_base_of<raw_ostream, OStream>::value,
+ OStream &&>::type
+operator<<(OStream &&OS, const T &Value) {
+ OS << Value;
+ return std::move(OS);
+}
+
/// An abstract base class for streams implementations that also support a
/// pwrite operation. This is useful for code that can mostly stream out data,
/// but needs to patch in a header that needs to know the output size.
template<typename T> std::string printToString(const T &Value) {
std::string res;
- llvm::raw_string_ostream(res) << Value;
- return res;
+ return (llvm::raw_string_ostream(res) << Value).str();
}
/// printToString - Print the given value to a stream which only has \arg
return res;
}
+struct X {};
+
+raw_ostream &operator<<(raw_ostream &OS, const X &) { return OS << 'X'; }
+
TEST(raw_ostreamTest, Types_Buffered) {
// Char
EXPECT_EQ("c", printToString('c'));
// Min and max.
EXPECT_EQ("18446744073709551615", printToString(UINT64_MAX));
EXPECT_EQ("-9223372036854775808", printToString(INT64_MIN));
+
+ // X, checking free operator<<().
+ EXPECT_EQ("X", printToString(X{}));
}
TEST(raw_ostreamTest, Types_Unbuffered) {
// Min and max.
EXPECT_EQ("18446744073709551615", printToStringUnbuffered(UINT64_MAX));
EXPECT_EQ("-9223372036854775808", printToStringUnbuffered(INT64_MIN));
+
+ // X, checking free operator<<().
+ EXPECT_EQ("X", printToString(X{}));
}
TEST(raw_ostreamTest, BufferEdge) {