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.

244 lines
6.0KB

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