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.

174 lines
6.2KB

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