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.

juce_Value.cpp 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. namespace juce
  20. {
  21. Value::ValueSource::ValueSource()
  22. {
  23. }
  24. Value::ValueSource::~ValueSource()
  25. {
  26. cancelPendingUpdate();
  27. }
  28. void Value::ValueSource::handleAsyncUpdate()
  29. {
  30. sendChangeMessage (true);
  31. }
  32. void Value::ValueSource::sendChangeMessage (const bool synchronous)
  33. {
  34. const int numListeners = valuesWithListeners.size();
  35. if (numListeners > 0)
  36. {
  37. if (synchronous)
  38. {
  39. const ReferenceCountedObjectPtr<ValueSource> localRef (this);
  40. cancelPendingUpdate();
  41. for (int i = numListeners; --i >= 0;)
  42. if (Value* const v = valuesWithListeners[i])
  43. v->callListeners();
  44. }
  45. else
  46. {
  47. triggerAsyncUpdate();
  48. }
  49. }
  50. }
  51. //==============================================================================
  52. class SimpleValueSource : public Value::ValueSource
  53. {
  54. public:
  55. SimpleValueSource()
  56. {
  57. }
  58. SimpleValueSource (const var& initialValue)
  59. : value (initialValue)
  60. {
  61. }
  62. var getValue() const override
  63. {
  64. return value;
  65. }
  66. void setValue (const var& newValue) override
  67. {
  68. if (! newValue.equalsWithSameType (value))
  69. {
  70. value = newValue;
  71. sendChangeMessage (false);
  72. }
  73. }
  74. private:
  75. var value;
  76. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SimpleValueSource)
  77. };
  78. //==============================================================================
  79. Value::Value() : value (new SimpleValueSource())
  80. {
  81. }
  82. Value::Value (ValueSource* const v) : value (v)
  83. {
  84. jassert (v != nullptr);
  85. }
  86. Value::Value (const var& initialValue) : value (new SimpleValueSource (initialValue))
  87. {
  88. }
  89. Value::Value (const Value& other) : value (other.value)
  90. {
  91. }
  92. Value::Value (Value&& other) noexcept
  93. {
  94. // moving a Value with listeners will lose those listeners, which
  95. // probably isn't what you wanted to happen!
  96. jassert (other.listeners.size() == 0);
  97. other.removeFromListenerList();
  98. value = static_cast<ReferenceCountedObjectPtr<ValueSource>&&> (other.value);
  99. }
  100. Value& Value::operator= (Value&& other) noexcept
  101. {
  102. // moving a Value with listeners will lose those listeners, which
  103. // probably isn't what you wanted to happen!
  104. jassert (other.listeners.size() == 0);
  105. other.removeFromListenerList();
  106. value = static_cast<ReferenceCountedObjectPtr<ValueSource>&&> (other.value);
  107. return *this;
  108. }
  109. Value::~Value()
  110. {
  111. removeFromListenerList();
  112. }
  113. void Value::removeFromListenerList()
  114. {
  115. if (listeners.size() > 0 && value != nullptr) // may be nullptr after a move operation
  116. value->valuesWithListeners.removeValue (this);
  117. }
  118. //==============================================================================
  119. var Value::getValue() const
  120. {
  121. return value->getValue();
  122. }
  123. Value::operator var() const
  124. {
  125. return value->getValue();
  126. }
  127. void Value::setValue (const var& newValue)
  128. {
  129. value->setValue (newValue);
  130. }
  131. String Value::toString() const
  132. {
  133. return value->getValue().toString();
  134. }
  135. Value& Value::operator= (const var& newValue)
  136. {
  137. value->setValue (newValue);
  138. return *this;
  139. }
  140. void Value::referTo (const Value& valueToReferTo)
  141. {
  142. if (valueToReferTo.value != value)
  143. {
  144. if (listeners.size() > 0)
  145. {
  146. value->valuesWithListeners.removeValue (this);
  147. valueToReferTo.value->valuesWithListeners.add (this);
  148. }
  149. value = valueToReferTo.value;
  150. callListeners();
  151. }
  152. }
  153. bool Value::refersToSameSourceAs (const Value& other) const
  154. {
  155. return value == other.value;
  156. }
  157. bool Value::operator== (const Value& other) const
  158. {
  159. return value == other.value || value->getValue() == other.getValue();
  160. }
  161. bool Value::operator!= (const Value& other) const
  162. {
  163. return value != other.value && value->getValue() != other.getValue();
  164. }
  165. //==============================================================================
  166. void Value::addListener (ValueListener* const listener)
  167. {
  168. if (listener != nullptr)
  169. {
  170. if (listeners.size() == 0)
  171. value->valuesWithListeners.add (this);
  172. listeners.add (listener);
  173. }
  174. }
  175. void Value::removeListener (ValueListener* const listener)
  176. {
  177. listeners.remove (listener);
  178. if (listeners.size() == 0)
  179. value->valuesWithListeners.removeValue (this);
  180. }
  181. void Value::callListeners()
  182. {
  183. if (listeners.size() > 0)
  184. {
  185. Value v (*this); // (create a copy in case this gets deleted by a callback)
  186. listeners.call (&ValueListener::valueChanged, v);
  187. }
  188. }
  189. OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const Value& value)
  190. {
  191. return stream << value.toString();
  192. }
  193. } // namespace juce