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.

310 lines
8.2KB

  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. NamedValueSet::NamedValue::NamedValue() noexcept
  22. {
  23. }
  24. inline NamedValueSet::NamedValue::NamedValue (const Identifier n, const var& v)
  25. : name (n), value (v)
  26. {
  27. }
  28. NamedValueSet::NamedValue::NamedValue (const NamedValue& other)
  29. : name (other.name), value (other.value)
  30. {
  31. }
  32. NamedValueSet::NamedValue& NamedValueSet::NamedValue::operator= (const NamedValueSet::NamedValue& other)
  33. {
  34. name = other.name;
  35. value = other.value;
  36. return *this;
  37. }
  38. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  39. NamedValueSet::NamedValue::NamedValue (NamedValue&& other) noexcept
  40. : nextListItem (static_cast <LinkedListPointer<NamedValue>&&> (other.nextListItem)),
  41. name (static_cast <Identifier&&> (other.name)),
  42. value (static_cast <var&&> (other.value))
  43. {
  44. }
  45. inline NamedValueSet::NamedValue::NamedValue (const Identifier n, var&& v)
  46. : name (n), value (static_cast <var&&> (v))
  47. {
  48. }
  49. NamedValueSet::NamedValue& NamedValueSet::NamedValue::operator= (NamedValue&& other) noexcept
  50. {
  51. nextListItem = static_cast <LinkedListPointer<NamedValue>&&> (other.nextListItem);
  52. name = static_cast <Identifier&&> (other.name);
  53. value = static_cast <var&&> (other.value);
  54. return *this;
  55. }
  56. #endif
  57. bool NamedValueSet::NamedValue::operator== (const NamedValueSet::NamedValue& other) const noexcept
  58. {
  59. return name == other.name && value == other.value;
  60. }
  61. //==============================================================================
  62. NamedValueSet::NamedValueSet() noexcept
  63. {
  64. }
  65. NamedValueSet::NamedValueSet (const NamedValueSet& other)
  66. {
  67. values.addCopyOfList (other.values);
  68. }
  69. NamedValueSet& NamedValueSet::operator= (const NamedValueSet& other)
  70. {
  71. clear();
  72. values.addCopyOfList (other.values);
  73. return *this;
  74. }
  75. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  76. NamedValueSet::NamedValueSet (NamedValueSet&& other) noexcept
  77. : values (static_cast <LinkedListPointer<NamedValue>&&> (other.values))
  78. {
  79. }
  80. NamedValueSet& NamedValueSet::operator= (NamedValueSet&& other) noexcept
  81. {
  82. other.values.swapWith (values);
  83. return *this;
  84. }
  85. #endif
  86. NamedValueSet::~NamedValueSet()
  87. {
  88. clear();
  89. }
  90. void NamedValueSet::clear()
  91. {
  92. values.deleteAll();
  93. }
  94. bool NamedValueSet::operator== (const NamedValueSet& other) const
  95. {
  96. const NamedValue* i1 = values;
  97. const NamedValue* i2 = other.values;
  98. while (i1 != nullptr && i2 != nullptr)
  99. {
  100. if (! (*i1 == *i2))
  101. return false;
  102. i1 = i1->nextListItem;
  103. i2 = i2->nextListItem;
  104. }
  105. return true;
  106. }
  107. bool NamedValueSet::operator!= (const NamedValueSet& other) const
  108. {
  109. return ! operator== (other);
  110. }
  111. int NamedValueSet::size() const noexcept
  112. {
  113. return values.size();
  114. }
  115. const var& NamedValueSet::operator[] (const Identifier name) const
  116. {
  117. for (NamedValue* i = values; i != nullptr; i = i->nextListItem)
  118. if (i->name == name)
  119. return i->value;
  120. return var::null;
  121. }
  122. var NamedValueSet::getWithDefault (const Identifier name, const var& defaultReturnValue) const
  123. {
  124. if (const var* const v = getVarPointer (name))
  125. return *v;
  126. return defaultReturnValue;
  127. }
  128. var* NamedValueSet::getVarPointer (const Identifier name) const noexcept
  129. {
  130. for (NamedValue* i = values; i != nullptr; i = i->nextListItem)
  131. if (i->name == name)
  132. return &(i->value);
  133. return nullptr;
  134. }
  135. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  136. bool NamedValueSet::set (const Identifier name, var&& newValue)
  137. {
  138. LinkedListPointer<NamedValue>* i = &values;
  139. while (i->get() != nullptr)
  140. {
  141. NamedValue* const v = i->get();
  142. if (v->name == name)
  143. {
  144. if (v->value.equalsWithSameType (newValue))
  145. return false;
  146. v->value = static_cast <var&&> (newValue);
  147. return true;
  148. }
  149. i = &(v->nextListItem);
  150. }
  151. i->insertNext (new NamedValue (name, static_cast <var&&> (newValue)));
  152. return true;
  153. }
  154. #endif
  155. bool NamedValueSet::set (const Identifier name, const var& newValue)
  156. {
  157. LinkedListPointer<NamedValue>* i = &values;
  158. while (i->get() != nullptr)
  159. {
  160. NamedValue* const v = i->get();
  161. if (v->name == name)
  162. {
  163. if (v->value.equalsWithSameType (newValue))
  164. return false;
  165. v->value = newValue;
  166. return true;
  167. }
  168. i = &(v->nextListItem);
  169. }
  170. i->insertNext (new NamedValue (name, newValue));
  171. return true;
  172. }
  173. bool NamedValueSet::contains (const Identifier name) const
  174. {
  175. return getVarPointer (name) != nullptr;
  176. }
  177. bool NamedValueSet::remove (const Identifier name)
  178. {
  179. LinkedListPointer<NamedValue>* i = &values;
  180. for (;;)
  181. {
  182. NamedValue* const v = i->get();
  183. if (v == nullptr)
  184. break;
  185. if (v->name == name)
  186. {
  187. delete i->removeNext();
  188. return true;
  189. }
  190. i = &(v->nextListItem);
  191. }
  192. return false;
  193. }
  194. const Identifier NamedValueSet::getName (const int index) const
  195. {
  196. const NamedValue* const v = values[index];
  197. jassert (v != nullptr);
  198. return v->name;
  199. }
  200. const var& NamedValueSet::getValueAt (const int index) const
  201. {
  202. const NamedValue* const v = values[index];
  203. jassert (v != nullptr);
  204. return v->value;
  205. }
  206. void NamedValueSet::setFromXmlAttributes (const XmlElement& xml)
  207. {
  208. clear();
  209. LinkedListPointer<NamedValue>::Appender appender (values);
  210. const int numAtts = xml.getNumAttributes(); // xxx inefficient - should write an att iterator..
  211. for (int i = 0; i < numAtts; ++i)
  212. {
  213. const String& name = xml.getAttributeName (i);
  214. const String& value = xml.getAttributeValue (i);
  215. if (name.startsWith ("base64:"))
  216. {
  217. MemoryBlock mb;
  218. if (mb.fromBase64Encoding (value))
  219. {
  220. appender.append (new NamedValue (name.substring (7), var (mb)));
  221. continue;
  222. }
  223. }
  224. appender.append (new NamedValue (name, var (value)));
  225. }
  226. }
  227. void NamedValueSet::copyToXmlAttributes (XmlElement& xml) const
  228. {
  229. for (NamedValue* i = values; i != nullptr; i = i->nextListItem)
  230. {
  231. if (const MemoryBlock* mb = i->value.getBinaryData())
  232. {
  233. xml.setAttribute ("base64:" + i->name.toString(),
  234. mb->toBase64Encoding());
  235. }
  236. else
  237. {
  238. // These types can't be stored as XML!
  239. jassert (! i->value.isObject());
  240. jassert (! i->value.isMethod());
  241. jassert (! i->value.isArray());
  242. xml.setAttribute (i->name.toString(),
  243. i->value.toString());
  244. }
  245. }
  246. }