juce_NamedValueSet.cpp 7.3 KB

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