make Vt_ValueWrapper objects equality-comparable.
authorhaldean <haldean@users.noreply.github.com>
Tue, 2 Jan 2024 22:42:20 +0000 (14:42 -0800)
committerpixar-oss <pixar-oss@users.noreply.github.com>
Tue, 2 Jan 2024 22:42:20 +0000 (14:42 -0800)
This defines equality comparison of ValueWrappers to just compare their wrapped VtValues.

This also gives them a more useful repr, so you can see what the wrapped value is; without
this, it's difficult to debug test failures that occur from comparing two wrapped values.

(Internal change: 2309999)

pxr/base/vt/testenv/testVtValue.py
pxr/base/vt/wrapValue.cpp

index e824e988da09a8233c494d5e77d03d33688dc579..7e8476a644f3f42a3165718859ba65f53777502c 100644 (file)
@@ -113,5 +113,23 @@ class TestVtValue(unittest.TestCase):
         with self.assertRaises(TypeError):
             Vt._ReturnDictionary(bad2)
 
+    def test_WrappedTypesComparable(self):
+        """
+        Makes sure that values wrapped in a value wrapper are still
+        equality-comparable.
+        """
+        t1 = Vt.Token("hello")
+        t2 = Vt.Token("hello")
+        t3 = Vt.Token("world")
+        f1 = Vt.Float(1.0)
+        f2 = Vt.Float(1.0)
+        d1 = Vt.Double(1.0)
+
+        self.assertEqual(t1, t2)
+        self.assertNotEqual(t1, t3)
+        self.assertNotEqual(t1, f1)
+        self.assertEqual(f1, f2)
+        self.assertNotEqual(f1, d1)
+
 if __name__ == '__main__':
     unittest.main()
index e7f1d395466a7517cdc6e2e8eef5e05fbb369a57..b5693d55a9100dc1a14749d9fb968855ec536f8c 100644 (file)
@@ -107,6 +107,19 @@ struct Vt_ValueWrapper {
     template <typename T> explicit Vt_ValueWrapper(T val) : _val(val) {}
 
     VtValue const &GetValue() const { return _val; }
+
+    bool operator==(const Vt_ValueWrapper &other) {
+        return _val == other._val;
+    }
+
+    bool operator!=(const Vt_ValueWrapper &other) {
+        return _val != other._val;
+    }
+
+    std::string GetAsString() {
+        return TfStringPrintf(
+            "%s(%s)", _val.GetTypeName().c_str(), TfStringify(_val).c_str());
+    }
     
   private:
     VtValue _val;
@@ -252,7 +265,12 @@ void wrapValue()
     Vt_ValueFromPython();
     Vt_ValueWrapperFromPython();
 
-    class_<Vt_ValueWrapper>("_ValueWrapper", no_init);
+    class_<Vt_ValueWrapper>("_ValueWrapper", no_init)
+        .def(self == self)
+        .def(self != self)
+        .def("__str__", &Vt_ValueWrapper::GetAsString)
+        .def("__repr__", &Vt_ValueWrapper::GetAsString)
+        ;
 
     static char const *funcDocString = "%s(value) -> _ValueWrapper\n\n"
         "value : %s\n\n"