LMMS
Loading...
Searching...
No Matches
juce_PropertySet.cpp
Go to the documentation of this file.
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),
29 ignoreCaseOfKeys (ignoreCaseOfKeyNames)
30{
31}
32
39
40PropertySet& PropertySet::operator= (const PropertySet& other)
41{
42 properties = other.properties;
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 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 (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);
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
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 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)
211}
212
216
217} // namespace juce
#define nullptr
Definition DistrhoDefines.h:75
virtual void propertyChanged()
Definition juce_PropertySet.cpp:213
StringPairArray properties
Definition juce_PropertySet.h:197
const CriticalSection & getLock() const noexcept
Definition juce_PropertySet.h:157
void setFallbackPropertySet(PropertySet *fallbackProperties) noexcept
Definition juce_PropertySet.cpp:172
PropertySet * fallbackProperties
Definition juce_PropertySet.h:198
bool ignoreCaseOfKeys
Definition juce_PropertySet.h:200
void clear()
Definition juce_PropertySet.cpp:54
virtual ~PropertySet()
Definition juce_PropertySet.cpp:50
void addAllPropertiesFrom(const PropertySet &source)
Definition juce_PropertySet.cpp:163
PropertySet(bool ignoreCaseOfKeyNames=false)
Definition juce_PropertySet.cpp:26
CriticalSection lock
Definition juce_PropertySet.h:199
String getValue(StringRef keyName, const String &defaultReturnValue=String()) const noexcept
Definition juce_PropertySet.cpp:65
void setValue(StringRef keyName, const var &value)
Definition juce_PropertySet.cpp:118
double getDoubleValue(StringRef keyName, double defaultReturnValue=0.0) const noexcept
Definition juce_PropertySet.cpp:89
bool containsKey(StringRef keyName) const noexcept
Definition juce_PropertySet.cpp:157
void removeValue(StringRef keyName)
Definition juce_PropertySet.cpp:136
int getIntValue(StringRef keyName, int defaultReturnValue=0) const noexcept
Definition juce_PropertySet.cpp:77
bool getBoolValue(StringRef keyName, bool defaultReturnValue=false) const noexcept
Definition juce_PropertySet.cpp:101
void restoreFromXml(const XmlElement &xml)
Definition juce_PropertySet.cpp:194
std::unique_ptr< XmlElement > createXml(const String &nodeName) const
Definition juce_PropertySet.cpp:178
std::unique_ptr< XmlElement > getXmlValue(StringRef keyName) const
Definition juce_PropertySet.cpp:113
Definition juce_String.h:53
const StringArray & getAllValues() const noexcept
Definition juce_StringPairArray.h:90
int size() const noexcept
Definition juce_StringPairArray.h:93
const StringArray & getAllKeys() const noexcept
Definition juce_StringPairArray.h:87
Definition juce_StringRef.h:62
bool isNotEmpty() const noexcept
Definition juce_StringRef.h:103
Definition juce_XmlElement.h:83
String toString(const TextFormat &format={}) const
Definition juce_XmlElement.cpp:352
Iterator< GetNextElementWithTagName > getChildWithTagNameIterator(StringRef name) const
Definition juce_XmlElement.h:730
Definition juce_Variant.h:42
* e
Definition inflate.c:1404
unsigned v[N_MAX]
Definition inflate.c:1584
register unsigned i
Definition inflate.c:1575
static PuglViewHint int value
Definition pugl.h:1708
#define jassert(expression)
Definition carla_juce.cpp:31
CriticalSection::ScopedLockType ScopedLock
Definition juce_CriticalSection.h:186
std::unique_ptr< XmlElement > parseXML(const String &textToParse)
Definition juce_XmlDocument.cpp:41
Definition juce_XmlElement.h:136
JUCE_NODISCARD TextFormat withoutHeader() const
Definition juce_XmlElement.cpp:345
JUCE_NODISCARD TextFormat singleLine() const
Definition juce_XmlElement.cpp:338
ulg size
Definition extract.c:2350