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.

170 lines
6.2KB

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