The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

219 lines
6.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. PropertySet::PropertySet (const bool ignoreCaseOfKeyNames)
  20. : properties (ignoreCaseOfKeyNames),
  21. fallbackProperties (nullptr),
  22. ignoreCaseOfKeys (ignoreCaseOfKeyNames)
  23. {
  24. }
  25. PropertySet::PropertySet (const PropertySet& other)
  26. : properties (other.properties),
  27. fallbackProperties (other.fallbackProperties),
  28. ignoreCaseOfKeys (other.ignoreCaseOfKeys)
  29. {
  30. }
  31. PropertySet& PropertySet::operator= (const PropertySet& other)
  32. {
  33. properties = other.properties;
  34. fallbackProperties = other.fallbackProperties;
  35. ignoreCaseOfKeys = other.ignoreCaseOfKeys;
  36. propertyChanged();
  37. return *this;
  38. }
  39. PropertySet::~PropertySet()
  40. {
  41. }
  42. void PropertySet::clear()
  43. {
  44. const ScopedLock sl (lock);
  45. if (properties.size() > 0)
  46. {
  47. properties.clear();
  48. propertyChanged();
  49. }
  50. }
  51. String PropertySet::getValue (StringRef keyName, const String& defaultValue) const noexcept
  52. {
  53. const ScopedLock sl (lock);
  54. const int index = properties.getAllKeys().indexOf (keyName, ignoreCaseOfKeys);
  55. if (index >= 0)
  56. return properties.getAllValues() [index];
  57. return fallbackProperties != nullptr ? fallbackProperties->getValue (keyName, defaultValue)
  58. : defaultValue;
  59. }
  60. int PropertySet::getIntValue (StringRef keyName, const int defaultValue) const noexcept
  61. {
  62. const ScopedLock sl (lock);
  63. const int index = properties.getAllKeys().indexOf (keyName, ignoreCaseOfKeys);
  64. if (index >= 0)
  65. return properties.getAllValues() [index].getIntValue();
  66. return fallbackProperties != nullptr ? fallbackProperties->getIntValue (keyName, defaultValue)
  67. : defaultValue;
  68. }
  69. double PropertySet::getDoubleValue (StringRef keyName, const double defaultValue) const noexcept
  70. {
  71. const ScopedLock sl (lock);
  72. const int index = properties.getAllKeys().indexOf (keyName, ignoreCaseOfKeys);
  73. if (index >= 0)
  74. return properties.getAllValues()[index].getDoubleValue();
  75. return fallbackProperties != nullptr ? fallbackProperties->getDoubleValue (keyName, defaultValue)
  76. : defaultValue;
  77. }
  78. bool PropertySet::getBoolValue (StringRef keyName, const bool defaultValue) const noexcept
  79. {
  80. const ScopedLock sl (lock);
  81. const int index = properties.getAllKeys().indexOf (keyName, ignoreCaseOfKeys);
  82. if (index >= 0)
  83. return properties.getAllValues() [index].getIntValue() != 0;
  84. return fallbackProperties != nullptr ? fallbackProperties->getBoolValue (keyName, defaultValue)
  85. : defaultValue;
  86. }
  87. XmlElement* PropertySet::getXmlValue (StringRef keyName) const
  88. {
  89. return XmlDocument::parse (getValue (keyName));
  90. }
  91. void PropertySet::setValue (const String& keyName, const var& v)
  92. {
  93. jassert (keyName.isNotEmpty()); // shouldn't use an empty key name!
  94. if (keyName.isNotEmpty())
  95. {
  96. const String value (v.toString());
  97. const ScopedLock sl (lock);
  98. const int index = properties.getAllKeys().indexOf (keyName, ignoreCaseOfKeys);
  99. if (index < 0 || properties.getAllValues() [index] != value)
  100. {
  101. properties.set (keyName, value);
  102. propertyChanged();
  103. }
  104. }
  105. }
  106. void PropertySet::removeValue (StringRef keyName)
  107. {
  108. if (keyName.isNotEmpty())
  109. {
  110. const ScopedLock sl (lock);
  111. const int index = properties.getAllKeys().indexOf (keyName, ignoreCaseOfKeys);
  112. if (index >= 0)
  113. {
  114. properties.remove (keyName);
  115. propertyChanged();
  116. }
  117. }
  118. }
  119. void PropertySet::setValue (const String& keyName, const XmlElement* const xml)
  120. {
  121. setValue (keyName, xml == nullptr ? var()
  122. : var (xml->createDocument ("", true)));
  123. }
  124. bool PropertySet::containsKey (StringRef keyName) const noexcept
  125. {
  126. const ScopedLock sl (lock);
  127. return properties.getAllKeys().contains (keyName, ignoreCaseOfKeys);
  128. }
  129. void PropertySet::addAllPropertiesFrom (const PropertySet& source)
  130. {
  131. const ScopedLock sl (source.getLock());
  132. for (int i = 0; i < source.properties.size(); ++i)
  133. setValue (source.properties.getAllKeys() [i],
  134. source.properties.getAllValues() [i]);
  135. }
  136. void PropertySet::setFallbackPropertySet (PropertySet* fallbackProperties_) noexcept
  137. {
  138. const ScopedLock sl (lock);
  139. fallbackProperties = fallbackProperties_;
  140. }
  141. XmlElement* PropertySet::createXml (const String& nodeName) const
  142. {
  143. const ScopedLock sl (lock);
  144. XmlElement* const xml = new XmlElement (nodeName);
  145. for (int i = 0; i < properties.getAllKeys().size(); ++i)
  146. {
  147. XmlElement* const e = xml->createNewChildElement ("VALUE");
  148. e->setAttribute ("name", properties.getAllKeys()[i]);
  149. e->setAttribute ("val", properties.getAllValues()[i]);
  150. }
  151. return xml;
  152. }
  153. void PropertySet::restoreFromXml (const XmlElement& xml)
  154. {
  155. const ScopedLock sl (lock);
  156. clear();
  157. forEachXmlChildElementWithTagName (xml, e, "VALUE")
  158. {
  159. if (e->hasAttribute ("name")
  160. && e->hasAttribute ("val"))
  161. {
  162. properties.set (e->getStringAttribute ("name"),
  163. e->getStringAttribute ("val"));
  164. }
  165. }
  166. if (properties.size() > 0)
  167. propertyChanged();
  168. }
  169. void PropertySet::propertyChanged()
  170. {
  171. }
  172. } // namespace juce