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.

284 lines
7.4KB

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