OpenShot Audio Library | OpenShotAudio: juce_PropertySet.cpp Source File

OpenShot Audio Library | OpenShotAudio 1.0.0

Loading...
Searching...
No Matches
juce_PropertySet.cpp
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
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
33 PropertySet::PropertySet (const PropertySet& other)
34 : properties (other.properties),
35 fallbackProperties (other.fallbackProperties),
36 ignoreCaseOfKeys (other.ignoreCaseOfKeys)
37{
38}
39
40 PropertySet& PropertySet::operator= (const PropertySet& other)
41{
42 properties = other.properties;
43 fallbackProperties = other.fallbackProperties;
44 ignoreCaseOfKeys = other.ignoreCaseOfKeys;
45
46 propertyChanged();
47 return *this;
48}
49
50PropertySet::~PropertySet()
51{
52}
53
54 void PropertySet::clear()
55{
56 const ScopedLock sl (lock);
57
58 if (properties.size() > 0)
59 {
60 properties.clear();
61 propertyChanged();
62 }
63}
64
65 String 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
77 int 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
89 double 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
101 bool 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
118 void PropertySet::setValue (StringRef 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);
131 propertyChanged();
132 }
133 }
134}
135
136 void PropertySet::removeValue (StringRef keyName)
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);
146 propertyChanged();
147 }
148 }
149}
150
151 void PropertySet::setValue (StringRef keyName, const XmlElement* xml)
152{
153 setValue (keyName, xml == nullptr ? var()
154 : var (xml->toString (XmlElement::TextFormat().singleLine().withoutHeader())));
155}
156
157 bool PropertySet::containsKey (StringRef keyName) const noexcept
158{
159 const ScopedLock sl (lock);
160 return properties.getAllKeys().contains (keyName, ignoreCaseOfKeys);
161}
162
163 void PropertySet::addAllPropertiesFrom (const PropertySet& source)
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
172 void 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
194 void PropertySet::restoreFromXml (const XmlElement& xml)
195{
196 const ScopedLock sl (lock);
197 clear();
198
199 for (auto* e : xml.getChildWithTagNameIterator ("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)
210 propertyChanged();
211}
212
213 void PropertySet::propertyChanged()
214{
215}
216
217} // 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::var::var
var() noexcept
Definition juce_Variant.cpp:497
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