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.

202 lines
8.8KB

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