Audio plugin host https://kx.studio/carla
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.

NamedValueSet.h 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. ==============================================================================
  3. This file is part of the Water library.
  4. Copyright (c) 2016 ROLI Ltd.
  5. Copyright (C) 2017-2018 Filipe Coelho <falktx@falktx.com>
  6. Permission is granted to use this software under the terms of the ISC license
  7. http://www.isc.org/downloads/software-support-policy/isc-license/
  8. Permission to use, copy, modify, and/or distribute this software for any
  9. purpose with or without fee is hereby granted, provided that the above
  10. copyright notice and this permission notice appear in all copies.
  11. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  12. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  13. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  14. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  15. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  16. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  17. OF THIS SOFTWARE.
  18. ==============================================================================
  19. */
  20. #ifndef WATER_NAMEDVALUESET_H_INCLUDED
  21. #define WATER_NAMEDVALUESET_H_INCLUDED
  22. #include "Array.h"
  23. #include "Variant.h"
  24. #include "../text/Identifier.h"
  25. namespace water {
  26. //==============================================================================
  27. /** Holds a set of named var objects.
  28. This can be used as a basic structure to hold a set of var object, which can
  29. be retrieved by using their identifier.
  30. */
  31. class NamedValueSet
  32. {
  33. public:
  34. /** Creates an empty set. */
  35. NamedValueSet() noexcept;
  36. /** Creates a copy of another set. */
  37. NamedValueSet (const NamedValueSet&);
  38. /** Replaces this set with a copy of another set. */
  39. NamedValueSet& operator= (const NamedValueSet&);
  40. #if WATER_COMPILER_SUPPORTS_MOVE_SEMANTICS
  41. NamedValueSet (NamedValueSet&&) noexcept;
  42. NamedValueSet& operator= (NamedValueSet&&) noexcept;
  43. #endif
  44. /** Destructor. */
  45. ~NamedValueSet() noexcept;
  46. bool operator== (const NamedValueSet&) const;
  47. bool operator!= (const NamedValueSet&) const;
  48. //==============================================================================
  49. struct NamedValue
  50. {
  51. NamedValue() noexcept : name(), value() {}
  52. NamedValue (const Identifier& n, const var& v) : name (n), value (v) {}
  53. NamedValue (const NamedValue& other) : name (other.name), value (other.value) {}
  54. #if WATER_COMPILER_SUPPORTS_MOVE_SEMANTICS
  55. NamedValue (NamedValue&& other) noexcept
  56. : name (static_cast<Identifier&&> (other.name)),
  57. value (static_cast<var&&> (other.value))
  58. {
  59. }
  60. NamedValue (Identifier&& n, var&& v) noexcept
  61. : name (static_cast<Identifier&&> (n)),
  62. value (static_cast<var&&> (v))
  63. {
  64. }
  65. NamedValue& operator= (NamedValue&& other) noexcept
  66. {
  67. name = static_cast<Identifier&&> (other.name);
  68. value = static_cast<var&&> (other.value);
  69. return *this;
  70. }
  71. #endif
  72. bool operator== (const NamedValue& other) const noexcept { return name == other.name && value == other.value; }
  73. bool operator!= (const NamedValue& other) const noexcept { return ! operator== (other); }
  74. Identifier name;
  75. var value;
  76. };
  77. NamedValueSet::NamedValue* begin() { return values.begin(); }
  78. NamedValueSet::NamedValue* end() { return values.end(); }
  79. //==============================================================================
  80. /** Returns the total number of values that the set contains. */
  81. int size() const noexcept;
  82. /** Returns true if the set is empty. */
  83. bool isEmpty() const noexcept;
  84. /** Returns the value of a named item.
  85. If the name isn't found, this will return a void variant.
  86. @see getProperty
  87. */
  88. const var& operator[] (const Identifier& name) const noexcept;
  89. /** Tries to return the named value, but if no such value is found, this will
  90. instead return the supplied default value.
  91. */
  92. var getWithDefault (const Identifier& name, const var& defaultReturnValue) const;
  93. /** Changes or adds a named value.
  94. @returns true if a value was changed or added; false if the
  95. value was already set the value passed-in.
  96. */
  97. bool set (const Identifier& name, const var& newValue);
  98. #if WATER_COMPILER_SUPPORTS_MOVE_SEMANTICS
  99. /** Changes or adds a named value.
  100. @returns true if a value was changed or added; false if the
  101. value was already set the value passed-in.
  102. */
  103. bool set (const Identifier& name, var&& newValue);
  104. #endif
  105. /** Returns true if the set contains an item with the specified name. */
  106. bool contains (const Identifier& name) const noexcept;
  107. /** Removes a value from the set.
  108. @returns true if a value was removed; false if there was no value
  109. with the name that was given.
  110. */
  111. bool remove (const Identifier& name);
  112. /** Returns the name of the value at a given index.
  113. The index must be between 0 and size() - 1.
  114. */
  115. Identifier getName (int index) const noexcept;
  116. /** Returns a pointer to the var that holds a named value, or null if there is
  117. no value with this name.
  118. Do not use this method unless you really need access to the internal var object
  119. for some reason - for normal reading and writing always prefer operator[]() and set().
  120. */
  121. var* getVarPointer (const Identifier& name) const noexcept;
  122. /** Returns the value of the item at a given index.
  123. The index must be between 0 and size() - 1.
  124. */
  125. const var& getValueAt (int index) const noexcept;
  126. /** Returns the value of the item at a given index.
  127. The index must be between 0 and size() - 1, or this will return a nullptr
  128. */
  129. var* getVarPointerAt (int index) const noexcept;
  130. /** Returns the index of the given name, or -1 if it's not found. */
  131. int indexOf (const Identifier& name) const noexcept;
  132. /** Removes all values. */
  133. void clear();
  134. private:
  135. //==============================================================================
  136. Array<NamedValue> values;
  137. };
  138. }
  139. #endif // WATER_NAMEDVALUESET_H_INCLUDED