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.

244 lines
5.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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
  59. {
  60. return value;
  61. }
  62. void setValue (const var& newValue)
  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::operator= (const Value& other)
  89. {
  90. value = other.value;
  91. return *this;
  92. }
  93. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  94. Value::Value (Value&& other) noexcept
  95. {
  96. // moving a Value with listeners will lose those listeners, which
  97. // probably isn't what you wanted to happen!
  98. jassert (other.listeners.size() == 0);
  99. other.removeFromListenerList();
  100. value = static_cast<ReferenceCountedObjectPtr<ValueSource>&&> (other.value);
  101. }
  102. Value& Value::operator= (Value&& other) noexcept
  103. {
  104. // moving a Value with listeners will lose those listeners, which
  105. // probably isn't what you wanted to happen!
  106. jassert (other.listeners.size() == 0);
  107. other.removeFromListenerList();
  108. value = static_cast<ReferenceCountedObjectPtr<ValueSource>&&> (other.value);
  109. return *this;
  110. }
  111. #endif
  112. Value::~Value()
  113. {
  114. removeFromListenerList();
  115. }
  116. void Value::removeFromListenerList()
  117. {
  118. if (listeners.size() > 0 && value != nullptr) // may be nullptr after a move operation
  119. value->valuesWithListeners.removeValue (this);
  120. }
  121. //==============================================================================
  122. var Value::getValue() const
  123. {
  124. return value->getValue();
  125. }
  126. Value::operator var() const
  127. {
  128. return value->getValue();
  129. }
  130. void Value::setValue (const var& newValue)
  131. {
  132. value->setValue (newValue);
  133. }
  134. String Value::toString() const
  135. {
  136. return value->getValue().toString();
  137. }
  138. Value& Value::operator= (const var& newValue)
  139. {
  140. value->setValue (newValue);
  141. return *this;
  142. }
  143. void Value::referTo (const Value& valueToReferTo)
  144. {
  145. if (valueToReferTo.value != value)
  146. {
  147. if (listeners.size() > 0)
  148. {
  149. value->valuesWithListeners.removeValue (this);
  150. valueToReferTo.value->valuesWithListeners.add (this);
  151. }
  152. value = valueToReferTo.value;
  153. callListeners();
  154. }
  155. }
  156. bool Value::refersToSameSourceAs (const Value& other) const
  157. {
  158. return value == other.value;
  159. }
  160. bool Value::operator== (const Value& other) const
  161. {
  162. return value == other.value || value->getValue() == other.getValue();
  163. }
  164. bool Value::operator!= (const Value& other) const
  165. {
  166. return value != other.value && value->getValue() != other.getValue();
  167. }
  168. //==============================================================================
  169. void Value::addListener (ValueListener* const listener)
  170. {
  171. if (listener != nullptr)
  172. {
  173. if (listeners.size() == 0)
  174. value->valuesWithListeners.add (this);
  175. listeners.add (listener);
  176. }
  177. }
  178. void Value::removeListener (ValueListener* const listener)
  179. {
  180. listeners.remove (listener);
  181. if (listeners.size() == 0)
  182. value->valuesWithListeners.removeValue (this);
  183. }
  184. void Value::callListeners()
  185. {
  186. if (listeners.size() > 0)
  187. {
  188. Value v (*this); // (create a copy in case this gets deleted by a callback)
  189. listeners.call (&ValueListener::valueChanged, v);
  190. }
  191. }
  192. OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const Value& value)
  193. {
  194. return stream << value.toString();
  195. }