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.

220 lines
5.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. BEGIN_JUCE_NAMESPACE
  19. //==============================================================================
  20. Value::ValueSource::ValueSource()
  21. {
  22. }
  23. Value::ValueSource::~ValueSource()
  24. {
  25. }
  26. void Value::ValueSource::sendChangeMessage (const bool synchronous)
  27. {
  28. if (synchronous)
  29. {
  30. // (hold a local reference to this object in case it's freed during the callbacks)
  31. const ReferenceCountedObjectPtr<ValueSource> localRef (this);
  32. for (int i = valuesWithListeners.size(); --i >= 0;)
  33. {
  34. Value* const v = valuesWithListeners[i];
  35. if (v != nullptr)
  36. v->callListeners();
  37. }
  38. }
  39. else
  40. {
  41. triggerAsyncUpdate();
  42. }
  43. }
  44. void Value::ValueSource::handleAsyncUpdate()
  45. {
  46. sendChangeMessage (true);
  47. }
  48. //==============================================================================
  49. class SimpleValueSource : public Value::ValueSource
  50. {
  51. public:
  52. SimpleValueSource()
  53. {
  54. }
  55. SimpleValueSource (const var& initialValue)
  56. : value (initialValue)
  57. {
  58. }
  59. var getValue() const
  60. {
  61. return value;
  62. }
  63. void setValue (const var& newValue)
  64. {
  65. if (! newValue.equalsWithSameType (value))
  66. {
  67. value = newValue;
  68. sendChangeMessage (false);
  69. }
  70. }
  71. private:
  72. var value;
  73. JUCE_DECLARE_NON_COPYABLE (SimpleValueSource);
  74. };
  75. //==============================================================================
  76. Value::Value()
  77. : value (new SimpleValueSource())
  78. {
  79. }
  80. Value::Value (ValueSource* const value_)
  81. : value (value_)
  82. {
  83. jassert (value_ != nullptr);
  84. }
  85. Value::Value (const var& initialValue)
  86. : value (new SimpleValueSource (initialValue))
  87. {
  88. }
  89. Value::Value (const Value& other)
  90. : value (other.value)
  91. {
  92. }
  93. Value& Value::operator= (const Value& other)
  94. {
  95. value = other.value;
  96. return *this;
  97. }
  98. Value::~Value()
  99. {
  100. if (listeners.size() > 0)
  101. value->valuesWithListeners.removeValue (this);
  102. }
  103. //==============================================================================
  104. var Value::getValue() const
  105. {
  106. return value->getValue();
  107. }
  108. Value::operator var() const
  109. {
  110. return getValue();
  111. }
  112. void Value::setValue (const var& newValue)
  113. {
  114. value->setValue (newValue);
  115. }
  116. String Value::toString() const
  117. {
  118. return value->getValue().toString();
  119. }
  120. Value& Value::operator= (const var& newValue)
  121. {
  122. value->setValue (newValue);
  123. return *this;
  124. }
  125. void Value::referTo (const Value& valueToReferTo)
  126. {
  127. if (valueToReferTo.value != value)
  128. {
  129. if (listeners.size() > 0)
  130. {
  131. value->valuesWithListeners.removeValue (this);
  132. valueToReferTo.value->valuesWithListeners.add (this);
  133. }
  134. value = valueToReferTo.value;
  135. callListeners();
  136. }
  137. }
  138. bool Value::refersToSameSourceAs (const Value& other) const
  139. {
  140. return value == other.value;
  141. }
  142. bool Value::operator== (const Value& other) const
  143. {
  144. return value == other.value || value->getValue() == other.getValue();
  145. }
  146. bool Value::operator!= (const Value& other) const
  147. {
  148. return value != other.value && value->getValue() != other.getValue();
  149. }
  150. //==============================================================================
  151. void Value::addListener (ValueListener* const listener)
  152. {
  153. if (listener != nullptr)
  154. {
  155. if (listeners.size() == 0)
  156. value->valuesWithListeners.add (this);
  157. listeners.add (listener);
  158. }
  159. }
  160. void Value::removeListener (ValueListener* const listener)
  161. {
  162. listeners.remove (listener);
  163. if (listeners.size() == 0)
  164. value->valuesWithListeners.removeValue (this);
  165. }
  166. void Value::callListeners()
  167. {
  168. Value v (*this); // (create a copy in case this gets deleted by a callback)
  169. listeners.call (&ValueListener::valueChanged, v);
  170. }
  171. OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const Value& value)
  172. {
  173. return stream << value.toString();
  174. }
  175. END_JUCE_NAMESPACE