Browse Source

Added a method var::hasProperty()

tags/2021-05-28
jules 7 years ago
parent
commit
4f204f405d
2 changed files with 27 additions and 14 deletions
  1. +25
    -14
      modules/juce_core/containers/juce_Variant.cpp
  2. +2
    -0
      modules/juce_core/containers/juce_Variant.h

+ 25
- 14
modules/juce_core/containers/juce_Variant.cpp View File

@@ -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<char> 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<var>* const thisArray = toArray (data);
const Array<var>* 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<var> 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<var> 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;


+ 2
- 0
modules/juce_core/containers/juce_Variant.h View File

@@ -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;


Loading…
Cancel
Save