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.

216 lines
5.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. #include "NamedValueSet.h"
  24. namespace water {
  25. //==============================================================================
  26. NamedValueSet::NamedValueSet() noexcept
  27. {
  28. }
  29. NamedValueSet::NamedValueSet (const NamedValueSet& other)
  30. : values (other.values)
  31. {
  32. }
  33. NamedValueSet& NamedValueSet::operator= (const NamedValueSet& other)
  34. {
  35. clear();
  36. values = other.values;
  37. return *this;
  38. }
  39. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  40. NamedValueSet::NamedValueSet (NamedValueSet&& other) noexcept
  41. : values (static_cast<Array<NamedValue>&&> (other.values))
  42. {
  43. }
  44. NamedValueSet& NamedValueSet::operator= (NamedValueSet&& other) noexcept
  45. {
  46. other.values.swapWith (values);
  47. return *this;
  48. }
  49. #endif
  50. NamedValueSet::~NamedValueSet() noexcept
  51. {
  52. }
  53. void NamedValueSet::clear()
  54. {
  55. values.clear();
  56. }
  57. bool NamedValueSet::operator== (const NamedValueSet& other) const
  58. {
  59. return values == other.values;
  60. }
  61. bool NamedValueSet::operator!= (const NamedValueSet& other) const
  62. {
  63. return ! operator== (other);
  64. }
  65. int NamedValueSet::size() const noexcept
  66. {
  67. return values.size();
  68. }
  69. bool NamedValueSet::isEmpty() const noexcept
  70. {
  71. return values.isEmpty();
  72. }
  73. static const var& getNullVarRef() noexcept
  74. {
  75. static var nullVar;
  76. return nullVar;
  77. }
  78. const var& NamedValueSet::operator[] (const Identifier& name) const noexcept
  79. {
  80. if (const var* v = getVarPointer (name))
  81. return *v;
  82. return getNullVarRef();
  83. }
  84. var NamedValueSet::getWithDefault (const Identifier& name, const var& defaultReturnValue) const
  85. {
  86. if (const var* const v = getVarPointer (name))
  87. return *v;
  88. return defaultReturnValue;
  89. }
  90. var* NamedValueSet::getVarPointer (const Identifier& name) const noexcept
  91. {
  92. for (NamedValue* e = values.end(), *i = values.begin(); i != e; ++i)
  93. if (i->name == name)
  94. return &(i->value);
  95. return nullptr;
  96. }
  97. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  98. bool NamedValueSet::set (const Identifier& name, var&& newValue)
  99. {
  100. if (var* const v = getVarPointer (name))
  101. {
  102. if (v->equalsWithSameType (newValue))
  103. return false;
  104. *v = static_cast<var&&> (newValue);
  105. return true;
  106. }
  107. values.add (NamedValue (name, static_cast<var&&> (newValue)));
  108. return true;
  109. }
  110. #endif
  111. bool NamedValueSet::set (const Identifier& name, const var& newValue)
  112. {
  113. if (var* const v = getVarPointer (name))
  114. {
  115. if (v->equalsWithSameType (newValue))
  116. return false;
  117. *v = newValue;
  118. return true;
  119. }
  120. values.add (NamedValue (name, newValue));
  121. return true;
  122. }
  123. bool NamedValueSet::contains (const Identifier& name) const noexcept
  124. {
  125. return getVarPointer (name) != nullptr;
  126. }
  127. int NamedValueSet::indexOf (const Identifier& name) const noexcept
  128. {
  129. const int numValues = values.size();
  130. for (int i = 0; i < numValues; ++i)
  131. if (values.getReference(i).name == name)
  132. return i;
  133. return -1;
  134. }
  135. bool NamedValueSet::remove (const Identifier& name)
  136. {
  137. const int numValues = values.size();
  138. for (int i = 0; i < numValues; ++i)
  139. {
  140. if (values.getReference(i).name == name)
  141. {
  142. values.remove (i);
  143. return true;
  144. }
  145. }
  146. return false;
  147. }
  148. Identifier NamedValueSet::getName (const int index) const noexcept
  149. {
  150. if (isPositiveAndBelow (index, values.size()))
  151. return values.getReference (index).name;
  152. jassertfalse;
  153. return Identifier();
  154. }
  155. const var& NamedValueSet::getValueAt (const int index) const noexcept
  156. {
  157. if (isPositiveAndBelow (index, values.size()))
  158. return values.getReference (index).value;
  159. jassertfalse;
  160. return getNullVarRef();
  161. }
  162. var* NamedValueSet::getVarPointerAt (int index) const noexcept
  163. {
  164. if (isPositiveAndBelow (index, values.size()))
  165. return &(values.getReference (index).value);
  166. return nullptr;
  167. }
  168. }