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.

186 lines
7.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. //==============================================================================
  20. /** Holds a set of named var objects.
  21. This can be used as a basic structure to hold a set of var object, which can
  22. be retrieved by using their identifier.
  23. @tags{Core}
  24. */
  25. class JUCE_API NamedValueSet
  26. {
  27. public:
  28. //==============================================================================
  29. /** Structure for a named var object */
  30. struct JUCE_API NamedValue
  31. {
  32. NamedValue() noexcept;
  33. ~NamedValue() noexcept;
  34. NamedValue (const Identifier& name, const var& value);
  35. NamedValue (const Identifier& name, var&& value) noexcept;
  36. NamedValue (Identifier&& name, var&& value) noexcept;
  37. NamedValue (const NamedValue&);
  38. NamedValue (NamedValue&&) noexcept;
  39. NamedValue& operator= (NamedValue&&) noexcept;
  40. bool operator== (const NamedValue&) const noexcept;
  41. bool operator!= (const NamedValue&) const noexcept;
  42. Identifier name;
  43. var value;
  44. };
  45. //==============================================================================
  46. /** Creates an empty set. */
  47. NamedValueSet() noexcept;
  48. NamedValueSet (const NamedValueSet&);
  49. NamedValueSet (NamedValueSet&&) noexcept;
  50. NamedValueSet& operator= (const NamedValueSet&);
  51. NamedValueSet& operator= (NamedValueSet&&) noexcept;
  52. /** Creates a NamedValueSet from a list of names and properties. */
  53. NamedValueSet (std::initializer_list<NamedValue>);
  54. /** Destructor. */
  55. ~NamedValueSet() noexcept;
  56. /** Two NamedValueSets are considered equal if they contain all the same key/value
  57. pairs, regardless of the order.
  58. */
  59. bool operator== (const NamedValueSet&) const noexcept;
  60. bool operator!= (const NamedValueSet&) const noexcept;
  61. const NamedValueSet::NamedValue* begin() const noexcept { return values.begin(); }
  62. const NamedValueSet::NamedValue* end() const noexcept { return values.end(); }
  63. //==============================================================================
  64. /** Returns the total number of values that the set contains. */
  65. int size() const noexcept;
  66. /** Returns true if the set is empty. */
  67. bool isEmpty() const noexcept;
  68. /** Returns the value of a named item.
  69. If the name isn't found, this will return a void variant.
  70. */
  71. const var& operator[] (const Identifier& name) const noexcept;
  72. /** Tries to return the named value, but if no such value is found, this will
  73. instead return the supplied default value.
  74. */
  75. var getWithDefault (const Identifier& name, const var& defaultReturnValue) const;
  76. /** Changes or adds a named value.
  77. @returns true if a value was changed or added; false if the
  78. value was already set the value passed-in.
  79. */
  80. bool set (const Identifier& name, const var& newValue);
  81. /** Changes or adds a named value.
  82. @returns true if a value was changed or added; false if the
  83. value was already set the value passed-in.
  84. */
  85. bool set (const Identifier& name, var&& newValue);
  86. /** Returns true if the set contains an item with the specified name. */
  87. bool contains (const Identifier& name) const noexcept;
  88. /** Removes a value from the set.
  89. @returns true if a value was removed; false if there was no value
  90. with the name that was given.
  91. */
  92. bool remove (const Identifier& name);
  93. /** Returns the name of the value at a given index.
  94. The index must be between 0 and size() - 1.
  95. */
  96. Identifier getName (int index) const noexcept;
  97. /** Returns a pointer to the var that holds a named value, or null if there is
  98. no value with this name.
  99. Do not use this method unless you really need access to the internal var object
  100. for some reason - for normal reading and writing always prefer operator[]() and set().
  101. Also note that the pointer returned may become invalid as soon as any subsequent
  102. methods are called on the NamedValueSet.
  103. */
  104. var* getVarPointer (const Identifier& name) noexcept;
  105. /** Returns a pointer to the var that holds a named value, or null if there is
  106. no value with this name.
  107. Do not use this method unless you really need access to the internal var object
  108. for some reason - for normal reading and writing always prefer operator[]() and set().
  109. Also note that the pointer returned may become invalid as soon as any subsequent
  110. methods are called on the NamedValueSet.
  111. */
  112. const var* getVarPointer (const Identifier& name) const noexcept;
  113. /** Returns the value of the item at a given index.
  114. The index must be between 0 and size() - 1.
  115. */
  116. const var& getValueAt (int index) const noexcept;
  117. /** Returns the value of the item at a given index.
  118. The index must be between 0 and size() - 1, or this will return a nullptr
  119. Also note that the pointer returned may become invalid as soon as any subsequent
  120. methods are called on the NamedValueSet.
  121. */
  122. var* getVarPointerAt (int index) noexcept;
  123. /** Returns the value of the item at a given index.
  124. The index must be between 0 and size() - 1, or this will return a nullptr
  125. Also note that the pointer returned may become invalid as soon as any subsequent
  126. methods are called on the NamedValueSet.
  127. */
  128. const var* getVarPointerAt (int index) const noexcept;
  129. /** Returns the index of the given name, or -1 if it's not found. */
  130. int indexOf (const Identifier& name) const noexcept;
  131. /** Removes all values. */
  132. void clear();
  133. //==============================================================================
  134. /** Sets properties to the values of all of an XML element's attributes. */
  135. void setFromXmlAttributes (const XmlElement& xml);
  136. /** Sets attributes in an XML element corresponding to each of this object's
  137. properties.
  138. */
  139. void copyToXmlAttributes (XmlElement& xml) const;
  140. private:
  141. //==============================================================================
  142. Array<NamedValue> values;
  143. };
  144. } // namespace juce