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.

238 lines
5.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. Value::ValueSource::ValueSource()
  20. {
  21. }
  22. Value::ValueSource::~ValueSource()
  23. {
  24. cancelPendingUpdate();
  25. }
  26. void Value::ValueSource::handleAsyncUpdate()
  27. {
  28. sendChangeMessage (true);
  29. }
  30. void Value::ValueSource::sendChangeMessage (const bool synchronous)
  31. {
  32. const int numListeners = valuesWithListeners.size();
  33. if (numListeners > 0)
  34. {
  35. if (synchronous)
  36. {
  37. const ReferenceCountedObjectPtr<ValueSource> localRef (this);
  38. cancelPendingUpdate();
  39. for (int i = numListeners; --i >= 0;)
  40. if (Value* const v = valuesWithListeners[i])
  41. v->callListeners();
  42. }
  43. else
  44. {
  45. triggerAsyncUpdate();
  46. }
  47. }
  48. }
  49. //==============================================================================
  50. class SimpleValueSource : public Value::ValueSource
  51. {
  52. public:
  53. SimpleValueSource()
  54. {
  55. }
  56. SimpleValueSource (const var& initialValue)
  57. : value (initialValue)
  58. {
  59. }
  60. var getValue() const override
  61. {
  62. return value;
  63. }
  64. void setValue (const var& newValue) override
  65. {
  66. if (! newValue.equalsWithSameType (value))
  67. {
  68. value = newValue;
  69. sendChangeMessage (false);
  70. }
  71. }
  72. private:
  73. var value;
  74. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SimpleValueSource)
  75. };
  76. //==============================================================================
  77. Value::Value() : value (new SimpleValueSource())
  78. {
  79. }
  80. Value::Value (ValueSource* const v) : value (v)
  81. {
  82. jassert (v != nullptr);
  83. }
  84. Value::Value (const var& initialValue) : value (new SimpleValueSource (initialValue))
  85. {
  86. }
  87. Value::Value (const Value& other) : value (other.value)
  88. {
  89. }
  90. Value::Value (Value&& other) noexcept
  91. {
  92. // moving a Value with listeners will lose those listeners, which
  93. // probably isn't what you wanted to happen!
  94. jassert (other.listeners.size() == 0);
  95. other.removeFromListenerList();
  96. value = static_cast<ReferenceCountedObjectPtr<ValueSource>&&> (other.value);
  97. }
  98. Value& Value::operator= (Value&& other) noexcept
  99. {
  100. // moving a Value with listeners will lose those listeners, which
  101. // probably isn't what you wanted to happen!
  102. jassert (other.listeners.size() == 0);
  103. other.removeFromListenerList();
  104. value = static_cast<ReferenceCountedObjectPtr<ValueSource>&&> (other.value);
  105. return *this;
  106. }
  107. Value::~Value()
  108. {
  109. removeFromListenerList();
  110. }
  111. void Value::removeFromListenerList()
  112. {
  113. if (listeners.size() > 0 && value != nullptr) // may be nullptr after a move operation
  114. value->valuesWithListeners.removeValue (this);
  115. }
  116. //==============================================================================
  117. var Value::getValue() const
  118. {
  119. return value->getValue();
  120. }
  121. Value::operator var() const
  122. {
  123. return value->getValue();
  124. }
  125. void Value::setValue (const var& newValue)
  126. {
  127. value->setValue (newValue);
  128. }
  129. String Value::toString() const
  130. {
  131. return value->getValue().toString();
  132. }
  133. Value& Value::operator= (const var& newValue)
  134. {
  135. value->setValue (newValue);
  136. return *this;
  137. }
  138. void Value::referTo (const Value& valueToReferTo)
  139. {
  140. if (valueToReferTo.value != value)
  141. {
  142. if (listeners.size() > 0)
  143. {
  144. value->valuesWithListeners.removeValue (this);
  145. valueToReferTo.value->valuesWithListeners.add (this);
  146. }
  147. value = valueToReferTo.value;
  148. callListeners();
  149. }
  150. }
  151. bool Value::refersToSameSourceAs (const Value& other) const
  152. {
  153. return value == other.value;
  154. }
  155. bool Value::operator== (const Value& other) const
  156. {
  157. return value == other.value || value->getValue() == other.getValue();
  158. }
  159. bool Value::operator!= (const Value& other) const
  160. {
  161. return value != other.value && value->getValue() != other.getValue();
  162. }
  163. //==============================================================================
  164. void Value::addListener (ValueListener* const listener)
  165. {
  166. if (listener != nullptr)
  167. {
  168. if (listeners.size() == 0)
  169. value->valuesWithListeners.add (this);
  170. listeners.add (listener);
  171. }
  172. }
  173. void Value::removeListener (ValueListener* const listener)
  174. {
  175. listeners.remove (listener);
  176. if (listeners.size() == 0)
  177. value->valuesWithListeners.removeValue (this);
  178. }
  179. void Value::callListeners()
  180. {
  181. if (listeners.size() > 0)
  182. {
  183. Value v (*this); // (create a copy in case this gets deleted by a callback)
  184. listeners.call (&ValueListener::valueChanged, v);
  185. }
  186. }
  187. OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const Value& value)
  188. {
  189. return stream << value.toString();
  190. }