juce_PropertySet.h 9.4 KB

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