diff --git a/modules/juce_core/containers/juce_DynamicObject.cpp b/modules/juce_core/containers/juce_DynamicObject.cpp index 69e9779..1d650be 100644 --- a/modules/juce_core/containers/juce_DynamicObject.cpp +++ b/modules/juce_core/containers/juce_DynamicObject.cpp @@ -52,6 +52,11 @@ const var& DynamicObject::getProperty (const Identifier& propertyName) const return properties [propertyName]; } +var& DynamicObject::getProperty (const Identifier& propertyName) +{ + return properties [propertyName]; +} + void DynamicObject::setProperty (const Identifier& propertyName, const var& newValue) { properties.set (propertyName, newValue); diff --git a/modules/juce_core/containers/juce_DynamicObject.h b/modules/juce_core/containers/juce_DynamicObject.h index 0709e2b..502e071 100644 --- a/modules/juce_core/containers/juce_DynamicObject.h +++ b/modules/juce_core/containers/juce_DynamicObject.h @@ -62,6 +62,7 @@ public: This returns var() if no such property exists. */ virtual const var& getProperty (const Identifier& propertyName) const; + virtual var& getProperty (const Identifier& propertyName); /** Sets a named property. */ virtual void setProperty (const Identifier& propertyName, const var& newValue); diff --git a/modules/juce_core/containers/juce_NamedValueSet.cpp b/modules/juce_core/containers/juce_NamedValueSet.cpp index b8c16ae..c81adaa 100644 --- a/modules/juce_core/containers/juce_NamedValueSet.cpp +++ b/modules/juce_core/containers/juce_NamedValueSet.cpp @@ -95,6 +95,16 @@ static const var& getNullVarRef() noexcept #endif } +static var& getSwallowNullVarRef() noexcept +{ +#if JUCE_ALLOW_STATIC_NULL_VARIABLES + return var::nullNonConst; +#else + static var nullVar; + return nullVar; +#endif +} + const var& NamedValueSet::operator[] (const Identifier& name) const noexcept { if (const var* v = getVarPointer (name)) @@ -103,6 +113,14 @@ const var& NamedValueSet::operator[] (const Identifier& name) const noexcept return getNullVarRef(); } +var& NamedValueSet::operator[] (const Identifier& name) +{ + if (var* v = getVarPointer (name)) + return *v; + + return getSwallowNullVarRef(); +} + var NamedValueSet::getWithDefault (const Identifier& name, const var& defaultReturnValue) const { if (const var* const v = getVarPointer (name)) diff --git a/modules/juce_core/containers/juce_NamedValueSet.h b/modules/juce_core/containers/juce_NamedValueSet.h index e55c6bf..8cc91c3 100644 --- a/modules/juce_core/containers/juce_NamedValueSet.h +++ b/modules/juce_core/containers/juce_NamedValueSet.h @@ -110,6 +110,7 @@ public: @see getProperty */ const var& operator[] (const Identifier& name) const noexcept; + var& operator[] (const Identifier& name); /** Tries to return the named value, but if no such value is found, this will instead return the supplied default value. diff --git a/modules/juce_core/containers/juce_Variant.cpp b/modules/juce_core/containers/juce_Variant.cpp index 2e1a479..1cbf57d 100644 --- a/modules/juce_core/containers/juce_Variant.cpp +++ b/modules/juce_core/containers/juce_Variant.cpp @@ -439,6 +439,7 @@ var::~var() noexcept { type->cleanUp (value); } #if JUCE_ALLOW_STATIC_NULL_VARIABLES const var var::null; +var var::nullNonConst; #endif //============================================================================== @@ -598,12 +599,25 @@ const var& var::operator[] (const Identifier& propertyName) const return getNullVarRef(); } +var& var::operator[] (const Identifier& propertyName) +{ + if (auto* o = getDynamicObject()) { + if (! o->hasProperty (propertyName)) + o->setProperty (propertyName, var()); + return o->getProperty (propertyName); + } + return getSwallowNullVarRef(); +} const var& var::operator[] (const char* const propertyName) const { return operator[] (Identifier (propertyName)); } +var& var::operator[] (const char* const propertyName) +{ + return operator[] (Identifier (propertyName)); +} var var::getProperty (const Identifier& propertyName, const var& defaultReturnValue) const { if (auto* o = getDynamicObject()) diff --git a/modules/juce_core/containers/juce_Variant.h b/modules/juce_core/containers/juce_Variant.h index d70e165..4526021 100644 --- a/modules/juce_core/containers/juce_Variant.h +++ b/modules/juce_core/containers/juce_Variant.h @@ -78,6 +78,7 @@ public: #if JUCE_ALLOW_STATIC_NULL_VARIABLES /** A static var object that can be used where you need an empty variant object. */ static const var null; + static var nullNonConst; #endif var (const var& valueToCopy); @@ -249,8 +250,10 @@ public: //============================================================================== /** If this variant is an object, this returns one of its properties. */ const var& operator[] (const Identifier& propertyName) const; + var& operator[] (const Identifier& propertyName); /** If this variant is an object, this returns one of its properties. */ const var& operator[] (const char* propertyName) const; + var& operator[] (const char* propertyName); /** 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;