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.

217 lines
9.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_PROPERTYSET_JUCEHEADER__
  19. #define __JUCE_PROPERTYSET_JUCEHEADER__
  20. #include "../text/juce_StringPairArray.h"
  21. #include "../xml/juce_XmlElement.h"
  22. #include "../containers/juce_Variant.h"
  23. //==============================================================================
  24. /**
  25. A set of named property values, which can be strings, integers, floating point, etc.
  26. Effectively, this just wraps a StringPairArray in an interface that makes it easier
  27. to load and save types other than strings.
  28. See the PropertiesFile class for a subclass of this, which automatically broadcasts change
  29. messages and saves/loads the list from a file.
  30. */
  31. class JUCE_API PropertySet
  32. {
  33. public:
  34. //==============================================================================
  35. /** Creates an empty PropertySet.
  36. @param ignoreCaseOfKeyNames if true, the names of properties are compared in a
  37. case-insensitive way
  38. */
  39. PropertySet (bool ignoreCaseOfKeyNames = false);
  40. /** Creates a copy of another PropertySet. */
  41. PropertySet (const PropertySet& other);
  42. /** Copies another PropertySet over this one. */
  43. PropertySet& operator= (const PropertySet& other);
  44. /** Destructor. */
  45. virtual ~PropertySet();
  46. //==============================================================================
  47. /** Returns one of the properties as a string.
  48. If the value isn't found in this set, then this will look for it in a fallback
  49. property set (if you've specified one with the setFallbackPropertySet() method),
  50. and if it can't find one there, it'll return the default value passed-in.
  51. @param keyName the name of the property to retrieve
  52. @param defaultReturnValue a value to return if the named property doesn't actually exist
  53. */
  54. String getValue (const String& keyName,
  55. const String& defaultReturnValue = String::empty) const noexcept;
  56. /** Returns one of the properties as an integer.
  57. If the value isn't found in this set, then this will look for it in a fallback
  58. property set (if you've specified one with the setFallbackPropertySet() method),
  59. and if it can't find one there, it'll return the default value passed-in.
  60. @param keyName the name of the property to retrieve
  61. @param defaultReturnValue a value to return if the named property doesn't actually exist
  62. */
  63. int getIntValue (const String& keyName,
  64. const int defaultReturnValue = 0) const noexcept;
  65. /** Returns one of the properties as an double.
  66. If the value isn't found in this set, then this will look for it in a fallback
  67. property set (if you've specified one with the setFallbackPropertySet() method),
  68. and if it can't find one there, it'll return the default value passed-in.
  69. @param keyName the name of the property to retrieve
  70. @param defaultReturnValue a value to return if the named property doesn't actually exist
  71. */
  72. double getDoubleValue (const String& keyName,
  73. const double defaultReturnValue = 0.0) const noexcept;
  74. /** Returns one of the properties as an boolean.
  75. The result will be true if the string found for this key name can be parsed as a non-zero
  76. integer.
  77. If the value isn't found in this set, then this will look for it in a fallback
  78. property set (if you've specified one with the setFallbackPropertySet() method),
  79. and if it can't find one there, it'll return the default value passed-in.
  80. @param keyName the name of the property to retrieve
  81. @param defaultReturnValue a value to return if the named property doesn't actually exist
  82. */
  83. bool getBoolValue (const String& keyName,
  84. const bool defaultReturnValue = false) const noexcept;
  85. /** Returns one of the properties as an XML element.
  86. The result will a new XMLElement object that the caller must delete. If may return 0 if the
  87. key isn't found, or if the entry contains an string that isn't valid XML.
  88. If the value isn't found in this set, then this will look for it in a fallback
  89. property set (if you've specified one with the setFallbackPropertySet() method),
  90. and if it can't find one there, it'll return the default value passed-in.
  91. @param keyName the name of the property to retrieve
  92. */
  93. XmlElement* getXmlValue (const String& keyName) const;
  94. //==============================================================================
  95. /** Sets a named property.
  96. @param keyName the name of the property to set. (This mustn't be an empty string)
  97. @param value the new value to set it to
  98. */
  99. void setValue (const String& keyName, const var& value);
  100. /** Sets a named property to an XML element.
  101. @param keyName the name of the property to set. (This mustn't be an empty string)
  102. @param xml the new element to set it to. If this is zero, the value will be set to
  103. an empty string
  104. @see getXmlValue
  105. */
  106. void setValue (const String& keyName, const XmlElement* xml);
  107. /** This copies all the values from a source PropertySet to this one.
  108. This won't remove any existing settings, it just adds any that it finds in the source set.
  109. */
  110. void addAllPropertiesFrom (const PropertySet& source);
  111. //==============================================================================
  112. /** Deletes a property.
  113. @param keyName the name of the property to delete. (This mustn't be an empty string)
  114. */
  115. void removeValue (const String& keyName);
  116. /** Returns true if the properies include the given key. */
  117. bool containsKey (const String& keyName) const noexcept;
  118. /** Removes all values. */
  119. void clear();
  120. //==============================================================================
  121. /** Returns the keys/value pair array containing all the properties. */
  122. StringPairArray& getAllProperties() noexcept { return properties; }
  123. /** Returns the lock used when reading or writing to this set */
  124. const CriticalSection& getLock() const noexcept { return lock; }
  125. //==============================================================================
  126. /** Returns an XML element which encapsulates all the items in this property set.
  127. The string parameter is the tag name that should be used for the node.
  128. @see restoreFromXml
  129. */
  130. XmlElement* createXml (const String& nodeName) const;
  131. /** Reloads a set of properties that were previously stored as XML.
  132. The node passed in must have been created by the createXml() method.
  133. @see createXml
  134. */
  135. void restoreFromXml (const XmlElement& xml);
  136. //==============================================================================
  137. /** Sets up a second PopertySet that will be used to look up any values that aren't
  138. set in this one.
  139. If you set this up to be a pointer to a second property set, then whenever one
  140. of the getValue() methods fails to find an entry in this set, it will look up that
  141. value in the fallback set, and if it finds it, it will return that.
  142. Make sure that you don't delete the fallback set while it's still being used by
  143. another set! To remove the fallback set, just call this method with a null pointer.
  144. @see getFallbackPropertySet
  145. */
  146. void setFallbackPropertySet (PropertySet* fallbackProperties) noexcept;
  147. /** Returns the fallback property set.
  148. @see setFallbackPropertySet
  149. */
  150. PropertySet* getFallbackPropertySet() const noexcept { return fallbackProperties; }
  151. protected:
  152. /** Subclasses can override this to be told when one of the properies has been changed. */
  153. virtual void propertyChanged();
  154. private:
  155. StringPairArray properties;
  156. PropertySet* fallbackProperties;
  157. CriticalSection lock;
  158. bool ignoreCaseOfKeys;
  159. JUCE_LEAK_DETECTOR (PropertySet);
  160. };
  161. #endif // __JUCE_PROPERTYSET_JUCEHEADER__