OpenShot Library | libopenshot-audio  0.1.9
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 
23 namespace juce
24 {
25 
26 PropertySet::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 
51 {
52 }
53 
55 {
56  const ScopedLock sl (lock);
57 
58  if (properties.size() > 0)
59  {
60  properties.clear();
62  }
63 }
64 
65 String 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 
78 int 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 
90 double 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 
102 bool 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 
115 {
116  return XmlDocument::parse (getValue (keyName));
117 }
118 
119 void PropertySet::setValue (const String& keyName, const var& v)
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);
133  propertyChanged();
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);
148  propertyChanged();
149  }
150  }
151 }
152 
153 void PropertySet::setValue (const String& keyName, const XmlElement* const xml)
154 {
155  setValue (keyName, xml == nullptr ? var()
156  : var (xml->createDocument ("", true)));
157 }
158 
159 bool PropertySet::containsKey (StringRef keyName) const noexcept
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 
174 void PropertySet::setFallbackPropertySet (PropertySet* fallbackProperties_) noexcept
175 {
176  const ScopedLock sl (lock);
177  fallbackProperties = fallbackProperties_;
178 }
179 
180 XmlElement* PropertySet::createXml (const String& nodeName) const
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)
211  propertyChanged();
212 }
213 
215 {
216 }
217 
218 } // namespace juce
const StringArray & getAllValues() const noexcept
Returns a list of all values in the array.
XmlElement * getXmlValue(StringRef keyName) const
Returns one of the properties as an XML element.
bool isNotEmpty() const noexcept
Returns true if the string contains at least one character.
Definition: juce_String.h:306
A simple class for holding temporary references to a string literal or String.
virtual ~PropertySet()
Destructor.
A variant class, that can be used to hold a range of primitive values.
Definition: juce_Variant.h:45
A set of named property values, which can be strings, integers, floating point, etc.
const StringArray & getAllKeys() const noexcept
Returns a list of all keys in the array.
void restoreFromXml(const XmlElement &xml)
Reloads a set of properties that were previously stored as XML.
virtual void propertyChanged()
Subclasses can override this to be told when one of the properies has been changed.
bool isNotEmpty() const noexcept
Returns true if the string is not empty.
Used to build a tree of elements representing an XML document.
PropertySet & operator=(const PropertySet &other)
Copies another PropertySet over this one.
String getValue(StringRef keyName, const String &defaultReturnValue=String()) const noexcept
Returns one of the properties as a string.
bool getBoolValue(StringRef keyName, bool defaultReturnValue=false) const noexcept
Returns one of the properties as an boolean.
The JUCE String class!
Definition: juce_String.h:42
void clear()
Removes all elements from the array.
int size() const noexcept
Returns the number of strings in the array.
bool containsKey(StringRef keyName) const noexcept
Returns true if the properies include the given key.
XmlElement * createNewChildElement(StringRef tagName)
Creates a new element with the given name and returns it, after adding it as a child element...
int indexOf(StringRef stringToLookFor, bool ignoreCase=false, int startIndex=0) const
Searches for a string in the array.
XmlElement * createXml(const String &nodeName) const
Returns an XML element which encapsulates all the items in this property set.
void setAttribute(const Identifier &attributeName, const String &newValue)
Adds a named attribute to the element.
void removeValue(StringRef keyName)
Deletes a property.
int getIntValue(StringRef keyName, int defaultReturnValue=0) const noexcept
Returns one of the properties as an integer.
String createDocument(StringRef dtdToUse, bool allOnOneLine=false, bool includeXmlHeader=true, StringRef encodingType="UTF-8", int lineWrapLength=60) const
Returns an XML text document that represents this element.
void addAllPropertiesFrom(const PropertySet &source)
This copies all the values from a source PropertySet to this one.
void set(const String &key, const String &value)
Adds or amends a key/value pair.
bool contains(StringRef stringToLookFor, bool ignoreCase=false) const
Searches for a string in the array.
static XmlElement * parse(const File &file)
A handy static method that parses a file.
int size() const noexcept
Returns the number of strings in the array.
void remove(StringRef key)
Removes a string from the array based on its key.
double getDoubleValue(StringRef keyName, double defaultReturnValue=0.0) const noexcept
Returns one of the properties as an double.
void setValue(const String &keyName, const var &value)
Sets a named property.
void clear()
Removes all values.
void setFallbackPropertySet(PropertySet *fallbackProperties) noexcept
Sets up a second PopertySet that will be used to look up any values that aren&#39;t set in this one...
Automatically locks and unlocks a mutex object.
PropertySet(bool ignoreCaseOfKeyNames=false)
Creates an empty PropertySet.
const CriticalSection & getLock() const noexcept
Returns the lock used when reading or writing to this set.