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.

227 lines
9.6KB

  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. */
  42. PropertySet (const PropertySet& other);
  43. /** Copies another PropertySet over this one.
  44. */
  45. PropertySet& operator= (const PropertySet& other);
  46. /** Destructor. */
  47. virtual ~PropertySet();
  48. //==============================================================================
  49. /** Returns one of the properties as a string.
  50. If the value isn't found in this set, then this will look for it in a fallback
  51. property set (if you've specified one with the setFallbackPropertySet() method),
  52. and if it can't find one there, it'll return the default value passed-in.
  53. @param keyName the name of the property to retrieve
  54. @param defaultReturnValue a value to return if the named property doesn't actually exist
  55. */
  56. String getValue (const String& keyName,
  57. const String& defaultReturnValue = String::empty) const noexcept;
  58. /** Returns one of the properties as an integer.
  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. int getIntValue (const String& keyName,
  66. const int defaultReturnValue = 0) const noexcept;
  67. /** Returns one of the properties as an double.
  68. If the value isn't found in this set, then this will look for it in a fallback
  69. property set (if you've specified one with the setFallbackPropertySet() method),
  70. and if it can't find one there, it'll return the default value passed-in.
  71. @param keyName the name of the property to retrieve
  72. @param defaultReturnValue a value to return if the named property doesn't actually exist
  73. */
  74. double getDoubleValue (const String& keyName,
  75. const double defaultReturnValue = 0.0) const noexcept;
  76. /** Returns one of the properties as an boolean.
  77. The result will be true if the string found for this key name can be parsed as a non-zero
  78. integer.
  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. @param defaultReturnValue a value to return if the named property doesn't actually exist
  84. */
  85. bool getBoolValue (const String& keyName,
  86. const bool defaultReturnValue = false) const noexcept;
  87. /** Returns one of the properties as an XML element.
  88. The result will a new XMLElement object that the caller must delete. If may return 0 if the
  89. key isn't found, or if the entry contains an string that isn't valid XML.
  90. If the value isn't found in this set, then this will look for it in a fallback
  91. property set (if you've specified one with the setFallbackPropertySet() method),
  92. and if it can't find one there, it'll return the default value passed-in.
  93. @param keyName the name of the property to retrieve
  94. */
  95. XmlElement* getXmlValue (const String& keyName) const;
  96. //==============================================================================
  97. /** Sets a named property.
  98. @param keyName the name of the property to set. (This mustn't be an empty string)
  99. @param value the new value to set it to
  100. */
  101. void setValue (const String& keyName, const var& value);
  102. /** Sets a named property to an XML element.
  103. @param keyName the name of the property to set. (This mustn't be an empty string)
  104. @param xml the new element to set it to. If this is zero, the value will be set to
  105. an empty string
  106. @see getXmlValue
  107. */
  108. void setValue (const String& keyName, const XmlElement* xml);
  109. /** This copies all the values from a source PropertySet to this one.
  110. This won't remove any existing settings, it just adds any that it finds in the source set.
  111. */
  112. void addAllPropertiesFrom (const PropertySet& source);
  113. //==============================================================================
  114. /** Deletes a property.
  115. @param keyName the name of the property to delete. (This mustn't be an empty string)
  116. */
  117. void removeValue (const String& keyName);
  118. /** Returns true if the properies include the given key. */
  119. bool containsKey (const String& keyName) const noexcept;
  120. /** Removes all values. */
  121. void clear();
  122. //==============================================================================
  123. /** Returns the keys/value pair array containing all the properties. */
  124. StringPairArray& getAllProperties() noexcept { return properties; }
  125. /** Returns the lock used when reading or writing to this set */
  126. const CriticalSection& getLock() const noexcept { return lock; }
  127. //==============================================================================
  128. /** Returns an XML element which encapsulates all the items in this property set.
  129. The string parameter is the tag name that should be used for the node.
  130. @see restoreFromXml
  131. */
  132. XmlElement* createXml (const String& nodeName) const;
  133. /** Reloads a set of properties that were previously stored as XML.
  134. The node passed in must have been created by the createXml() method.
  135. @see createXml
  136. */
  137. void restoreFromXml (const XmlElement& xml);
  138. //==============================================================================
  139. /** Sets up a second PopertySet that will be used to look up any values that aren't
  140. set in this one.
  141. If you set this up to be a pointer to a second property set, then whenever one
  142. of the getValue() methods fails to find an entry in this set, it will look up that
  143. value in the fallback set, and if it finds it, it will return that.
  144. Make sure that you don't delete the fallback set while it's still being used by
  145. another set! To remove the fallback set, just call this method with a null pointer.
  146. @see getFallbackPropertySet
  147. */
  148. void setFallbackPropertySet (PropertySet* fallbackProperties) noexcept;
  149. /** Returns the fallback property set.
  150. @see setFallbackPropertySet
  151. */
  152. PropertySet* getFallbackPropertySet() const noexcept { return fallbackProperties; }
  153. protected:
  154. //==============================================================================
  155. /** Subclasses can override this to be told when one of the properies has been changed. */
  156. virtual void propertyChanged();
  157. private:
  158. //==============================================================================
  159. StringPairArray properties;
  160. PropertySet* fallbackProperties;
  161. CriticalSection lock;
  162. bool ignoreCaseOfKeys;
  163. JUCE_LEAK_DETECTOR (PropertySet);
  164. };
  165. #endif // __JUCE_PROPERTYSET_JUCEHEADER__