diff --git a/modules/juce_core/containers/juce_Variant.cpp b/modules/juce_core/containers/juce_Variant.cpp index 7c517bb886..7a2f3c3c0a 100644 --- a/modules/juce_core/containers/juce_Variant.cpp +++ b/modules/juce_core/containers/juce_Variant.cpp @@ -240,7 +240,7 @@ public: void writeToStream (const ValueUnion& data, OutputStream& output) const override { - const String* const s = getString (data); + auto* s = getString (data); const size_t len = s->getNumBytesAsUTF8() + 1; HeapBlock temp (len); s->copyToUTF8 (temp, len); @@ -282,7 +282,7 @@ public: var clone (const var& original) const override { - if (DynamicObject* d = original.getDynamicObject()) + if (auto* d = original.getDynamicObject()) return d->clone().get(); jassertfalse; // can only clone DynamicObjects! @@ -317,8 +317,8 @@ public: bool equals (const ValueUnion& data, const ValueUnion& otherData, const VariantType& otherType) const noexcept override { - const Array* const thisArray = toArray (data); - const Array* const otherArray = otherType.toArray (otherData); + auto* thisArray = toArray (data); + auto* otherArray = otherType.toArray (otherData); return thisArray == otherArray || (thisArray != nullptr && otherArray != nullptr && *otherArray == *thisArray); } @@ -327,8 +327,12 @@ public: Array arrayCopy; if (auto* array = toArray (original.value)) - for (int i = 0; i < array->size(); ++i) - arrayCopy.add (array->getReference(i).clone()); + { + arrayCopy.ensureStorageAllocated (array->size()); + + for (auto& i : *array) + arrayCopy.add (i.clone()); + } return var (arrayCopy); } @@ -338,11 +342,10 @@ public: if (auto* array = toArray (data)) { MemoryOutputStream buffer (512); - const int numItems = array->size(); - buffer.writeCompressedInt (numItems); + buffer.writeCompressedInt (array->size()); - for (int i = 0; i < numItems; ++i) - array->getReference(i).writeToStream (buffer); + for (auto& i : *array) + i.writeToStream (buffer); output.writeCompressedInt (1 + (int) buffer.getDataSize()); output.writeByte (varMarker_Array); @@ -457,12 +460,12 @@ var::var (const MemoryBlock& v) : type (&VariantType_Binary::instance) { v var::var (const StringArray& v) : type (&VariantType_Array::instance) { Array strings; + strings.ensureStorageAllocated (v.size()); - const int n = v.size(); - for (int i = 0; i < n; ++i) - strings.add (var (v[i])); + for (auto& i : v) + strings.add (var (i)); - value.objectValue = new VariantType_Array::RefCountedArray(strings); + value.objectValue = new VariantType_Array::RefCountedArray (strings); } var::var (ReferenceCountedObject* const object) : type (&VariantType_Object::instance) @@ -607,6 +610,14 @@ var var::getProperty (const Identifier& propertyName, const var& defaultReturnVa return defaultReturnValue; } +bool var::hasProperty (const Identifier& propertyName) const noexcept +{ + if (auto* o = getDynamicObject()) + return o->hasProperty (propertyName); + + return false; +} + var::NativeFunction var::getNativeFunction() const { return isMethod() && (value.methodValue != nullptr) ? *value.methodValue : nullptr; diff --git a/modules/juce_core/containers/juce_Variant.h b/modules/juce_core/containers/juce_Variant.h index 6cb65043aa..3a9cb7d24d 100644 --- a/modules/juce_core/containers/juce_Variant.h +++ b/modules/juce_core/containers/juce_Variant.h @@ -243,6 +243,8 @@ public: /** If this variant is an object, this returns one of its properties, or a default fallback value if the property is not set. */ var getProperty (const Identifier& propertyName, const var& defaultReturnValue) const; + /** Returns true if this variant is an object and if it has the given property. */ + bool hasProperty (const Identifier& propertyName) const noexcept; /** Invokes a named method call with no arguments. */ var call (const Identifier& method) const;