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.

253 lines
6.6KB

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