juce_PropertySet.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. ==============================================================================
  3. This file is part of the juce_core module of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. or without fee is hereby granted, provided that the above copyright notice and this
  7. permission notice appear in all copies.
  8. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  9. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  10. NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  11. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  12. IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. ------------------------------------------------------------------------------
  15. NOTE! This permissive ISC license applies ONLY to files within the juce_core module!
  16. All other JUCE modules are covered by a dual GPL/commercial license, so if you are
  17. using any other modules, be sure to check that you also comply with their license.
  18. For more details, visit www.juce.com
  19. ==============================================================================
  20. */
  21. PropertySet::PropertySet (const bool ignoreCaseOfKeyNames)
  22. : properties (ignoreCaseOfKeyNames),
  23. fallbackProperties (nullptr),
  24. ignoreCaseOfKeys (ignoreCaseOfKeyNames)
  25. {
  26. }
  27. PropertySet::PropertySet (const PropertySet& other)
  28. : properties (other.properties),
  29. fallbackProperties (other.fallbackProperties),
  30. ignoreCaseOfKeys (other.ignoreCaseOfKeys)
  31. {
  32. }
  33. PropertySet& PropertySet::operator= (const PropertySet& other)
  34. {
  35. properties = other.properties;
  36. fallbackProperties = other.fallbackProperties;
  37. ignoreCaseOfKeys = other.ignoreCaseOfKeys;
  38. propertyChanged();
  39. return *this;
  40. }
  41. PropertySet::~PropertySet()
  42. {
  43. }
  44. void PropertySet::clear()
  45. {
  46. const ScopedLock sl (lock);
  47. if (properties.size() > 0)
  48. {
  49. properties.clear();
  50. propertyChanged();
  51. }
  52. }
  53. String PropertySet::getValue (StringRef keyName, const String& defaultValue) const noexcept
  54. {
  55. const ScopedLock sl (lock);
  56. const int index = properties.getAllKeys().indexOf (keyName, ignoreCaseOfKeys);
  57. if (index >= 0)
  58. return properties.getAllValues() [index];
  59. return fallbackProperties != nullptr ? fallbackProperties->getValue (keyName, defaultValue)
  60. : defaultValue;
  61. }
  62. int PropertySet::getIntValue (StringRef keyName, const int defaultValue) const noexcept
  63. {
  64. const ScopedLock sl (lock);
  65. const int index = properties.getAllKeys().indexOf (keyName, ignoreCaseOfKeys);
  66. if (index >= 0)
  67. return properties.getAllValues() [index].getIntValue();
  68. return fallbackProperties != nullptr ? fallbackProperties->getIntValue (keyName, defaultValue)
  69. : defaultValue;
  70. }
  71. double PropertySet::getDoubleValue (StringRef keyName, const double defaultValue) const noexcept
  72. {
  73. const ScopedLock sl (lock);
  74. const int index = properties.getAllKeys().indexOf (keyName, ignoreCaseOfKeys);
  75. if (index >= 0)
  76. return properties.getAllValues()[index].getDoubleValue();
  77. return fallbackProperties != nullptr ? fallbackProperties->getDoubleValue (keyName, defaultValue)
  78. : defaultValue;
  79. }
  80. bool PropertySet::getBoolValue (StringRef keyName, const bool defaultValue) const noexcept
  81. {
  82. const ScopedLock sl (lock);
  83. const int index = properties.getAllKeys().indexOf (keyName, ignoreCaseOfKeys);
  84. if (index >= 0)
  85. return properties.getAllValues() [index].getIntValue() != 0;
  86. return fallbackProperties != nullptr ? fallbackProperties->getBoolValue (keyName, defaultValue)
  87. : defaultValue;
  88. }
  89. XmlElement* PropertySet::getXmlValue (StringRef keyName) const
  90. {
  91. return XmlDocument::parse (getValue (keyName));
  92. }
  93. void PropertySet::setValue (const String& keyName, const var& v)
  94. {
  95. jassert (keyName.isNotEmpty()); // shouldn't use an empty key name!
  96. if (keyName.isNotEmpty())
  97. {
  98. const String value (v.toString());
  99. const ScopedLock sl (lock);
  100. const int index = properties.getAllKeys().indexOf (keyName, ignoreCaseOfKeys);
  101. if (index < 0 || properties.getAllValues() [index] != value)
  102. {
  103. properties.set (keyName, value);
  104. propertyChanged();
  105. }
  106. }
  107. }
  108. void PropertySet::removeValue (StringRef keyName)
  109. {
  110. if (keyName.isNotEmpty())
  111. {
  112. const ScopedLock sl (lock);
  113. const int index = properties.getAllKeys().indexOf (keyName, ignoreCaseOfKeys);
  114. if (index >= 0)
  115. {
  116. properties.remove (keyName);
  117. propertyChanged();
  118. }
  119. }
  120. }
  121. void PropertySet::setValue (const String& keyName, const XmlElement* const xml)
  122. {
  123. setValue (keyName, xml == nullptr ? var()
  124. : var (xml->createDocument ("", true)));
  125. }
  126. bool PropertySet::containsKey (StringRef keyName) const noexcept
  127. {
  128. const ScopedLock sl (lock);
  129. return properties.getAllKeys().contains (keyName, ignoreCaseOfKeys);
  130. }
  131. void PropertySet::addAllPropertiesFrom (const PropertySet& source)
  132. {
  133. const ScopedLock sl (source.getLock());
  134. for (int i = 0; i < source.properties.size(); ++i)
  135. setValue (source.properties.getAllKeys() [i],
  136. source.properties.getAllValues() [i]);
  137. }
  138. void PropertySet::setFallbackPropertySet (PropertySet* fallbackProperties_) noexcept
  139. {
  140. const ScopedLock sl (lock);
  141. fallbackProperties = fallbackProperties_;
  142. }
  143. XmlElement* PropertySet::createXml (const String& nodeName) const
  144. {
  145. const ScopedLock sl (lock);
  146. XmlElement* const xml = new XmlElement (nodeName);
  147. for (int i = 0; i < properties.getAllKeys().size(); ++i)
  148. {
  149. XmlElement* const e = xml->createNewChildElement ("VALUE");
  150. e->setAttribute ("name", properties.getAllKeys()[i]);
  151. e->setAttribute ("val", properties.getAllValues()[i]);
  152. }
  153. return xml;
  154. }
  155. void PropertySet::restoreFromXml (const XmlElement& xml)
  156. {
  157. const ScopedLock sl (lock);
  158. clear();
  159. forEachXmlChildElementWithTagName (xml, e, "VALUE")
  160. {
  161. if (e->hasAttribute ("name")
  162. && e->hasAttribute ("val"))
  163. {
  164. properties.set (e->getStringAttribute ("name"),
  165. e->getStringAttribute ("val"));
  166. }
  167. }
  168. if (properties.size() > 0)
  169. propertyChanged();
  170. }
  171. void PropertySet::propertyChanged()
  172. {
  173. }