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.

252 lines
6.5KB

  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. //==============================================================================
  24. NamedValueSet::NamedValueSet() noexcept
  25. {
  26. }
  27. NamedValueSet::NamedValueSet (const NamedValueSet& other)
  28. : values (other.values)
  29. {
  30. }
  31. NamedValueSet& NamedValueSet::operator= (const NamedValueSet& other)
  32. {
  33. clear();
  34. values = other.values;
  35. return *this;
  36. }
  37. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  38. NamedValueSet::NamedValueSet (NamedValueSet&& other) noexcept
  39. : values (static_cast<Array<NamedValue>&&> (other.values))
  40. {
  41. }
  42. NamedValueSet& NamedValueSet::operator= (NamedValueSet&& other) noexcept
  43. {
  44. other.values.swapWith (values);
  45. return *this;
  46. }
  47. #endif
  48. NamedValueSet::~NamedValueSet() noexcept
  49. {
  50. }
  51. void NamedValueSet::clear()
  52. {
  53. values.clear();
  54. }
  55. bool NamedValueSet::operator== (const NamedValueSet& other) const
  56. {
  57. return values == other.values;
  58. }
  59. bool NamedValueSet::operator!= (const NamedValueSet& other) const
  60. {
  61. return ! operator== (other);
  62. }
  63. int NamedValueSet::size() const noexcept
  64. {
  65. return values.size();
  66. }
  67. bool NamedValueSet::isEmpty() const noexcept
  68. {
  69. return values.isEmpty();
  70. }
  71. static const var& getNullVarRef() noexcept
  72. {
  73. static var nullVar;
  74. return nullVar;
  75. }
  76. const var& NamedValueSet::operator[] (const Identifier& name) const noexcept
  77. {
  78. if (const var* v = getVarPointer (name))
  79. return *v;
  80. return getNullVarRef();
  81. }
  82. var NamedValueSet::getWithDefault (const Identifier& name, const var& defaultReturnValue) const
  83. {
  84. if (const var* const v = getVarPointer (name))
  85. return *v;
  86. return defaultReturnValue;
  87. }
  88. var* NamedValueSet::getVarPointer (const Identifier& name) const noexcept
  89. {
  90. for (NamedValue* e = values.end(), *i = values.begin(); i != e; ++i)
  91. if (i->name == name)
  92. return &(i->value);
  93. return nullptr;
  94. }
  95. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  96. bool NamedValueSet::set (const Identifier& name, var&& newValue)
  97. {
  98. if (var* const v = getVarPointer (name))
  99. {
  100. if (v->equalsWithSameType (newValue))
  101. return false;
  102. *v = static_cast<var&&> (newValue);
  103. return true;
  104. }
  105. values.add (NamedValue (name, static_cast<var&&> (newValue)));
  106. return true;
  107. }
  108. #endif
  109. bool NamedValueSet::set (const Identifier& name, const var& newValue)
  110. {
  111. if (var* const v = getVarPointer (name))
  112. {
  113. if (v->equalsWithSameType (newValue))
  114. return false;
  115. *v = newValue;
  116. return true;
  117. }
  118. values.add (NamedValue (name, newValue));
  119. return true;
  120. }
  121. bool NamedValueSet::contains (const Identifier& name) const noexcept
  122. {
  123. return getVarPointer (name) != nullptr;
  124. }
  125. int NamedValueSet::indexOf (const Identifier& name) const noexcept
  126. {
  127. const int numValues = values.size();
  128. for (int i = 0; i < numValues; ++i)
  129. if (values.getReference(i).name == name)
  130. return i;
  131. return -1;
  132. }
  133. bool NamedValueSet::remove (const Identifier& name)
  134. {
  135. const int numValues = values.size();
  136. for (int i = 0; i < numValues; ++i)
  137. {
  138. if (values.getReference(i).name == name)
  139. {
  140. values.remove (i);
  141. return true;
  142. }
  143. }
  144. return false;
  145. }
  146. Identifier NamedValueSet::getName (const int index) const noexcept
  147. {
  148. if (isPositiveAndBelow (index, values.size()))
  149. return values.getReference (index).name;
  150. jassertfalse;
  151. return Identifier();
  152. }
  153. const var& NamedValueSet::getValueAt (const int index) const noexcept
  154. {
  155. if (isPositiveAndBelow (index, values.size()))
  156. return values.getReference (index).value;
  157. jassertfalse;
  158. return getNullVarRef();
  159. }
  160. var* NamedValueSet::getVarPointerAt (int index) const noexcept
  161. {
  162. if (isPositiveAndBelow (index, values.size()))
  163. return &(values.getReference (index).value);
  164. return nullptr;
  165. }
  166. void NamedValueSet::setFromXmlAttributes (const XmlElement& xml)
  167. {
  168. values.clearQuick();
  169. for (const XmlElement::XmlAttributeNode* att = xml.attributes; att != nullptr; att = att->nextListItem)
  170. {
  171. if (att->name.toString().startsWith ("base64:"))
  172. {
  173. MemoryBlock mb;
  174. if (mb.fromBase64Encoding (att->value))
  175. {
  176. values.add (NamedValue (att->name.toString().substring (7), var (mb)));
  177. continue;
  178. }
  179. }
  180. values.add (NamedValue (att->name, var (att->value)));
  181. }
  182. }
  183. void NamedValueSet::copyToXmlAttributes (XmlElement& xml) const
  184. {
  185. for (NamedValue* e = values.end(), *i = values.begin(); i != e; ++i)
  186. {
  187. if (const MemoryBlock* mb = i->value.getBinaryData())
  188. {
  189. xml.setAttribute ("base64:" + i->name.toString(), mb->toBase64Encoding());
  190. }
  191. else
  192. {
  193. // These types can't be stored as XML!
  194. jassert (! i->value.isObject());
  195. jassert (! i->value.isMethod());
  196. jassert (! i->value.isArray());
  197. xml.setAttribute (i->name.toString(),
  198. i->value.toString());
  199. }
  200. }
  201. }