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.

167 lines
5.9KB

  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_NAMEDVALUESET_JUCEHEADER__
  19. #define __JUCE_NAMEDVALUESET_JUCEHEADER__
  20. #include "juce_Variant.h"
  21. #include "../containers/juce_LinkedListPointer.h"
  22. class XmlElement;
  23. #ifndef DOXYGEN
  24. class JSONFormatter;
  25. #endif
  26. //==============================================================================
  27. /** Holds a set of named var objects.
  28. This can be used as a basic structure to hold a set of var object, which can
  29. be retrieved by using their identifier.
  30. */
  31. class JUCE_API NamedValueSet
  32. {
  33. public:
  34. /** Creates an empty set. */
  35. NamedValueSet() noexcept;
  36. /** Creates a copy of another set. */
  37. NamedValueSet (const NamedValueSet& other);
  38. /** Replaces this set with a copy of another set. */
  39. NamedValueSet& operator= (const NamedValueSet& other);
  40. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  41. NamedValueSet (NamedValueSet&& other) noexcept;
  42. NamedValueSet& operator= (NamedValueSet&& other) noexcept;
  43. #endif
  44. /** Destructor. */
  45. ~NamedValueSet();
  46. bool operator== (const NamedValueSet& other) const;
  47. bool operator!= (const NamedValueSet& other) const;
  48. //==============================================================================
  49. /** Returns the total number of values that the set contains. */
  50. int size() const noexcept;
  51. /** Returns the value of a named item.
  52. If the name isn't found, this will return a void variant.
  53. @see getProperty
  54. */
  55. const var& operator[] (const Identifier& name) const;
  56. /** Tries to return the named value, but if no such value is found, this will
  57. instead return the supplied default value.
  58. */
  59. var getWithDefault (const Identifier& name, const var& defaultReturnValue) const;
  60. /** Changes or adds a named value.
  61. @returns true if a value was changed or added; false if the
  62. value was already set the the value passed-in.
  63. */
  64. bool set (const Identifier& name, const var& newValue);
  65. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  66. /** Changes or adds a named value.
  67. @returns true if a value was changed or added; false if the
  68. value was already set the the value passed-in.
  69. */
  70. bool set (const Identifier& name, var&& newValue);
  71. #endif
  72. /** Returns true if the set contains an item with the specified name. */
  73. bool contains (const Identifier& name) const;
  74. /** Removes a value from the set.
  75. @returns true if a value was removed; false if there was no value
  76. with the name that was given.
  77. */
  78. bool remove (const Identifier& name);
  79. /** Returns the name of the value at a given index.
  80. The index must be between 0 and size() - 1.
  81. */
  82. const Identifier getName (int index) const;
  83. /** Returns the value of the item at a given index.
  84. The index must be between 0 and size() - 1.
  85. */
  86. const var& getValueAt (int index) const;
  87. /** Removes all values. */
  88. void clear();
  89. //==============================================================================
  90. /** Returns a pointer to the var that holds a named value, or null if there is
  91. no value with this name.
  92. Do not use this method unless you really need access to the internal var object
  93. for some reason - for normal reading and writing always prefer operator[]() and set().
  94. */
  95. var* getVarPointer (const Identifier& name) const noexcept;
  96. //==============================================================================
  97. /** Sets properties to the values of all of an XML element's attributes. */
  98. void setFromXmlAttributes (const XmlElement& xml);
  99. /** Sets attributes in an XML element corresponding to each of this object's
  100. properties.
  101. */
  102. void copyToXmlAttributes (XmlElement& xml) const;
  103. private:
  104. //==============================================================================
  105. class NamedValue
  106. {
  107. public:
  108. NamedValue() noexcept;
  109. NamedValue (const NamedValue&);
  110. NamedValue (const Identifier& name, const var& value);
  111. NamedValue& operator= (const NamedValue&);
  112. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  113. NamedValue (NamedValue&&) noexcept;
  114. NamedValue (const Identifier& name, var&& value);
  115. NamedValue& operator= (NamedValue&&) noexcept;
  116. #endif
  117. bool operator== (const NamedValue& other) const noexcept;
  118. LinkedListPointer<NamedValue> nextListItem;
  119. Identifier name;
  120. var value;
  121. private:
  122. JUCE_LEAK_DETECTOR (NamedValue);
  123. };
  124. friend class LinkedListPointer<NamedValue>;
  125. LinkedListPointer<NamedValue> values;
  126. friend class JSONFormatter;
  127. };
  128. #endif // __JUCE_NAMEDVALUESET_JUCEHEADER__