Audio plugin host https://kx.studio/carla
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.

189 lines
6.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. #ifndef JUCE_NAMEDVALUESET_H_INCLUDED
  24. #define JUCE_NAMEDVALUESET_H_INCLUDED
  25. //==============================================================================
  26. /** Holds a set of named var objects.
  27. This can be used as a basic structure to hold a set of var object, which can
  28. be retrieved by using their identifier.
  29. */
  30. class JUCE_API NamedValueSet
  31. {
  32. public:
  33. /** Creates an empty set. */
  34. NamedValueSet() noexcept;
  35. /** Creates a copy of another set. */
  36. NamedValueSet (const NamedValueSet&);
  37. /** Replaces this set with a copy of another set. */
  38. NamedValueSet& operator= (const NamedValueSet&);
  39. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  40. NamedValueSet (NamedValueSet&&) noexcept;
  41. NamedValueSet& operator= (NamedValueSet&&) noexcept;
  42. #endif
  43. /** Destructor. */
  44. ~NamedValueSet() noexcept;
  45. bool operator== (const NamedValueSet&) const;
  46. bool operator!= (const NamedValueSet&) const;
  47. //==============================================================================
  48. struct NamedValue
  49. {
  50. NamedValue() noexcept {}
  51. NamedValue (const Identifier& n, const var& v) : name (n), value (v) {}
  52. NamedValue (const NamedValue& other) : name (other.name), value (other.value) {}
  53. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  54. NamedValue (NamedValue&& other) noexcept
  55. : name (static_cast<Identifier&&> (other.name)),
  56. value (static_cast<var&&> (other.value))
  57. {
  58. }
  59. NamedValue (Identifier&& n, var&& v) noexcept
  60. : name (static_cast<Identifier&&> (n)),
  61. value (static_cast<var&&> (v))
  62. {
  63. }
  64. NamedValue& operator= (NamedValue&& other) noexcept
  65. {
  66. name = static_cast<Identifier&&> (other.name);
  67. value = static_cast<var&&> (other.value);
  68. return *this;
  69. }
  70. #endif
  71. bool operator== (const NamedValue& other) const noexcept { return name == other.name && value == other.value; }
  72. bool operator!= (const NamedValue& other) const noexcept { return ! operator== (other); }
  73. Identifier name;
  74. var value;
  75. };
  76. NamedValueSet::NamedValue* begin() { return values.begin(); }
  77. NamedValueSet::NamedValue* end() { return values.end(); }
  78. //==============================================================================
  79. /** Returns the total number of values that the set contains. */
  80. int size() const noexcept;
  81. /** Returns true if the set is empty. */
  82. bool isEmpty() const noexcept;
  83. /** Returns the value of a named item.
  84. If the name isn't found, this will return a void variant.
  85. @see getProperty
  86. */
  87. const var& operator[] (const Identifier& name) const noexcept;
  88. /** Tries to return the named value, but if no such value is found, this will
  89. instead return the supplied default value.
  90. */
  91. var getWithDefault (const Identifier& name, const var& defaultReturnValue) const;
  92. /** Changes or adds a named value.
  93. @returns true if a value was changed or added; false if the
  94. value was already set the value passed-in.
  95. */
  96. bool set (const Identifier& name, const var& newValue);
  97. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  98. /** Changes or adds a named value.
  99. @returns true if a value was changed or added; false if the
  100. value was already set the value passed-in.
  101. */
  102. bool set (const Identifier& name, var&& newValue);
  103. #endif
  104. /** Returns true if the set contains an item with the specified name. */
  105. bool contains (const Identifier& name) const noexcept;
  106. /** Removes a value from the set.
  107. @returns true if a value was removed; false if there was no value
  108. with the name that was given.
  109. */
  110. bool remove (const Identifier& name);
  111. /** Returns the name of the value at a given index.
  112. The index must be between 0 and size() - 1.
  113. */
  114. Identifier getName (int index) const noexcept;
  115. /** Returns a pointer to the var that holds a named value, or null if there is
  116. no value with this name.
  117. Do not use this method unless you really need access to the internal var object
  118. for some reason - for normal reading and writing always prefer operator[]() and set().
  119. */
  120. var* getVarPointer (const Identifier& name) const noexcept;
  121. /** Returns the value of the item at a given index.
  122. The index must be between 0 and size() - 1.
  123. */
  124. const var& getValueAt (int index) const noexcept;
  125. /** Returns the value of the item at a given index.
  126. The index must be between 0 and size() - 1, or this will return a nullptr
  127. */
  128. 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. #endif // JUCE_NAMEDVALUESET_H_INCLUDED