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.

283 lines
7.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. NamedValueSet::NamedValue::NamedValue() noexcept {}
  20. NamedValueSet::NamedValue::~NamedValue() noexcept {}
  21. NamedValueSet::NamedValue::NamedValue (const Identifier& n, const var& v) : name (n), value (v) {}
  22. NamedValueSet::NamedValue::NamedValue (const NamedValue& other) : NamedValue (other.name, other.value) {}
  23. NamedValueSet::NamedValue::NamedValue (NamedValue&& other) noexcept
  24. : NamedValue (static_cast<Identifier&&> (other.name),
  25. static_cast<var&&> (other.value))
  26. {}
  27. NamedValueSet::NamedValue::NamedValue (const Identifier& n, var&& v) noexcept
  28. : name (n), value (static_cast<var&&> (v))
  29. {
  30. }
  31. NamedValueSet::NamedValue::NamedValue (Identifier&& n, var&& v) noexcept
  32. : name (static_cast<Identifier&&> (n)),
  33. value (static_cast<var&&> (v))
  34. {}
  35. NamedValueSet::NamedValue& NamedValueSet::NamedValue::operator= (NamedValue&& other) noexcept
  36. {
  37. name = static_cast<Identifier&&> (other.name);
  38. value = static_cast<var&&> (other.value);
  39. return *this;
  40. }
  41. bool NamedValueSet::NamedValue::operator== (const NamedValue& other) const noexcept { return name == other.name && value == other.value; }
  42. bool NamedValueSet::NamedValue::operator!= (const NamedValue& other) const noexcept { return ! operator== (other); }
  43. //==============================================================================
  44. NamedValueSet::NamedValueSet() noexcept {}
  45. NamedValueSet::~NamedValueSet() noexcept {}
  46. NamedValueSet::NamedValueSet (const NamedValueSet& other) : values (other.values) {}
  47. NamedValueSet::NamedValueSet (NamedValueSet&& other) noexcept : values (static_cast<Array<NamedValue>&&> (other.values)) {}
  48. NamedValueSet& NamedValueSet::operator= (const NamedValueSet& other)
  49. {
  50. clear();
  51. values = other.values;
  52. return *this;
  53. }
  54. NamedValueSet& NamedValueSet::operator= (NamedValueSet&& other) noexcept
  55. {
  56. other.values.swapWith (values);
  57. return *this;
  58. }
  59. void NamedValueSet::clear()
  60. {
  61. values.clear();
  62. }
  63. bool NamedValueSet::operator== (const NamedValueSet& other) const noexcept
  64. {
  65. auto num = values.size();
  66. if (num != other.values.size())
  67. return false;
  68. for (int i = 0; i < num; ++i)
  69. {
  70. // optimise for the case where the keys are in the same order
  71. if (values.getReference(i).name == other.values.getReference(i).name)
  72. {
  73. if (values.getReference(i).value != other.values.getReference(i).value)
  74. return false;
  75. }
  76. else
  77. {
  78. // if we encounter keys that are in a different order, search remaining items by brute force..
  79. for (int j = i; j < num; ++j)
  80. {
  81. if (auto* otherVal = other.getVarPointer (values.getReference(j).name))
  82. if (values.getReference(j).value == *otherVal)
  83. continue;
  84. return false;
  85. }
  86. return true;
  87. }
  88. }
  89. return true;
  90. }
  91. bool NamedValueSet::operator!= (const NamedValueSet& other) const noexcept { return ! operator== (other); }
  92. int NamedValueSet::size() const noexcept { return values.size(); }
  93. bool NamedValueSet::isEmpty() const noexcept { return values.isEmpty(); }
  94. static const var& getNullVarRef() noexcept
  95. {
  96. static var nullVar;
  97. return nullVar;
  98. }
  99. const var& NamedValueSet::operator[] (const Identifier& name) const noexcept
  100. {
  101. if (auto* v = getVarPointer (name))
  102. return *v;
  103. return getNullVarRef();
  104. }
  105. var NamedValueSet::getWithDefault (const Identifier& name, const var& defaultReturnValue) const
  106. {
  107. if (auto* v = getVarPointer (name))
  108. return *v;
  109. return defaultReturnValue;
  110. }
  111. var* NamedValueSet::getVarPointer (const Identifier& name) const noexcept
  112. {
  113. for (auto& i : values)
  114. if (i.name == name)
  115. return &(i.value);
  116. return {};
  117. }
  118. bool NamedValueSet::set (const Identifier& name, var&& newValue)
  119. {
  120. if (auto* v = getVarPointer (name))
  121. {
  122. if (v->equalsWithSameType (newValue))
  123. return false;
  124. *v = static_cast<var&&> (newValue);
  125. return true;
  126. }
  127. values.add ({ name, static_cast<var&&> (newValue) });
  128. return true;
  129. }
  130. bool NamedValueSet::set (const Identifier& name, const var& newValue)
  131. {
  132. if (auto* v = getVarPointer (name))
  133. {
  134. if (v->equalsWithSameType (newValue))
  135. return false;
  136. *v = newValue;
  137. return true;
  138. }
  139. values.add ({ name, newValue });
  140. return true;
  141. }
  142. bool NamedValueSet::contains (const Identifier& name) const noexcept
  143. {
  144. return getVarPointer (name) != nullptr;
  145. }
  146. int NamedValueSet::indexOf (const Identifier& name) const noexcept
  147. {
  148. auto numValues = values.size();
  149. for (int i = 0; i < numValues; ++i)
  150. if (values.getReference(i).name == name)
  151. return i;
  152. return -1;
  153. }
  154. bool NamedValueSet::remove (const Identifier& name)
  155. {
  156. auto numValues = values.size();
  157. for (int i = 0; i < numValues; ++i)
  158. {
  159. if (values.getReference(i).name == name)
  160. {
  161. values.remove (i);
  162. return true;
  163. }
  164. }
  165. return false;
  166. }
  167. Identifier NamedValueSet::getName (const int index) const noexcept
  168. {
  169. if (isPositiveAndBelow (index, values.size()))
  170. return values.getReference (index).name;
  171. jassertfalse;
  172. return {};
  173. }
  174. const var& NamedValueSet::getValueAt (const int index) const noexcept
  175. {
  176. if (isPositiveAndBelow (index, values.size()))
  177. return values.getReference (index).value;
  178. jassertfalse;
  179. return getNullVarRef();
  180. }
  181. var* NamedValueSet::getVarPointerAt (int index) const noexcept
  182. {
  183. if (isPositiveAndBelow (index, values.size()))
  184. return &(values.getReference (index).value);
  185. return {};
  186. }
  187. void NamedValueSet::setFromXmlAttributes (const XmlElement& xml)
  188. {
  189. values.clearQuick();
  190. for (auto* att = xml.attributes.get(); att != nullptr; att = att->nextListItem)
  191. {
  192. if (att->name.toString().startsWith ("base64:"))
  193. {
  194. MemoryBlock mb;
  195. if (mb.fromBase64Encoding (att->value))
  196. {
  197. values.add ({ att->name.toString().substring (7), var (mb) });
  198. continue;
  199. }
  200. }
  201. values.add ({ att->name, var (att->value) });
  202. }
  203. }
  204. void NamedValueSet::copyToXmlAttributes (XmlElement& xml) const
  205. {
  206. for (auto& i : values)
  207. {
  208. if (auto* mb = i.value.getBinaryData())
  209. {
  210. xml.setAttribute ("base64:" + i.name.toString(), mb->toBase64Encoding());
  211. }
  212. else
  213. {
  214. // These types can't be stored as XML!
  215. jassert (! i.value.isObject());
  216. jassert (! i.value.isMethod());
  217. jassert (! i.value.isArray());
  218. xml.setAttribute (i.name.toString(),
  219. i.value.toString());
  220. }
  221. }
  222. }
  223. } // namespace juce