OpenShot Library | libopenshot-audio 0.2.0
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 (const 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
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
69 const int index = properties.getAllKeys().indexOf (keyName, ignoreCaseOfKeys);
70
71 if (index >= 0)
72 return properties.getAllValues() [index];
73
74 return fallbackProperties != nullptr ? fallbackProperties->getValue (keyName, defaultValue)
75 : defaultValue;
76}
77
78int PropertySet::getIntValue (StringRef keyName, const int defaultValue) const noexcept
79{
80 const ScopedLock sl (lock);
81 const int index = properties.getAllKeys().indexOf (keyName, ignoreCaseOfKeys);
82
83 if (index >= 0)
84 return properties.getAllValues() [index].getIntValue();
85
86 return fallbackProperties != nullptr ? fallbackProperties->getIntValue (keyName, defaultValue)
87 : defaultValue;
88}
89
90double PropertySet::getDoubleValue (StringRef keyName, const double defaultValue) const noexcept
91{
92 const ScopedLock sl (lock);
93 const int index = properties.getAllKeys().indexOf (keyName, ignoreCaseOfKeys);
94
95 if (index >= 0)
96 return properties.getAllValues()[index].getDoubleValue();
97
98 return fallbackProperties != nullptr ? fallbackProperties->getDoubleValue (keyName, defaultValue)
99 : defaultValue;
100}
101
102bool PropertySet::getBoolValue (StringRef keyName, const bool defaultValue) const noexcept
103{
104 const ScopedLock sl (lock);
105 const int index = properties.getAllKeys().indexOf (keyName, ignoreCaseOfKeys);
106
107 if (index >= 0)
108 return properties.getAllValues() [index].getIntValue() != 0;
109
110 return fallbackProperties != nullptr ? fallbackProperties->getBoolValue (keyName, defaultValue)
111 : defaultValue;
112}
113
118
120{
121 jassert (keyName.isNotEmpty()); // shouldn't use an empty key name!
122
123 if (keyName.isNotEmpty())
124 {
125 const String value (v.toString());
126 const ScopedLock sl (lock);
127
128 const int index = properties.getAllKeys().indexOf (keyName, ignoreCaseOfKeys);
129
130 if (index < 0 || properties.getAllValues() [index] != value)
131 {
132 properties.set (keyName, value);
134 }
135 }
136}
137
139{
140 if (keyName.isNotEmpty())
141 {
142 const ScopedLock sl (lock);
143 const int index = properties.getAllKeys().indexOf (keyName, ignoreCaseOfKeys);
144
145 if (index >= 0)
146 {
147 properties.remove (keyName);
149 }
150 }
151}
152
154{
155 setValue (keyName, xml == nullptr ? var()
156 : var (xml->createDocument ("", true)));
157}
158
160{
161 const ScopedLock sl (lock);
162 return properties.getAllKeys().contains (keyName, ignoreCaseOfKeys);
163}
164
166{
167 const ScopedLock sl (source.getLock());
168
169 for (int i = 0; i < source.properties.size(); ++i)
170 setValue (source.properties.getAllKeys() [i],
171 source.properties.getAllValues() [i]);
172}
173
175{
176 const ScopedLock sl (lock);
177 fallbackProperties = fallbackProperties_;
178}
179
181{
182 const ScopedLock sl (lock);
183 XmlElement* const xml = new XmlElement (nodeName);
184
185 for (int i = 0; i < properties.getAllKeys().size(); ++i)
186 {
187 XmlElement* const e = xml->createNewChildElement ("VALUE");
188 e->setAttribute ("name", properties.getAllKeys()[i]);
189 e->setAttribute ("val", properties.getAllValues()[i]);
190 }
191
192 return xml;
193}
194
196{
197 const ScopedLock sl (lock);
198 clear();
199
200 forEachXmlChildElementWithTagName (xml, e, "VALUE")
201 {
202 if (e->hasAttribute ("name")
203 && e->hasAttribute ("val"))
204 {
205 properties.set (e->getStringAttribute ("name"),
206 e->getStringAttribute ("val"));
207 }
208 }
209
210 if (properties.size() > 0)
212}
213
217
218} // namespace juce
Holds a resizable array of primitive or copy-by-value objects.
Definition juce_Array.h:60
int indexOf(ParameterType elementToLookFor) const
Finds the index of the first element which matches the value passed in.
Definition juce_Array.h:339
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 properies 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.
XmlElement * createXml(const String &nodeName) const
Returns an XML element which encapsulates all the items in this property set.
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 properies 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.
PropertySet & operator=(const PropertySet &other)
Copies another PropertySet over this one.
XmlElement * getXmlValue(StringRef keyName) const
Returns one of the properties as an XML element.
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.
The JUCE String class!
Definition juce_String.h:43
static XmlElement * parse(const File &file)
A handy static method that parses a file.
Used to build a tree of elements representing an XML document.
A variant class, that can be used to hold a range of primitive values.