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.

280 lines
7.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. NamedValueSet::NamedValue::NamedValue() noexcept
  19. {
  20. }
  21. inline NamedValueSet::NamedValue::NamedValue (const Identifier& name_, const var& value_)
  22. : name (name_), value (value_)
  23. {
  24. }
  25. NamedValueSet::NamedValue::NamedValue (const NamedValue& other)
  26. : name (other.name), value (other.value)
  27. {
  28. }
  29. NamedValueSet::NamedValue& NamedValueSet::NamedValue::operator= (const NamedValueSet::NamedValue& other)
  30. {
  31. name = other.name;
  32. value = other.value;
  33. return *this;
  34. }
  35. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  36. NamedValueSet::NamedValue::NamedValue (NamedValue&& other) noexcept
  37. : nextListItem (static_cast <LinkedListPointer<NamedValue>&&> (other.nextListItem)),
  38. name (static_cast <Identifier&&> (other.name)),
  39. value (static_cast <var&&> (other.value))
  40. {
  41. }
  42. inline NamedValueSet::NamedValue::NamedValue (const Identifier& name_, var&& value_)
  43. : name (name_), value (static_cast <var&&> (value_))
  44. {
  45. }
  46. NamedValueSet::NamedValue& NamedValueSet::NamedValue::operator= (NamedValue&& other) noexcept
  47. {
  48. nextListItem = static_cast <LinkedListPointer<NamedValue>&&> (other.nextListItem);
  49. name = static_cast <Identifier&&> (other.name);
  50. value = static_cast <var&&> (other.value);
  51. return *this;
  52. }
  53. #endif
  54. bool NamedValueSet::NamedValue::operator== (const NamedValueSet::NamedValue& other) const noexcept
  55. {
  56. return name == other.name && value == other.value;
  57. }
  58. //==============================================================================
  59. NamedValueSet::NamedValueSet() noexcept
  60. {
  61. }
  62. NamedValueSet::NamedValueSet (const NamedValueSet& other)
  63. {
  64. values.addCopyOfList (other.values);
  65. }
  66. NamedValueSet& NamedValueSet::operator= (const NamedValueSet& other)
  67. {
  68. clear();
  69. values.addCopyOfList (other.values);
  70. return *this;
  71. }
  72. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  73. NamedValueSet::NamedValueSet (NamedValueSet&& other) noexcept
  74. : values (static_cast <LinkedListPointer<NamedValue>&&> (other.values))
  75. {
  76. }
  77. NamedValueSet& NamedValueSet::operator= (NamedValueSet&& other) noexcept
  78. {
  79. other.values.swapWith (values);
  80. return *this;
  81. }
  82. #endif
  83. NamedValueSet::~NamedValueSet()
  84. {
  85. clear();
  86. }
  87. void NamedValueSet::clear()
  88. {
  89. values.deleteAll();
  90. }
  91. bool NamedValueSet::operator== (const NamedValueSet& other) const
  92. {
  93. const NamedValue* i1 = values;
  94. const NamedValue* i2 = other.values;
  95. while (i1 != nullptr && i2 != nullptr)
  96. {
  97. if (! (*i1 == *i2))
  98. return false;
  99. i1 = i1->nextListItem;
  100. i2 = i2->nextListItem;
  101. }
  102. return true;
  103. }
  104. bool NamedValueSet::operator!= (const NamedValueSet& other) const
  105. {
  106. return ! operator== (other);
  107. }
  108. int NamedValueSet::size() const noexcept
  109. {
  110. return values.size();
  111. }
  112. const var& NamedValueSet::operator[] (const Identifier& name) const
  113. {
  114. for (NamedValue* i = values; i != nullptr; i = i->nextListItem)
  115. if (i->name == name)
  116. return i->value;
  117. return var::null;
  118. }
  119. var NamedValueSet::getWithDefault (const Identifier& name, const var& defaultReturnValue) const
  120. {
  121. if (const var* const v = getVarPointer (name))
  122. return *v;
  123. return defaultReturnValue;
  124. }
  125. var* NamedValueSet::getVarPointer (const Identifier& name) const noexcept
  126. {
  127. for (NamedValue* i = values; i != nullptr; i = i->nextListItem)
  128. if (i->name == name)
  129. return &(i->value);
  130. return nullptr;
  131. }
  132. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  133. bool NamedValueSet::set (const Identifier& name, var&& newValue)
  134. {
  135. LinkedListPointer<NamedValue>* i = &values;
  136. while (i->get() != nullptr)
  137. {
  138. NamedValue* const v = i->get();
  139. if (v->name == name)
  140. {
  141. if (v->value.equalsWithSameType (newValue))
  142. return false;
  143. v->value = static_cast <var&&> (newValue);
  144. return true;
  145. }
  146. i = &(v->nextListItem);
  147. }
  148. i->insertNext (new NamedValue (name, static_cast <var&&> (newValue)));
  149. return true;
  150. }
  151. #endif
  152. bool NamedValueSet::set (const Identifier& name, const var& newValue)
  153. {
  154. LinkedListPointer<NamedValue>* i = &values;
  155. while (i->get() != nullptr)
  156. {
  157. NamedValue* const v = i->get();
  158. if (v->name == name)
  159. {
  160. if (v->value.equalsWithSameType (newValue))
  161. return false;
  162. v->value = newValue;
  163. return true;
  164. }
  165. i = &(v->nextListItem);
  166. }
  167. i->insertNext (new NamedValue (name, newValue));
  168. return true;
  169. }
  170. bool NamedValueSet::contains (const Identifier& name) const
  171. {
  172. return getVarPointer (name) != nullptr;
  173. }
  174. bool NamedValueSet::remove (const Identifier& name)
  175. {
  176. LinkedListPointer<NamedValue>* i = &values;
  177. for (;;)
  178. {
  179. NamedValue* const v = i->get();
  180. if (v == nullptr)
  181. break;
  182. if (v->name == name)
  183. {
  184. delete i->removeNext();
  185. return true;
  186. }
  187. i = &(v->nextListItem);
  188. }
  189. return false;
  190. }
  191. const Identifier NamedValueSet::getName (const int index) const
  192. {
  193. const NamedValue* const v = values[index];
  194. jassert (v != nullptr);
  195. return v->name;
  196. }
  197. const var& NamedValueSet::getValueAt (const int index) const
  198. {
  199. const NamedValue* const v = values[index];
  200. jassert (v != nullptr);
  201. return v->value;
  202. }
  203. void NamedValueSet::setFromXmlAttributes (const XmlElement& xml)
  204. {
  205. clear();
  206. LinkedListPointer<NamedValue>::Appender appender (values);
  207. const int numAtts = xml.getNumAttributes(); // xxx inefficient - should write an att iterator..
  208. for (int i = 0; i < numAtts; ++i)
  209. appender.append (new NamedValue (xml.getAttributeName (i), var (xml.getAttributeValue (i))));
  210. }
  211. void NamedValueSet::copyToXmlAttributes (XmlElement& xml) const
  212. {
  213. for (NamedValue* i = values; i != nullptr; i = i->nextListItem)
  214. {
  215. jassert (! i->value.isObject()); // DynamicObjects can't be stored as XML!
  216. xml.setAttribute (i->name.toString(),
  217. i->value.toString());
  218. }
  219. }