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.

256 lines
6.6KB

  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. #if JUCE_ALLOW_STATIC_NULL_VARIABLES
  74. return var::null;
  75. #else
  76. static var nullVar;
  77. return nullVar;
  78. #endif
  79. }
  80. const var& NamedValueSet::operator[] (const Identifier& name) const noexcept
  81. {
  82. if (const var* v = getVarPointer (name))
  83. return *v;
  84. return getNullVarRef();
  85. }
  86. var NamedValueSet::getWithDefault (const Identifier& name, const var& defaultReturnValue) const
  87. {
  88. if (const var* const v = getVarPointer (name))
  89. return *v;
  90. return defaultReturnValue;
  91. }
  92. var* NamedValueSet::getVarPointer (const Identifier& name) const noexcept
  93. {
  94. for (NamedValue* e = values.end(), *i = values.begin(); i != e; ++i)
  95. if (i->name == name)
  96. return &(i->value);
  97. return nullptr;
  98. }
  99. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  100. bool NamedValueSet::set (const Identifier& name, var&& newValue)
  101. {
  102. if (var* const v = getVarPointer (name))
  103. {
  104. if (v->equalsWithSameType (newValue))
  105. return false;
  106. *v = static_cast<var&&> (newValue);
  107. return true;
  108. }
  109. values.add (NamedValue (name, static_cast<var&&> (newValue)));
  110. return true;
  111. }
  112. #endif
  113. bool NamedValueSet::set (const Identifier& name, const var& newValue)
  114. {
  115. if (var* const v = getVarPointer (name))
  116. {
  117. if (v->equalsWithSameType (newValue))
  118. return false;
  119. *v = newValue;
  120. return true;
  121. }
  122. values.add (NamedValue (name, newValue));
  123. return true;
  124. }
  125. bool NamedValueSet::contains (const Identifier& name) const noexcept
  126. {
  127. return getVarPointer (name) != nullptr;
  128. }
  129. int NamedValueSet::indexOf (const Identifier& name) const noexcept
  130. {
  131. const int numValues = values.size();
  132. for (int i = 0; i < numValues; ++i)
  133. if (values.getReference(i).name == name)
  134. return i;
  135. return -1;
  136. }
  137. bool NamedValueSet::remove (const Identifier& name)
  138. {
  139. const int numValues = values.size();
  140. for (int i = 0; i < numValues; ++i)
  141. {
  142. if (values.getReference(i).name == name)
  143. {
  144. values.remove (i);
  145. return true;
  146. }
  147. }
  148. return false;
  149. }
  150. Identifier NamedValueSet::getName (const int index) const noexcept
  151. {
  152. if (isPositiveAndBelow (index, values.size()))
  153. return values.getReference (index).name;
  154. jassertfalse;
  155. return Identifier();
  156. }
  157. const var& NamedValueSet::getValueAt (const int index) const noexcept
  158. {
  159. if (isPositiveAndBelow (index, values.size()))
  160. return values.getReference (index).value;
  161. jassertfalse;
  162. return getNullVarRef();
  163. }
  164. var* NamedValueSet::getVarPointerAt (int index) const noexcept
  165. {
  166. if (isPositiveAndBelow (index, values.size()))
  167. return &(values.getReference (index).value);
  168. return nullptr;
  169. }
  170. void NamedValueSet::setFromXmlAttributes (const XmlElement& xml)
  171. {
  172. values.clearQuick();
  173. for (const XmlElement::XmlAttributeNode* att = xml.attributes; att != nullptr; att = att->nextListItem)
  174. {
  175. if (att->name.toString().startsWith ("base64:"))
  176. {
  177. MemoryBlock mb;
  178. if (mb.fromBase64Encoding (att->value))
  179. {
  180. values.add (NamedValue (att->name.toString().substring (7), var (mb)));
  181. continue;
  182. }
  183. }
  184. values.add (NamedValue (att->name, var (att->value)));
  185. }
  186. }
  187. void NamedValueSet::copyToXmlAttributes (XmlElement& xml) const
  188. {
  189. for (NamedValue* e = values.end(), *i = values.begin(); i != e; ++i)
  190. {
  191. if (const MemoryBlock* mb = i->value.getBinaryData())
  192. {
  193. xml.setAttribute ("base64:" + i->name.toString(), mb->toBase64Encoding());
  194. }
  195. else
  196. {
  197. // These types can't be stored as XML!
  198. jassert (! i->value.isObject());
  199. jassert (! i->value.isMethod());
  200. jassert (! i->value.isArray());
  201. xml.setAttribute (i->name.toString(),
  202. i->value.toString());
  203. }
  204. }
  205. }