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.

273 lines
7.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the juce_core module of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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)
  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. const var& NamedValueSet::operator[] (const Identifier& name) const noexcept
  94. {
  95. if (const var* v = getVarPointer (name))
  96. return *v;
  97. return var::null;
  98. }
  99. var NamedValueSet::getWithDefault (const Identifier& name, const var& defaultReturnValue) const
  100. {
  101. if (const var* const v = getVarPointer (name))
  102. return *v;
  103. return defaultReturnValue;
  104. }
  105. var* NamedValueSet::getVarPointer (const Identifier& name) const noexcept
  106. {
  107. for (NamedValue* e = values.end(), *i = values.begin(); i != e; ++i)
  108. if (i->name == name)
  109. return &(i->value);
  110. return nullptr;
  111. }
  112. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  113. bool NamedValueSet::set (const Identifier& name, var&& newValue)
  114. {
  115. if (var* const v = getVarPointer (name))
  116. {
  117. if (v->equalsWithSameType (newValue))
  118. return false;
  119. *v = static_cast<var&&> (newValue);
  120. return true;
  121. }
  122. values.add (NamedValue (name, static_cast<var&&> (newValue)));
  123. return true;
  124. }
  125. #endif
  126. bool NamedValueSet::set (const Identifier& name, const var& newValue)
  127. {
  128. if (var* const v = getVarPointer (name))
  129. {
  130. if (v->equalsWithSameType (newValue))
  131. return false;
  132. *v = newValue;
  133. return true;
  134. }
  135. values.add (NamedValue (name, newValue));
  136. return true;
  137. }
  138. bool NamedValueSet::contains (const Identifier& name) const noexcept
  139. {
  140. return getVarPointer (name) != nullptr;
  141. }
  142. int NamedValueSet::indexOf (const Identifier& name) const noexcept
  143. {
  144. const int numValues = values.size();
  145. for (int i = 0; i < numValues; ++i)
  146. if (values.getReference(i).name == name)
  147. return i;
  148. return -1;
  149. }
  150. bool NamedValueSet::remove (const Identifier& name)
  151. {
  152. const int numValues = values.size();
  153. for (int i = 0; i < numValues; ++i)
  154. {
  155. if (values.getReference(i).name == name)
  156. {
  157. values.remove (i);
  158. return true;
  159. }
  160. }
  161. return false;
  162. }
  163. Identifier NamedValueSet::getName (const int index) const noexcept
  164. {
  165. if (isPositiveAndBelow (index, values.size()))
  166. return values.getReference (index).name;
  167. jassertfalse;
  168. return Identifier();
  169. }
  170. const var& NamedValueSet::getValueAt (const int index) const noexcept
  171. {
  172. if (isPositiveAndBelow (index, values.size()))
  173. return values.getReference (index).value;
  174. jassertfalse;
  175. return var::null;
  176. }
  177. var* NamedValueSet::getVarPointerAt (int index) const noexcept
  178. {
  179. if (isPositiveAndBelow (index, values.size()))
  180. return &(values.getReference (index).value);
  181. return nullptr;
  182. }
  183. void NamedValueSet::setFromXmlAttributes (const XmlElement& xml)
  184. {
  185. values.clearQuick();
  186. for (const XmlElement::XmlAttributeNode* att = xml.attributes; att != nullptr; att = att->nextListItem)
  187. {
  188. if (att->name.toString().startsWith ("base64:"))
  189. {
  190. MemoryBlock mb;
  191. if (mb.fromBase64Encoding (att->value))
  192. {
  193. values.add (NamedValue (att->name.toString().substring (7), var (mb)));
  194. continue;
  195. }
  196. }
  197. values.add (NamedValue (att->name, var (att->value)));
  198. }
  199. }
  200. void NamedValueSet::copyToXmlAttributes (XmlElement& xml) const
  201. {
  202. for (NamedValue* e = values.end(), *i = values.begin(); i != e; ++i)
  203. {
  204. if (const MemoryBlock* mb = i->value.getBinaryData())
  205. {
  206. xml.setAttribute ("base64:" + i->name.toString(), mb->toBase64Encoding());
  207. }
  208. else
  209. {
  210. // These types can't be stored as XML!
  211. jassert (! i->value.isObject());
  212. jassert (! i->value.isMethod());
  213. jassert (! i->value.isArray());
  214. xml.setAttribute (i->name.toString(),
  215. i->value.toString());
  216. }
  217. }
  218. }