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.

278 lines
7.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the juce_core module of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. or without fee is hereby granted, provided that the above copyright notice and this
  7. permission notice appear in all copies.
  8. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  9. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  10. NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  11. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  12. IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. ------------------------------------------------------------------------------
  15. NOTE! This permissive ISC license applies ONLY to files within the juce_core module!
  16. All other JUCE modules are covered by a dual GPL/commercial license, so if you are
  17. using any other modules, be sure to check that you also comply with their license.
  18. For more details, visit www.juce.com
  19. ==============================================================================
  20. */
  21. struct NamedValueSet::NamedValue
  22. {
  23. NamedValue() noexcept {}
  24. NamedValue (const Identifier& n, const var& v) : name (n), value (v) {}
  25. NamedValue (const NamedValue& other) : name (other.name), value (other.value) {}
  26. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  27. NamedValue (NamedValue&& other) noexcept
  28. : name (static_cast<Identifier&&> (other.name)),
  29. value (static_cast<var&&> (other.value))
  30. {
  31. }
  32. NamedValue (Identifier&& n, var&& v) noexcept
  33. : name (static_cast<Identifier&&> (n)),
  34. value (static_cast<var&&> (v))
  35. {
  36. }
  37. NamedValue& operator= (NamedValue&& other) noexcept
  38. {
  39. name = static_cast<Identifier&&> (other.name);
  40. value = static_cast<var&&> (other.value);
  41. return *this;
  42. }
  43. #endif
  44. bool operator== (const NamedValue& other) const noexcept { return name == other.name && value == other.value; }
  45. bool operator!= (const NamedValue& other) const noexcept { return ! operator== (other); }
  46. Identifier name;
  47. var value;
  48. };
  49. //==============================================================================
  50. NamedValueSet::NamedValueSet() noexcept
  51. {
  52. }
  53. NamedValueSet::NamedValueSet (const NamedValueSet& other)
  54. : values (other.values)
  55. {
  56. }
  57. NamedValueSet& NamedValueSet::operator= (const NamedValueSet& other)
  58. {
  59. clear();
  60. values = other.values;
  61. return *this;
  62. }
  63. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  64. NamedValueSet::NamedValueSet (NamedValueSet&& other) noexcept
  65. : values (static_cast<Array<NamedValue>&&> (other.values))
  66. {
  67. }
  68. NamedValueSet& NamedValueSet::operator= (NamedValueSet&& other) noexcept
  69. {
  70. other.values.swapWith (values);
  71. return *this;
  72. }
  73. #endif
  74. NamedValueSet::~NamedValueSet() noexcept
  75. {
  76. }
  77. void NamedValueSet::clear()
  78. {
  79. values.clear();
  80. }
  81. bool NamedValueSet::operator== (const NamedValueSet& other) const
  82. {
  83. return values == other.values;
  84. }
  85. bool NamedValueSet::operator!= (const NamedValueSet& other) const
  86. {
  87. return ! operator== (other);
  88. }
  89. int NamedValueSet::size() const noexcept
  90. {
  91. return values.size();
  92. }
  93. bool NamedValueSet::isEmpty() const noexcept
  94. {
  95. return values.isEmpty();
  96. }
  97. const var& NamedValueSet::operator[] (const Identifier& name) const noexcept
  98. {
  99. if (const var* v = getVarPointer (name))
  100. return *v;
  101. return var::null;
  102. }
  103. var NamedValueSet::getWithDefault (const Identifier& name, const var& defaultReturnValue) const
  104. {
  105. if (const var* const v = getVarPointer (name))
  106. return *v;
  107. return defaultReturnValue;
  108. }
  109. var* NamedValueSet::getVarPointer (const Identifier& name) const noexcept
  110. {
  111. for (NamedValue* e = values.end(), *i = values.begin(); i != e; ++i)
  112. if (i->name == name)
  113. return &(i->value);
  114. return nullptr;
  115. }
  116. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  117. bool NamedValueSet::set (const Identifier& name, var&& newValue)
  118. {
  119. if (var* const v = getVarPointer (name))
  120. {
  121. if (v->equalsWithSameType (newValue))
  122. return false;
  123. *v = static_cast<var&&> (newValue);
  124. return true;
  125. }
  126. values.add (NamedValue (name, static_cast<var&&> (newValue)));
  127. return true;
  128. }
  129. #endif
  130. bool NamedValueSet::set (const Identifier& name, const var& newValue)
  131. {
  132. if (var* const v = getVarPointer (name))
  133. {
  134. if (v->equalsWithSameType (newValue))
  135. return false;
  136. *v = newValue;
  137. return true;
  138. }
  139. values.add (NamedValue (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. const int 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. const int 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 Identifier();
  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 var::null;
  180. }
  181. var* NamedValueSet::getVarPointerAt (int index) const noexcept
  182. {
  183. if (isPositiveAndBelow (index, values.size()))
  184. return &(values.getReference (index).value);
  185. return nullptr;
  186. }
  187. void NamedValueSet::setFromXmlAttributes (const XmlElement& xml)
  188. {
  189. values.clearQuick();
  190. for (const XmlElement::XmlAttributeNode* att = xml.attributes; 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 (NamedValue (att->name.toString().substring (7), var (mb)));
  198. continue;
  199. }
  200. }
  201. values.add (NamedValue (att->name, var (att->value)));
  202. }
  203. }
  204. void NamedValueSet::copyToXmlAttributes (XmlElement& xml) const
  205. {
  206. for (NamedValue* e = values.end(), *i = values.begin(); i != e; ++i)
  207. {
  208. if (const MemoryBlock* 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. }