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.

307 lines
8.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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 (std::move (other.name),
  25. std::move (other.value))
  26. {}
  27. NamedValueSet::NamedValue::NamedValue (const Identifier& n, var&& v) noexcept
  28. : name (n), value (std::move (v))
  29. {
  30. }
  31. NamedValueSet::NamedValue::NamedValue (Identifier&& n, var&& v) noexcept
  32. : name (std::move (n)),
  33. value (std::move (v))
  34. {}
  35. NamedValueSet::NamedValue& NamedValueSet::NamedValue::operator= (NamedValue&& other) noexcept
  36. {
  37. name = std::move (other.name);
  38. value = std::move (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
  48. : values (std::move (other.values)) {}
  49. NamedValueSet::NamedValueSet (std::initializer_list<NamedValue> list)
  50. : values (std::move (list))
  51. {
  52. }
  53. NamedValueSet& NamedValueSet::operator= (const NamedValueSet& other)
  54. {
  55. clear();
  56. values = other.values;
  57. return *this;
  58. }
  59. NamedValueSet& NamedValueSet::operator= (NamedValueSet&& other) noexcept
  60. {
  61. other.values.swapWith (values);
  62. return *this;
  63. }
  64. void NamedValueSet::clear()
  65. {
  66. values.clear();
  67. }
  68. bool NamedValueSet::operator== (const NamedValueSet& other) const noexcept
  69. {
  70. auto num = values.size();
  71. if (num != other.values.size())
  72. return false;
  73. for (int i = 0; i < num; ++i)
  74. {
  75. // optimise for the case where the keys are in the same order
  76. if (values.getReference(i).name == other.values.getReference(i).name)
  77. {
  78. if (values.getReference(i).value != other.values.getReference(i).value)
  79. return false;
  80. }
  81. else
  82. {
  83. // if we encounter keys that are in a different order, search remaining items by brute force..
  84. for (int j = i; j < num; ++j)
  85. {
  86. if (auto* otherVal = other.getVarPointer (values.getReference(j).name))
  87. if (values.getReference(j).value == *otherVal)
  88. continue;
  89. return false;
  90. }
  91. return true;
  92. }
  93. }
  94. return true;
  95. }
  96. bool NamedValueSet::operator!= (const NamedValueSet& other) const noexcept { return ! operator== (other); }
  97. int NamedValueSet::size() const noexcept { return values.size(); }
  98. bool NamedValueSet::isEmpty() const noexcept { return values.isEmpty(); }
  99. static const var& getNullVarRef() noexcept
  100. {
  101. static var nullVar;
  102. return nullVar;
  103. }
  104. const var& NamedValueSet::operator[] (const Identifier& name) const noexcept
  105. {
  106. if (auto* v = getVarPointer (name))
  107. return *v;
  108. return getNullVarRef();
  109. }
  110. var NamedValueSet::getWithDefault (const Identifier& name, const var& defaultReturnValue) const
  111. {
  112. if (auto* v = getVarPointer (name))
  113. return *v;
  114. return defaultReturnValue;
  115. }
  116. var* NamedValueSet::getVarPointer (const Identifier& name) noexcept
  117. {
  118. for (auto& i : values)
  119. if (i.name == name)
  120. return &(i.value);
  121. return {};
  122. }
  123. const var* NamedValueSet::getVarPointer (const Identifier& name) const noexcept
  124. {
  125. for (auto& i : values)
  126. if (i.name == name)
  127. return &(i.value);
  128. return {};
  129. }
  130. bool NamedValueSet::set (const Identifier& name, var&& newValue)
  131. {
  132. if (auto* v = getVarPointer (name))
  133. {
  134. if (v->equalsWithSameType (newValue))
  135. return false;
  136. *v = std::move (newValue);
  137. return true;
  138. }
  139. values.add ({ name, std::move (newValue) });
  140. return true;
  141. }
  142. bool NamedValueSet::set (const Identifier& name, const var& newValue)
  143. {
  144. if (auto* v = getVarPointer (name))
  145. {
  146. if (v->equalsWithSameType (newValue))
  147. return false;
  148. *v = newValue;
  149. return true;
  150. }
  151. values.add ({ name, newValue });
  152. return true;
  153. }
  154. bool NamedValueSet::contains (const Identifier& name) const noexcept
  155. {
  156. return getVarPointer (name) != nullptr;
  157. }
  158. int NamedValueSet::indexOf (const Identifier& name) const noexcept
  159. {
  160. auto numValues = values.size();
  161. for (int i = 0; i < numValues; ++i)
  162. if (values.getReference(i).name == name)
  163. return i;
  164. return -1;
  165. }
  166. bool NamedValueSet::remove (const Identifier& name)
  167. {
  168. auto numValues = values.size();
  169. for (int i = 0; i < numValues; ++i)
  170. {
  171. if (values.getReference(i).name == name)
  172. {
  173. values.remove (i);
  174. return true;
  175. }
  176. }
  177. return false;
  178. }
  179. Identifier NamedValueSet::getName (const int index) const noexcept
  180. {
  181. if (isPositiveAndBelow (index, values.size()))
  182. return values.getReference (index).name;
  183. jassertfalse;
  184. return {};
  185. }
  186. const var& NamedValueSet::getValueAt (const int index) const noexcept
  187. {
  188. if (isPositiveAndBelow (index, values.size()))
  189. return values.getReference (index).value;
  190. jassertfalse;
  191. return getNullVarRef();
  192. }
  193. var* NamedValueSet::getVarPointerAt (int index) noexcept
  194. {
  195. if (isPositiveAndBelow (index, values.size()))
  196. return &(values.getReference (index).value);
  197. return {};
  198. }
  199. const var* NamedValueSet::getVarPointerAt (int index) const noexcept
  200. {
  201. if (isPositiveAndBelow (index, values.size()))
  202. return &(values.getReference (index).value);
  203. return {};
  204. }
  205. void NamedValueSet::setFromXmlAttributes (const XmlElement& xml)
  206. {
  207. values.clearQuick();
  208. for (auto* att = xml.attributes.get(); att != nullptr; att = att->nextListItem)
  209. {
  210. if (att->name.toString().startsWith ("base64:"))
  211. {
  212. MemoryBlock mb;
  213. if (mb.fromBase64Encoding (att->value))
  214. {
  215. values.add ({ att->name.toString().substring (7), var (mb) });
  216. continue;
  217. }
  218. }
  219. values.add ({ att->name, var (att->value) });
  220. }
  221. }
  222. void NamedValueSet::copyToXmlAttributes (XmlElement& xml) const
  223. {
  224. for (auto& i : values)
  225. {
  226. if (auto* mb = i.value.getBinaryData())
  227. {
  228. xml.setAttribute ("base64:" + i.name.toString(), mb->toBase64Encoding());
  229. }
  230. else
  231. {
  232. // These types can't be stored as XML!
  233. jassert (! i.value.isObject());
  234. jassert (! i.value.isMethod());
  235. jassert (! i.value.isArray());
  236. xml.setAttribute (i.name.toString(),
  237. i.value.toString());
  238. }
  239. }
  240. }
  241. } // namespace juce