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.

182 lines
6.7KB

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