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.

236 lines
5.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. Value::ValueSource::ValueSource()
  18. {
  19. }
  20. Value::ValueSource::~ValueSource()
  21. {
  22. cancelPendingUpdate();
  23. }
  24. void Value::ValueSource::handleAsyncUpdate()
  25. {
  26. sendChangeMessage (true);
  27. }
  28. void Value::ValueSource::sendChangeMessage (const bool synchronous)
  29. {
  30. const int numListeners = valuesWithListeners.size();
  31. if (numListeners > 0)
  32. {
  33. if (synchronous)
  34. {
  35. const ReferenceCountedObjectPtr<ValueSource> localRef (this);
  36. cancelPendingUpdate();
  37. for (int i = numListeners; --i >= 0;)
  38. if (Value* const v = valuesWithListeners[i])
  39. v->callListeners();
  40. }
  41. else
  42. {
  43. triggerAsyncUpdate();
  44. }
  45. }
  46. }
  47. //==============================================================================
  48. class SimpleValueSource : public Value::ValueSource
  49. {
  50. public:
  51. SimpleValueSource()
  52. {
  53. }
  54. SimpleValueSource (const var& initialValue)
  55. : value (initialValue)
  56. {
  57. }
  58. var getValue() const override
  59. {
  60. return value;
  61. }
  62. void setValue (const var& newValue) override
  63. {
  64. if (! newValue.equalsWithSameType (value))
  65. {
  66. value = newValue;
  67. sendChangeMessage (false);
  68. }
  69. }
  70. private:
  71. var value;
  72. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SimpleValueSource)
  73. };
  74. //==============================================================================
  75. Value::Value() : value (new SimpleValueSource())
  76. {
  77. }
  78. Value::Value (ValueSource* const v) : value (v)
  79. {
  80. jassert (v != nullptr);
  81. }
  82. Value::Value (const var& initialValue) : value (new SimpleValueSource (initialValue))
  83. {
  84. }
  85. Value::Value (const Value& other) : value (other.value)
  86. {
  87. }
  88. Value::Value (Value&& other) noexcept
  89. {
  90. // moving a Value with listeners will lose those listeners, which
  91. // probably isn't what you wanted to happen!
  92. jassert (other.listeners.size() == 0);
  93. other.removeFromListenerList();
  94. value = static_cast<ReferenceCountedObjectPtr<ValueSource>&&> (other.value);
  95. }
  96. Value& Value::operator= (Value&& other) noexcept
  97. {
  98. // moving a Value with listeners will lose those listeners, which
  99. // probably isn't what you wanted to happen!
  100. jassert (other.listeners.size() == 0);
  101. other.removeFromListenerList();
  102. value = static_cast<ReferenceCountedObjectPtr<ValueSource>&&> (other.value);
  103. return *this;
  104. }
  105. Value::~Value()
  106. {
  107. removeFromListenerList();
  108. }
  109. void Value::removeFromListenerList()
  110. {
  111. if (listeners.size() > 0 && value != nullptr) // may be nullptr after a move operation
  112. value->valuesWithListeners.removeValue (this);
  113. }
  114. //==============================================================================
  115. var Value::getValue() const
  116. {
  117. return value->getValue();
  118. }
  119. Value::operator var() const
  120. {
  121. return value->getValue();
  122. }
  123. void Value::setValue (const var& newValue)
  124. {
  125. value->setValue (newValue);
  126. }
  127. String Value::toString() const
  128. {
  129. return value->getValue().toString();
  130. }
  131. Value& Value::operator= (const var& newValue)
  132. {
  133. value->setValue (newValue);
  134. return *this;
  135. }
  136. void Value::referTo (const Value& valueToReferTo)
  137. {
  138. if (valueToReferTo.value != value)
  139. {
  140. if (listeners.size() > 0)
  141. {
  142. value->valuesWithListeners.removeValue (this);
  143. valueToReferTo.value->valuesWithListeners.add (this);
  144. }
  145. value = valueToReferTo.value;
  146. callListeners();
  147. }
  148. }
  149. bool Value::refersToSameSourceAs (const Value& other) const
  150. {
  151. return value == other.value;
  152. }
  153. bool Value::operator== (const Value& other) const
  154. {
  155. return value == other.value || value->getValue() == other.getValue();
  156. }
  157. bool Value::operator!= (const Value& other) const
  158. {
  159. return value != other.value && value->getValue() != other.getValue();
  160. }
  161. //==============================================================================
  162. void Value::addListener (ValueListener* const listener)
  163. {
  164. if (listener != nullptr)
  165. {
  166. if (listeners.size() == 0)
  167. value->valuesWithListeners.add (this);
  168. listeners.add (listener);
  169. }
  170. }
  171. void Value::removeListener (ValueListener* const listener)
  172. {
  173. listeners.remove (listener);
  174. if (listeners.size() == 0)
  175. value->valuesWithListeners.removeValue (this);
  176. }
  177. void Value::callListeners()
  178. {
  179. if (listeners.size() > 0)
  180. {
  181. Value v (*this); // (create a copy in case this gets deleted by a callback)
  182. listeners.call (&ValueListener::valueChanged, v);
  183. }
  184. }
  185. OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const Value& value)
  186. {
  187. return stream << value.toString();
  188. }