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.

220 lines
9.7KB

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