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.

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