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.

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