juce_NamedValueSet.h 5.5 KB

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