1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-10 23:44:24 +00:00

Variant: Make DynamicObject comparison more intuitive

This commit is contained in:
reuk 2025-11-25 13:13:21 +00:00
parent 4488813de7
commit 95eef1995a
No known key found for this signature in database
2 changed files with 70 additions and 1 deletions

View file

@ -2,6 +2,28 @@
# Version 8.0.11 # Version 8.0.11
## Change
var::equals(), var::operator==(), and var::operator!=() will now carry out a
deep equality check when comparing two stored DynamicObjects, as opposed to
just comparing the objects' addresses, which was the old behaviour.
**Possible Issues**
Program that depend on variants only comparing equal when the object pointers
are equal will now exhibit unexpected behaviour.
**Workaround**
There is no workaround for this change.
**Rationale**
The previous behaviour was unintuitive, as it meant that two different var
instances may compare unequal, even when those var instances were both created
by parsing the same JSON string.
## Change ## Change
Enabling JUCE_ASIO will now default to using bundled ASIO sources. Enabling JUCE_ASIO will now default to using bundled ASIO sources.

View file

@ -343,7 +343,13 @@ struct var::VariantType
static bool objectEquals (const ValueUnion& data, const ValueUnion& otherData, const VariantType& otherType) noexcept static bool objectEquals (const ValueUnion& data, const ValueUnion& otherData, const VariantType& otherType) noexcept
{ {
return otherType.toObject (otherData) == data.objectValue; const auto* otherObject = otherType.toObject (otherData);
if (auto* dynamicObjectOther = dynamic_cast<const DynamicObject*> (otherObject))
if (auto* dynamicObjectSelf = dynamic_cast<const DynamicObject*> (data.objectValue))
return dynamicObjectSelf->equals (*dynamicObjectOther);
return otherObject == data.objectValue;
} }
static void objectWriteToStream (const ValueUnion&, OutputStream& output) static void objectWriteToStream (const ValueUnion&, OutputStream& output)
@ -904,4 +910,45 @@ JUCE_END_IGNORE_DEPRECATION_WARNINGS
#endif #endif
//==============================================================================
//==============================================================================
#if JUCE_UNIT_TESTS
struct VariantTests : public UnitTest
{
public:
VariantTests() : UnitTest ("Variant", UnitTestCategories::json) {}
void runTest() override
{
beginTest ("object comparisons have value semantics");
{
DynamicObject::Ptr a = new DynamicObject;
a->setProperty ("foo", 1);
a->setProperty ("bar", "hello world");
a->setProperty ("baz", 2.3);
const Array<var> nestedArray { var { 5 }, var { 6 }, var { 7 }, var { std::invoke ([]
{
auto* result = new DynamicObject;
result->setProperty ("innerA", 0);
result->setProperty ("innerB", "");
return result;
}) } };
a->setProperty ("nestedArray", nestedArray);
var varB = a->clone().release();
var varA = a.get();
expect (varA.equals (varB));
expect (varB.equals (varA));
}
}
};
static VariantTests variantTests;
#endif
} // namespace juce } // namespace juce