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.

233 lines
5.3KB

  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()
  76. : value (new SimpleValueSource())
  77. {
  78. }
  79. Value::Value (ValueSource* const v)
  80. : value (v)
  81. {
  82. jassert (v != nullptr);
  83. }
  84. Value::Value (const var& initialValue)
  85. : value (new SimpleValueSource (initialValue))
  86. {
  87. }
  88. Value::Value (const Value& other)
  89. : value (other.value)
  90. {
  91. }
  92. Value& Value::operator= (const Value& other)
  93. {
  94. value = other.value;
  95. return *this;
  96. }
  97. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  98. Value::Value (Value&& other) noexcept
  99. : value (static_cast<ReferenceCountedObjectPtr<ValueSource>&&> (other.value))
  100. {
  101. }
  102. Value& Value::operator= (Value&& other) noexcept
  103. {
  104. value = static_cast<ReferenceCountedObjectPtr<ValueSource>&&> (other.value);
  105. return *this;
  106. }
  107. #endif
  108. Value::~Value()
  109. {
  110. if (listeners.size() > 0)
  111. value->valuesWithListeners.removeValue (this);
  112. }
  113. //==============================================================================
  114. var Value::getValue() const
  115. {
  116. return value->getValue();
  117. }
  118. Value::operator var() const
  119. {
  120. return value->getValue();
  121. }
  122. void Value::setValue (const var& newValue)
  123. {
  124. value->setValue (newValue);
  125. }
  126. String Value::toString() const
  127. {
  128. return value->getValue().toString();
  129. }
  130. Value& Value::operator= (const var& newValue)
  131. {
  132. value->setValue (newValue);
  133. return *this;
  134. }
  135. void Value::referTo (const Value& valueToReferTo)
  136. {
  137. if (valueToReferTo.value != value)
  138. {
  139. if (listeners.size() > 0)
  140. {
  141. value->valuesWithListeners.removeValue (this);
  142. valueToReferTo.value->valuesWithListeners.add (this);
  143. }
  144. value = valueToReferTo.value;
  145. callListeners();
  146. }
  147. }
  148. bool Value::refersToSameSourceAs (const Value& other) const
  149. {
  150. return value == other.value;
  151. }
  152. bool Value::operator== (const Value& other) const
  153. {
  154. return value == other.value || value->getValue() == other.getValue();
  155. }
  156. bool Value::operator!= (const Value& other) const
  157. {
  158. return value != other.value && value->getValue() != other.getValue();
  159. }
  160. //==============================================================================
  161. void Value::addListener (ValueListener* const listener)
  162. {
  163. if (listener != nullptr)
  164. {
  165. if (listeners.size() == 0)
  166. value->valuesWithListeners.add (this);
  167. listeners.add (listener);
  168. }
  169. }
  170. void Value::removeListener (ValueListener* const listener)
  171. {
  172. listeners.remove (listener);
  173. if (listeners.size() == 0)
  174. value->valuesWithListeners.removeValue (this);
  175. }
  176. void Value::callListeners()
  177. {
  178. if (listeners.size() > 0)
  179. {
  180. Value v (*this); // (create a copy in case this gets deleted by a callback)
  181. listeners.call (&ValueListener::valueChanged, v);
  182. }
  183. }
  184. OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const Value& value)
  185. {
  186. return stream << value.toString();
  187. }