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.

206 lines
8.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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. //==============================================================================
  20. /**
  21. A set of named property values, which can be strings, integers, floating point, etc.
  22. Effectively, this just wraps a StringPairArray in an interface that makes it easier
  23. to load and save types other than strings.
  24. See the PropertiesFile class for a subclass of this, which automatically broadcasts change
  25. messages and saves/loads the list from a file.
  26. @tags{Core}
  27. */
  28. class JUCE_API PropertySet
  29. {
  30. public:
  31. //==============================================================================
  32. /** Creates an empty PropertySet.
  33. @param ignoreCaseOfKeyNames if true, the names of properties are compared in a
  34. case-insensitive way
  35. */
  36. PropertySet (bool ignoreCaseOfKeyNames = false);
  37. /** Creates a copy of another PropertySet. */
  38. PropertySet (const PropertySet& other);
  39. /** Copies another PropertySet over this one. */
  40. PropertySet& operator= (const PropertySet& other);
  41. /** Destructor. */
  42. virtual ~PropertySet();
  43. //==============================================================================
  44. /** Returns one of the properties as a string.
  45. If the value isn't found in this set, then this will look for it in a fallback
  46. property set (if you've specified one with the setFallbackPropertySet() method),
  47. and if it can't find one there, it'll return the default value passed-in.
  48. @param keyName the name of the property to retrieve
  49. @param defaultReturnValue a value to return if the named property doesn't actually exist
  50. */
  51. String getValue (StringRef keyName, const String& defaultReturnValue = String()) const noexcept;
  52. /** Returns one of the properties as an integer.
  53. If the value isn't found in this set, then this will look for it in a fallback
  54. property set (if you've specified one with the setFallbackPropertySet() method),
  55. and if it can't find one there, it'll return the default value passed-in.
  56. @param keyName the name of the property to retrieve
  57. @param defaultReturnValue a value to return if the named property doesn't actually exist
  58. */
  59. int getIntValue (StringRef keyName, int defaultReturnValue = 0) const noexcept;
  60. /** Returns one of the properties as an double.
  61. If the value isn't found in this set, then this will look for it in a fallback
  62. property set (if you've specified one with the setFallbackPropertySet() method),
  63. and if it can't find one there, it'll return the default value passed-in.
  64. @param keyName the name of the property to retrieve
  65. @param defaultReturnValue a value to return if the named property doesn't actually exist
  66. */
  67. double getDoubleValue (StringRef keyName, double defaultReturnValue = 0.0) const noexcept;
  68. /** Returns one of the properties as an boolean.
  69. The result will be true if the string found for this key name can be parsed as a non-zero
  70. integer.
  71. If the value isn't found in this set, then this will look for it in a fallback
  72. property set (if you've specified one with the setFallbackPropertySet() method),
  73. and if it can't find one there, it'll return the default value passed-in.
  74. @param keyName the name of the property to retrieve
  75. @param defaultReturnValue a value to return if the named property doesn't actually exist
  76. */
  77. bool getBoolValue (StringRef keyName, bool defaultReturnValue = false) const noexcept;
  78. /** Returns one of the properties as an XML element.
  79. The result will a new XMLElement object. It may return nullptr if the key isn't found,
  80. or if the entry contains an string that isn't valid XML.
  81. If the value isn't found in this set, then this will look for it in a fallback
  82. property set (if you've specified one with the setFallbackPropertySet() method),
  83. and if it can't find one there, it'll return the default value passed-in.
  84. @param keyName the name of the property to retrieve
  85. */
  86. std::unique_ptr<XmlElement> getXmlValue (StringRef keyName) const;
  87. //==============================================================================
  88. /** Sets a named property.
  89. @param keyName the name of the property to set. (This mustn't be an empty string)
  90. @param value the new value to set it to
  91. */
  92. void setValue (StringRef keyName, const var& value);
  93. /** Sets a named property to an XML element.
  94. @param keyName the name of the property to set. (This mustn't be an empty string)
  95. @param xml the new element to set it to. If this is a nullptr, the value will
  96. be set to an empty string
  97. @see getXmlValue
  98. */
  99. void setValue (StringRef keyName, const XmlElement* xml);
  100. /** This copies all the values from a source PropertySet to this one.
  101. This won't remove any existing settings, it just adds any that it finds in the source set.
  102. */
  103. void addAllPropertiesFrom (const PropertySet& source);
  104. //==============================================================================
  105. /** Deletes a property.
  106. @param keyName the name of the property to delete. (This mustn't be an empty string)
  107. */
  108. void removeValue (StringRef keyName);
  109. /** Returns true if the properties include the given key. */
  110. bool containsKey (StringRef keyName) const noexcept;
  111. /** Removes all values. */
  112. void clear();
  113. //==============================================================================
  114. /** Returns the keys/value pair array containing all the properties. */
  115. StringPairArray& getAllProperties() noexcept { return properties; }
  116. /** Returns the lock used when reading or writing to this set */
  117. const CriticalSection& getLock() const noexcept { return lock; }
  118. //==============================================================================
  119. /** Returns an XML element which encapsulates all the items in this property set.
  120. The string parameter is the tag name that should be used for the node.
  121. @see restoreFromXml
  122. */
  123. std::unique_ptr<XmlElement> createXml (const String& nodeName) const;
  124. /** Reloads a set of properties that were previously stored as XML.
  125. The node passed in must have been created by the createXml() method.
  126. @see createXml
  127. */
  128. void restoreFromXml (const XmlElement& xml);
  129. //==============================================================================
  130. /** Sets up a second PopertySet that will be used to look up any values that aren't
  131. set in this one.
  132. If you set this up to be a pointer to a second property set, then whenever one
  133. of the getValue() methods fails to find an entry in this set, it will look up that
  134. value in the fallback set, and if it finds it, it will return that.
  135. Make sure that you don't delete the fallback set while it's still being used by
  136. another set! To remove the fallback set, just call this method with a null pointer.
  137. @see getFallbackPropertySet
  138. */
  139. void setFallbackPropertySet (PropertySet* fallbackProperties) noexcept;
  140. /** Returns the fallback property set.
  141. @see setFallbackPropertySet
  142. */
  143. PropertySet* getFallbackPropertySet() const noexcept { return fallbackProperties; }
  144. protected:
  145. /** Subclasses can override this to be told when one of the properties has been changed. */
  146. virtual void propertyChanged();
  147. private:
  148. StringPairArray properties;
  149. PropertySet* fallbackProperties;
  150. CriticalSection lock;
  151. bool ignoreCaseOfKeys;
  152. JUCE_LEAK_DETECTOR (PropertySet)
  153. };
  154. } // namespace juce