OpenShot Library | OpenShotAudio 0.2.2
juce_PropertySet.cpp
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2017 - ROLI Ltd.
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
15
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18 DISCLAIMED.
19
20 ==============================================================================
21*/
22
23namespace juce
24{
25
26PropertySet::PropertySet (bool ignoreCaseOfKeyNames)
27 : properties (ignoreCaseOfKeyNames),
28 fallbackProperties (nullptr),
29 ignoreCaseOfKeys (ignoreCaseOfKeyNames)
30{
31}
32
34 : properties (other.properties),
35 fallbackProperties (other.fallbackProperties),
36 ignoreCaseOfKeys (other.ignoreCaseOfKeys)
37{
38}
39
41{
42 properties = other.properties;
43 fallbackProperties = other.fallbackProperties;
44 ignoreCaseOfKeys = other.ignoreCaseOfKeys;
45
47 return *this;
48}
49
51{
52}
53
55{
56 const ScopedLock sl (lock);
57
58 if (properties.size() > 0)
59 {
60 properties.clear();
62 }
63}
64
65String PropertySet::getValue (StringRef keyName, const String& defaultValue) const noexcept
66{
67 const ScopedLock sl (lock);
68 auto index = properties.getAllKeys().indexOf (keyName, ignoreCaseOfKeys);
69
70 if (index >= 0)
71 return properties.getAllValues() [index];
72
73 return fallbackProperties != nullptr ? fallbackProperties->getValue (keyName, defaultValue)
74 : defaultValue;
75}
76
77int PropertySet::getIntValue (StringRef keyName, int defaultValue) const noexcept
78{
79 const ScopedLock sl (lock);
80 auto index = properties.getAllKeys().indexOf (keyName, ignoreCaseOfKeys);
81
82 if (index >= 0)
83 return properties.getAllValues() [index].getIntValue();
84
85 return fallbackProperties != nullptr ? fallbackProperties->getIntValue (keyName, defaultValue)
86 : defaultValue;
87}
88
89double PropertySet::getDoubleValue (StringRef keyName, double defaultValue) const noexcept
90{
91 const ScopedLock sl (lock);
92 auto index = properties.getAllKeys().indexOf (keyName, ignoreCaseOfKeys);
93
94 if (index >= 0)
95 return properties.getAllValues()[index].getDoubleValue();
96
97 return fallbackProperties != nullptr ? fallbackProperties->getDoubleValue (keyName, defaultValue)
98 : defaultValue;
99}
100
101bool PropertySet::getBoolValue (StringRef keyName, bool defaultValue) const noexcept
102{
103 const ScopedLock sl (lock);
104 auto index = properties.getAllKeys().indexOf (keyName, ignoreCaseOfKeys);
105
106 if (index >= 0)
107 return properties.getAllValues() [index].getIntValue() != 0;
108
109 return fallbackProperties != nullptr ? fallbackProperties->getBoolValue (keyName, defaultValue)
110 : defaultValue;
111}
112
113std::unique_ptr<XmlElement> PropertySet::getXmlValue (StringRef keyName) const
114{
115 return parseXML (getValue (keyName));
116}
117
118void PropertySet::setValue (const String& keyName, const var& v)
119{
120 jassert (keyName.isNotEmpty()); // shouldn't use an empty key name!
121
122 if (keyName.isNotEmpty())
123 {
124 auto value = v.toString();
125 const ScopedLock sl (lock);
126 auto index = properties.getAllKeys().indexOf (keyName, ignoreCaseOfKeys);
127
128 if (index < 0 || properties.getAllValues() [index] != value)
129 {
130 properties.set (keyName, value);
132 }
133 }
134}
135
137{
138 if (keyName.isNotEmpty())
139 {
140 const ScopedLock sl (lock);
141 auto index = properties.getAllKeys().indexOf (keyName, ignoreCaseOfKeys);
142
143 if (index >= 0)
144 {
145 properties.remove (keyName);
147 }
148 }
149}
150
151void PropertySet::setValue (const String& keyName, const XmlElement* xml)
152{
153 setValue (keyName, xml == nullptr ? var()
155}
156
157bool PropertySet::containsKey (StringRef keyName) const noexcept
158{
159 const ScopedLock sl (lock);
160 return properties.getAllKeys().contains (keyName, ignoreCaseOfKeys);
161}
162
164{
165 const ScopedLock sl (source.getLock());
166
167 for (int i = 0; i < source.properties.size(); ++i)
168 setValue (source.properties.getAllKeys() [i],
169 source.properties.getAllValues() [i]);
170}
171
172void PropertySet::setFallbackPropertySet (PropertySet* fallbackProperties_) noexcept
173{
174 const ScopedLock sl (lock);
175 fallbackProperties = fallbackProperties_;
176}
177
178std::unique_ptr<XmlElement> PropertySet::createXml (const String& nodeName) const
179{
180 auto xml = std::make_unique<XmlElement> (nodeName);
181
182 const ScopedLock sl (lock);
183
184 for (int i = 0; i < properties.getAllKeys().size(); ++i)
185 {
186 auto e = xml->createNewChildElement ("VALUE");
187 e->setAttribute ("name", properties.getAllKeys()[i]);
188 e->setAttribute ("val", properties.getAllValues()[i]);
189 }
190
191 return xml;
192}
193
195{
196 const ScopedLock sl (lock);
197 clear();
198
199 forEachXmlChildElementWithTagName (xml, e, "VALUE")
200 {
201 if (e->hasAttribute ("name")
202 && e->hasAttribute ("val"))
203 {
204 properties.set (e->getStringAttribute ("name"),
205 e->getStringAttribute ("val"));
206 }
207 }
208
209 if (properties.size() > 0)
211}
212
214{
215}
216
217} // namespace juce
Automatically locks and unlocks a mutex object.
A set of named property values, which can be strings, integers, floating point, etc.
virtual void propertyChanged()
Subclasses can override this to be told when one of the properties has been changed.
const CriticalSection & getLock() const noexcept
Returns the lock used when reading or writing to this set.
void setFallbackPropertySet(PropertySet *fallbackProperties) noexcept
Sets up a second PopertySet that will be used to look up any values that aren't set in this one.
void clear()
Removes all values.
virtual ~PropertySet()
Destructor.
void addAllPropertiesFrom(const PropertySet &source)
This copies all the values from a source PropertySet to this one.
PropertySet(bool ignoreCaseOfKeyNames=false)
Creates an empty PropertySet.
String getValue(StringRef keyName, const String &defaultReturnValue=String()) const noexcept
Returns one of the properties as a string.
double getDoubleValue(StringRef keyName, double defaultReturnValue=0.0) const noexcept
Returns one of the properties as an double.
bool containsKey(StringRef keyName) const noexcept
Returns true if the properties include the given key.
void removeValue(StringRef keyName)
Deletes a property.
int getIntValue(StringRef keyName, int defaultReturnValue=0) const noexcept
Returns one of the properties as an integer.
void setValue(const String &keyName, const var &value)
Sets a named property.
bool getBoolValue(StringRef keyName, bool defaultReturnValue=false) const noexcept
Returns one of the properties as an boolean.
void restoreFromXml(const XmlElement &xml)
Reloads a set of properties that were previously stored as XML.
std::unique_ptr< XmlElement > createXml(const String &nodeName) const
Returns an XML element which encapsulates all the items in this property set.
std::unique_ptr< XmlElement > getXmlValue(StringRef keyName) const
Returns one of the properties as an XML element.
PropertySet & operator=(const PropertySet &other)
Copies another PropertySet over this one.
int indexOf(StringRef stringToLookFor, bool ignoreCase=false, int startIndex=0) const
Searches for a string in the array.
void set(const String &key, const String &value)
Adds or amends a key/value pair.
void clear()
Removes all elements from the array.
void remove(StringRef key)
Removes a string from the array based on its key.
const StringArray & getAllValues() const noexcept
Returns a list of all values in the array.
int size() const noexcept
Returns the number of strings in the array.
const StringArray & getAllKeys() const noexcept
Returns a list of all keys in the array.
A simple class for holding temporary references to a string literal or String.
bool isNotEmpty() const noexcept
Returns true if the string is not empty.
The JUCE String class!
Definition: juce_String.h:43
bool isNotEmpty() const noexcept
Returns true if the string contains at least one character.
Definition: juce_String.h:306
Used to build a tree of elements representing an XML document.
String toString(const TextFormat &format={}) const
Returns a text version of this XML element.
A variant class, that can be used to hold a range of primitive values.
Definition: juce_Variant.h:46
A struct containing options for formatting the text when representing an XML element as a string.
TextFormat withoutHeader() const
returns a copy of this format with the addDefaultHeader flag set to false.
TextFormat singleLine() const
returns a copy of this format with newLineChars set to nullptr.