Loading...
Searching...
No Matches
juce_PropertySet.cpp
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2022 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
PropertySet::PropertySet (bool ignoreCaseOfKeyNames)
: properties (ignoreCaseOfKeyNames),
fallbackProperties (nullptr),
ignoreCaseOfKeys (ignoreCaseOfKeyNames)
{
}
PropertySet::PropertySet (const PropertySet& other)
: properties (other.properties),
fallbackProperties (other.fallbackProperties),
ignoreCaseOfKeys (other.ignoreCaseOfKeys)
{
}
PropertySet& PropertySet::operator= (const PropertySet& other)
{
properties = other.properties;
fallbackProperties = other.fallbackProperties;
ignoreCaseOfKeys = other.ignoreCaseOfKeys;
propertyChanged();
return *this;
}
PropertySet::~PropertySet()
{
}
void PropertySet::clear()
{
const ScopedLock sl (lock);
if (properties.size() > 0)
{
properties.clear();
propertyChanged();
}
}
String PropertySet::getValue (StringRef keyName, const String& defaultValue) const noexcept
{
const ScopedLock sl (lock);
auto index = properties.getAllKeys().indexOf (keyName, ignoreCaseOfKeys);
if (index >= 0)
return properties.getAllValues() [index];
return fallbackProperties != nullptr ? fallbackProperties->getValue (keyName, defaultValue)
: defaultValue;
}
int PropertySet::getIntValue (StringRef keyName, int defaultValue) const noexcept
{
const ScopedLock sl (lock);
auto index = properties.getAllKeys().indexOf (keyName, ignoreCaseOfKeys);
if (index >= 0)
return properties.getAllValues() [index].getIntValue();
return fallbackProperties != nullptr ? fallbackProperties->getIntValue (keyName, defaultValue)
: defaultValue;
}
double PropertySet::getDoubleValue (StringRef keyName, double defaultValue) const noexcept
{
const ScopedLock sl (lock);
auto index = properties.getAllKeys().indexOf (keyName, ignoreCaseOfKeys);
if (index >= 0)
return properties.getAllValues()[index].getDoubleValue();
return fallbackProperties != nullptr ? fallbackProperties->getDoubleValue (keyName, defaultValue)
: defaultValue;
}
bool PropertySet::getBoolValue (StringRef keyName, bool defaultValue) const noexcept
{
const ScopedLock sl (lock);
auto index = properties.getAllKeys().indexOf (keyName, ignoreCaseOfKeys);
if (index >= 0)
return properties.getAllValues() [index].getIntValue() != 0;
return fallbackProperties != nullptr ? fallbackProperties->getBoolValue (keyName, defaultValue)
: defaultValue;
}
std::unique_ptr<XmlElement> PropertySet::getXmlValue (StringRef keyName) const
{
return parseXML (getValue (keyName));
}
void PropertySet::setValue (StringRef keyName, const var& v)
{
jassert (keyName.isNotEmpty()); // shouldn't use an empty key name!
if (keyName.isNotEmpty())
{
auto value = v.toString();
const ScopedLock sl (lock);
auto index = properties.getAllKeys().indexOf (keyName, ignoreCaseOfKeys);
if (index < 0 || properties.getAllValues() [index] != value)
{
properties.set (keyName, value);
propertyChanged();
}
}
}
void PropertySet::removeValue (StringRef keyName)
{
if (keyName.isNotEmpty())
{
const ScopedLock sl (lock);
auto index = properties.getAllKeys().indexOf (keyName, ignoreCaseOfKeys);
if (index >= 0)
{
properties.remove (keyName);
propertyChanged();
}
}
}
void PropertySet::setValue (StringRef keyName, const XmlElement* xml)
{
setValue (keyName, xml == nullptr ? var()
: var (xml->toString (XmlElement::TextFormat().singleLine().withoutHeader())));
}
bool PropertySet::containsKey (StringRef keyName) const noexcept
{
const ScopedLock sl (lock);
return properties.getAllKeys().contains (keyName, ignoreCaseOfKeys);
}
void PropertySet::addAllPropertiesFrom (const PropertySet& source)
{
const ScopedLock sl (source.getLock());
for (int i = 0; i < source.properties.size(); ++i)
setValue (source.properties.getAllKeys() [i],
source.properties.getAllValues() [i]);
}
void PropertySet::setFallbackPropertySet (PropertySet* fallbackProperties_) noexcept
{
const ScopedLock sl (lock);
fallbackProperties = fallbackProperties_;
}
std::unique_ptr<XmlElement> PropertySet::createXml (const String& nodeName) const
{
auto xml = std::make_unique<XmlElement> (nodeName);
const ScopedLock sl (lock);
for (int i = 0; i < properties.getAllKeys().size(); ++i)
{
auto e = xml->createNewChildElement ("VALUE");
e->setAttribute ("name", properties.getAllKeys()[i]);
e->setAttribute ("val", properties.getAllValues()[i]);
}
return xml;
}
void PropertySet::restoreFromXml (const XmlElement& xml)
{
const ScopedLock sl (lock);
clear();
for (auto* e : xml.getChildWithTagNameIterator ("VALUE"))
{
if (e->hasAttribute ("name")
&& e->hasAttribute ("val"))
{
properties.set (e->getStringAttribute ("name"),
e->getStringAttribute ("val"));
}
}
if (properties.size() > 0)
propertyChanged();
}
void PropertySet::propertyChanged()
{
}
} // namespace juce
juce::PropertySet::propertyChanged
virtual void propertyChanged()
Definition juce_PropertySet.cpp:213
juce::PropertySet::getLock
const CriticalSection & getLock() const noexcept
Definition juce_PropertySet.h:157
juce::PropertySet::setFallbackPropertySet
void setFallbackPropertySet(PropertySet *fallbackProperties) noexcept
Definition juce_PropertySet.cpp:172
juce::PropertySet::clear
void clear()
Definition juce_PropertySet.cpp:54
juce::PropertySet::~PropertySet
virtual ~PropertySet()
Definition juce_PropertySet.cpp:50
juce::PropertySet::addAllPropertiesFrom
void addAllPropertiesFrom(const PropertySet &source)
Definition juce_PropertySet.cpp:163
juce::PropertySet::PropertySet
PropertySet(bool ignoreCaseOfKeyNames=false)
Definition juce_PropertySet.cpp:26
juce::PropertySet::getValue
String getValue(StringRef keyName, const String &defaultReturnValue=String()) const noexcept
Definition juce_PropertySet.cpp:65
juce::PropertySet::setValue
void setValue(StringRef keyName, const var &value)
Definition juce_PropertySet.cpp:118
juce::PropertySet::getDoubleValue
double getDoubleValue(StringRef keyName, double defaultReturnValue=0.0) const noexcept
Definition juce_PropertySet.cpp:89
juce::PropertySet::containsKey
bool containsKey(StringRef keyName) const noexcept
Definition juce_PropertySet.cpp:157
juce::PropertySet::removeValue
void removeValue(StringRef keyName)
Definition juce_PropertySet.cpp:136
juce::PropertySet::getIntValue
int getIntValue(StringRef keyName, int defaultReturnValue=0) const noexcept
Definition juce_PropertySet.cpp:77
juce::PropertySet::getBoolValue
bool getBoolValue(StringRef keyName, bool defaultReturnValue=false) const noexcept
Definition juce_PropertySet.cpp:101
juce::PropertySet::restoreFromXml
void restoreFromXml(const XmlElement &xml)
Definition juce_PropertySet.cpp:194
juce::PropertySet::createXml
std::unique_ptr< XmlElement > createXml(const String &nodeName) const
Definition juce_PropertySet.cpp:178
juce::PropertySet::getXmlValue
std::unique_ptr< XmlElement > getXmlValue(StringRef keyName) const
Definition juce_PropertySet.cpp:113
juce::PropertySet::operator=
PropertySet & operator=(const PropertySet &other)
Definition juce_PropertySet.cpp:40
juce::StringPairArray::getAllValues
const StringArray & getAllValues() const noexcept
Definition juce_StringPairArray.h:90
juce::StringPairArray::size
int size() const noexcept
Definition juce_StringPairArray.h:93
juce::StringPairArray::getAllKeys
const StringArray & getAllKeys() const noexcept
Definition juce_StringPairArray.h:87
juce::StringRef
Definition juce_StringRef.h:62
juce::StringRef::isNotEmpty
bool isNotEmpty() const noexcept
Definition juce_StringRef.h:103
juce::String
Definition juce_String.h:53
juce::XmlElement
Definition juce_XmlElement.h:83
juce::XmlElement::toString
String toString(const TextFormat &format={}) const
Definition juce_XmlElement.cpp:352
juce::XmlElement::getChildWithTagNameIterator
Iterator< GetNextElementWithTagName > getChildWithTagNameIterator(StringRef name) const
Definition juce_XmlElement.h:730
juce::var
Definition juce_Variant.h:42
juce::XmlElement::TextFormat
Definition juce_XmlElement.h:136
juce::XmlElement::TextFormat::withoutHeader
TextFormat withoutHeader() const
Definition juce_XmlElement.cpp:345
juce::XmlElement::TextFormat::singleLine
TextFormat singleLine() const
Definition juce_XmlElement.cpp:338