Don't overload operators in std namespace.
authorDavid Neto <dneto@google.com>
Tue, 1 Sep 2015 18:56:09 +0000 (14:56 -0400)
committerDavid Neto <dneto@google.com>
Mon, 26 Oct 2015 16:52:01 +0000 (12:52 -0400)
Use a spvtest::WordVector proxy object to easily print
std::vector<uint32_t> and spv_binary_t values.

test/ExtInstGLSLstd450.cpp
test/UnitSPIRV.h

index 9cdc228..7ae39fa 100644 (file)
@@ -107,7 +107,8 @@ OpFunctionEnd
   EXPECT_NE(binary->code + binary->wordCount,
             std::search(binary->code, binary->code + binary->wordCount,
                         expected_contains.begin(), expected_contains.end()))
-      << "Cannot find\n" << expected_contains << "in\n" << *binary;
+      << "Cannot find\n" << spvtest::WordVector(expected_contains).str()
+      << "in\n" << spvtest::WordVector(*binary).str();
 
   // Check round trip gives the same text.
   spv_text output_text;
index 96bfbf1..d8f22fc 100644 (file)
@@ -63,34 +63,51 @@ static const union {
   uint32_t value;
 } o32_host_order = {{0, 1, 2, 3}};
 
-inline ::std::ostream& operator<<(::std::ostream& os,
-                                  const spv_binary_t& binary) {
-  for (size_t i = 0; i < binary.wordCount; ++i) {
-    os << "0x" << std::setw(8) << std::setfill('0') << std::hex
-       << binary.code[i] << " ";
-    if (i % 8 == 7) {
-      os << std::endl;
-    }
+
+// A namespace for utilities used in SPIR-V Tools unit tests.
+// TODO(dneto): Move other type declarations into this namespace.
+namespace spvtest {
+
+class WordVector;
+
+// Emits the given word vector to the given stream.
+// This function can be used by the gtest value printer.
+void PrintTo(const WordVector& words, ::std::ostream* os);
+
+// A proxy class to allow us to easily write out vectors of SPIR-V words.
+class WordVector {
+ public:
+  explicit WordVector(const std::vector<uint32_t>& value) : value_(value) {}
+  explicit WordVector(const spv_binary_t& binary)
+      : value_(binary.code, binary.code + binary.wordCount) {}
+
+  // Returns the underlying vector.
+  const std::vector<uint32_t>& value() const { return value_; }
+
+  // Returns the string representation of this word vector.
+  std::string str() const {
+    std::ostringstream os;
+    PrintTo(*this, &os);
+    return os.str();
   }
-  os << std::endl;
-  return os;
-}
 
-namespace std {
-inline ::std::ostream& operator<<(::std::ostream& os,
-                                  const std::vector<uint32_t>& value) {
+ private:
+  const std::vector<uint32_t> value_;
+};
+
+inline void PrintTo(const WordVector& words, ::std::ostream* os) {
   size_t count = 0;
-  for (size_t i : value) {
-    os << "0x" << std::setw(8) << std::setfill('0') << std::hex << i << " ";
+  for (uint32_t value : words.value()) {
+    *os << "0x" << std::setw(8) << std::setfill('0') << std::hex << value << " ";
     if (count++ % 8 == 7) {
-      os << std::endl;
+      *os << std::endl;
     }
   }
-  os << std::endl;
-  return os;
-}
+  *os << std::endl;
 }
 
+} // namespace spvtest
+
 // A type for easily creating spv_text_t values, with an implicit conversion to
 // spv_text.
 struct AutoText {