Audio plugin host https://kx.studio/carla
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) 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. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  89. Value::Value (Value&& other) noexcept
  90. {
  91. // moving a Value with listeners will lose those listeners, which
  92. // probably isn't what you wanted to happen!
  93. jassert (other.listeners.size() == 0);
  94. other.removeFromListenerList();
  95. value = static_cast<ReferenceCountedObjectPtr<ValueSource>&&> (other.value);
  96. }
  97. Value& Value::operator= (Value&& other) noexcept
  98. {
  99. // moving a Value with listeners will lose those listeners, which
  100. // probably isn't what you wanted to happen!
  101. jassert (other.listeners.size() == 0);
  102. other.removeFromListenerList();
  103. value = static_cast<ReferenceCountedObjectPtr<ValueSource>&&> (other.value);
  104. return *this;
  105. }
  106. #endif
  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. }