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.

254 lines
6.6KB

  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. //==============================================================================
  22. NamedValueSet::NamedValueSet() noexcept
  23. {
  24. }
  25. NamedValueSet::NamedValueSet (const NamedValueSet& other)
  26. : values (other.values)
  27. {
  28. }
  29. NamedValueSet& NamedValueSet::operator= (const NamedValueSet& other)
  30. {
  31. clear();
  32. values = other.values;
  33. return *this;
  34. }
  35. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  36. NamedValueSet::NamedValueSet (NamedValueSet&& other) noexcept
  37. : values (static_cast<Array<NamedValue>&&> (other.values))
  38. {
  39. }
  40. NamedValueSet& NamedValueSet::operator= (NamedValueSet&& other) noexcept
  41. {
  42. other.values.swapWith (values);
  43. return *this;
  44. }
  45. #endif
  46. NamedValueSet::~NamedValueSet() noexcept
  47. {
  48. }
  49. void NamedValueSet::clear()
  50. {
  51. values.clear();
  52. }
  53. bool NamedValueSet::operator== (const NamedValueSet& other) const
  54. {
  55. return values == other.values;
  56. }
  57. bool NamedValueSet::operator!= (const NamedValueSet& other) const
  58. {
  59. return ! operator== (other);
  60. }
  61. int NamedValueSet::size() const noexcept
  62. {
  63. return values.size();
  64. }
  65. bool NamedValueSet::isEmpty() const noexcept
  66. {
  67. return values.isEmpty();
  68. }
  69. static const var& getNullVarRef() noexcept
  70. {
  71. #if JUCE_ALLOW_STATIC_NULL_VARIABLES
  72. return var::null;
  73. #else
  74. static var nullVar;
  75. return nullVar;
  76. #endif
  77. }
  78. const var& NamedValueSet::operator[] (const Identifier& name) const noexcept
  79. {
  80. if (const var* v = getVarPointer (name))
  81. return *v;
  82. return getNullVarRef();
  83. }
  84. var NamedValueSet::getWithDefault (const Identifier& name, const var& defaultReturnValue) const
  85. {
  86. if (const var* const v = getVarPointer (name))
  87. return *v;
  88. return defaultReturnValue;
  89. }
  90. var* NamedValueSet::getVarPointer (const Identifier& name) const noexcept
  91. {
  92. for (NamedValue* e = values.end(), *i = values.begin(); i != e; ++i)
  93. if (i->name == name)
  94. return &(i->value);
  95. return nullptr;
  96. }
  97. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  98. bool NamedValueSet::set (const Identifier& name, var&& newValue)
  99. {
  100. if (var* const v = getVarPointer (name))
  101. {
  102. if (v->equalsWithSameType (newValue))
  103. return false;
  104. *v = static_cast<var&&> (newValue);
  105. return true;
  106. }
  107. values.add (NamedValue (name, static_cast<var&&> (newValue)));
  108. return true;
  109. }
  110. #endif
  111. bool NamedValueSet::set (const Identifier& name, const var& newValue)
  112. {
  113. if (var* const v = getVarPointer (name))
  114. {
  115. if (v->equalsWithSameType (newValue))
  116. return false;
  117. *v = newValue;
  118. return true;
  119. }
  120. values.add (NamedValue (name, newValue));
  121. return true;
  122. }
  123. bool NamedValueSet::contains (const Identifier& name) const noexcept
  124. {
  125. return getVarPointer (name) != nullptr;
  126. }
  127. int NamedValueSet::indexOf (const Identifier& name) const noexcept
  128. {
  129. const int numValues = values.size();
  130. for (int i = 0; i < numValues; ++i)
  131. if (values.getReference(i).name == name)
  132. return i;
  133. return -1;
  134. }
  135. bool NamedValueSet::remove (const Identifier& name)
  136. {
  137. const int numValues = values.size();
  138. for (int i = 0; i < numValues; ++i)
  139. {
  140. if (values.getReference(i).name == name)
  141. {
  142. values.remove (i);
  143. return true;
  144. }
  145. }
  146. return false;
  147. }
  148. Identifier NamedValueSet::getName (const int index) const noexcept
  149. {
  150. if (isPositiveAndBelow (index, values.size()))
  151. return values.getReference (index).name;
  152. jassertfalse;
  153. return Identifier();
  154. }
  155. const var& NamedValueSet::getValueAt (const int index) const noexcept
  156. {
  157. if (isPositiveAndBelow (index, values.size()))
  158. return values.getReference (index).value;
  159. jassertfalse;
  160. return getNullVarRef();
  161. }
  162. var* NamedValueSet::getVarPointerAt (int index) const noexcept
  163. {
  164. if (isPositiveAndBelow (index, values.size()))
  165. return &(values.getReference (index).value);
  166. return nullptr;
  167. }
  168. void NamedValueSet::setFromXmlAttributes (const XmlElement& xml)
  169. {
  170. values.clearQuick();
  171. for (const XmlElement::XmlAttributeNode* att = xml.attributes; att != nullptr; att = att->nextListItem)
  172. {
  173. if (att->name.toString().startsWith ("base64:"))
  174. {
  175. MemoryBlock mb;
  176. if (mb.fromBase64Encoding (att->value))
  177. {
  178. values.add (NamedValue (att->name.toString().substring (7), var (mb)));
  179. continue;
  180. }
  181. }
  182. values.add (NamedValue (att->name, var (att->value)));
  183. }
  184. }
  185. void NamedValueSet::copyToXmlAttributes (XmlElement& xml) const
  186. {
  187. for (NamedValue* e = values.end(), *i = values.begin(); i != e; ++i)
  188. {
  189. if (const MemoryBlock* mb = i->value.getBinaryData())
  190. {
  191. xml.setAttribute ("base64:" + i->name.toString(), mb->toBase64Encoding());
  192. }
  193. else
  194. {
  195. // These types can't be stored as XML!
  196. jassert (! i->value.isObject());
  197. jassert (! i->value.isMethod());
  198. jassert (! i->value.isArray());
  199. xml.setAttribute (i->name.toString(),
  200. i->value.toString());
  201. }
  202. }
  203. }