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.

229 lines
5.8KB

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