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
9.0KB

  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. //==============================================================================
  22. /**
  23. Represents a shared variant value.
  24. A Value object contains a reference to a var object, and can get and set its value.
  25. Listeners can be attached to be told when the value is changed.
  26. The Value class is a wrapper around a shared, reference-counted underlying data
  27. object - this means that multiple Value objects can all refer to the same piece of
  28. data, allowing all of them to be notified when any of them changes it.
  29. When you create a Value with its default constructor, it acts as a wrapper around a
  30. simple var object, but by creating a Value that refers to a custom subclass of ValueSource,
  31. you can map the Value onto any kind of underlying data.
  32. Important note! The Value class is not thread-safe! If you're accessing one from
  33. multiple threads, then you'll need to use your own synchronisation around any code
  34. that accesses it.
  35. */
  36. class JUCE_API Value
  37. {
  38. public:
  39. //==============================================================================
  40. /** Creates an empty Value, containing a void var. */
  41. Value();
  42. /** Creates a Value that refers to the same value as another one.
  43. Note that this doesn't make a copy of the other value - both this and the other
  44. Value will share the same underlying value, so that when either one alters it, both
  45. will see it change.
  46. */
  47. Value (const Value& other);
  48. /** Creates a Value that is set to the specified value. */
  49. explicit Value (const var& initialValue);
  50. /** Move constructor */
  51. Value (Value&&) noexcept;
  52. /** Destructor. */
  53. ~Value();
  54. //==============================================================================
  55. /** Returns the current value. */
  56. var getValue() const;
  57. /** Returns the current value. */
  58. operator var() const;
  59. /** Returns the value as a string.
  60. This is a shortcut for "myValue.getValue().toString()".
  61. */
  62. String toString() const;
  63. /** Sets the current value.
  64. You can also use operator= to set the value.
  65. If there are any listeners registered, they will be notified of the
  66. change asynchronously.
  67. */
  68. void setValue (const var& newValue);
  69. /** Sets the current value.
  70. This is the same as calling setValue().
  71. If there are any listeners registered, they will be notified of the
  72. change asynchronously.
  73. */
  74. Value& operator= (const var& newValue);
  75. /** Move assignment operator */
  76. Value& operator= (Value&&) noexcept;
  77. /** Makes this object refer to the same underlying ValueSource as another one.
  78. Once this object has been connected to another one, changing either one
  79. will update the other.
  80. Existing listeners will still be registered after you call this method, and
  81. they'll continue to receive messages when the new value changes.
  82. */
  83. void referTo (const Value& valueToReferTo);
  84. /** Returns true if this value and the other one are references to the same value.
  85. */
  86. bool refersToSameSourceAs (const Value& other) const;
  87. /** Compares two values.
  88. This is a compare-by-value comparison, so is effectively the same as
  89. saying (this->getValue() == other.getValue()).
  90. */
  91. bool operator== (const Value& other) const;
  92. /** Compares two values.
  93. This is a compare-by-value comparison, so is effectively the same as
  94. saying (this->getValue() != other.getValue()).
  95. */
  96. bool operator!= (const Value& other) const;
  97. //==============================================================================
  98. /** Receives callbacks when a Value object changes.
  99. @see Value::addListener
  100. */
  101. class JUCE_API Listener
  102. {
  103. public:
  104. Listener() {}
  105. virtual ~Listener() {}
  106. /** Called when a Value object is changed.
  107. Note that the Value object passed as a parameter may not be exactly the same
  108. object that you registered the listener with - it might be a copy that refers
  109. to the same underlying ValueSource. To find out, you can call Value::refersToSameSourceAs().
  110. */
  111. virtual void valueChanged (Value& value) = 0;
  112. };
  113. /** Adds a listener to receive callbacks when the value changes.
  114. The listener is added to this specific Value object, and not to the shared
  115. object that it refers to. When this object is deleted, all the listeners will
  116. be lost, even if other references to the same Value still exist. So when you're
  117. adding a listener, make sure that you add it to a Value instance that will last
  118. for as long as you need the listener. In general, you'd never want to add a listener
  119. to a local stack-based Value, but more likely to one that's a member variable.
  120. @see removeListener
  121. */
  122. void addListener (Listener* listener);
  123. /** Removes a listener that was previously added with addListener(). */
  124. void removeListener (Listener* listener);
  125. //==============================================================================
  126. /**
  127. Used internally by the Value class as the base class for its shared value objects.
  128. The Value class is essentially a reference-counted pointer to a shared instance
  129. of a ValueSource object. If you're feeling adventurous, you can create your own custom
  130. ValueSource classes to allow Value objects to represent your own custom data items.
  131. */
  132. class JUCE_API ValueSource : public ReferenceCountedObject,
  133. private AsyncUpdater
  134. {
  135. public:
  136. ValueSource();
  137. virtual ~ValueSource();
  138. /** Returns the current value of this object. */
  139. virtual var getValue() const = 0;
  140. /** Changes the current value.
  141. This must also trigger a change message if the value actually changes.
  142. */
  143. virtual void setValue (const var& newValue) = 0;
  144. /** Delivers a change message to all the listeners that are registered with
  145. this value.
  146. If dispatchSynchronously is true, the method will call all the listeners
  147. before returning; otherwise it'll dispatch a message and make the call later.
  148. */
  149. void sendChangeMessage (bool dispatchSynchronously);
  150. protected:
  151. //==============================================================================
  152. friend class Value;
  153. SortedSet<Value*> valuesWithListeners;
  154. private:
  155. void handleAsyncUpdate() override;
  156. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ValueSource)
  157. };
  158. //==============================================================================
  159. /** Creates a Value object that uses this valueSource object as its underlying data. */
  160. explicit Value (ValueSource* valueSource);
  161. /** Returns the ValueSource that this value is referring to. */
  162. ValueSource& getValueSource() noexcept { return *value; }
  163. private:
  164. //==============================================================================
  165. friend class ValueSource;
  166. ReferenceCountedObjectPtr<ValueSource> value;
  167. ListenerList<Listener> listeners;
  168. void callListeners();
  169. void removeFromListenerList();
  170. // This is disallowed to avoid confusion about whether it should
  171. // do a by-value or by-reference copy.
  172. Value& operator= (const Value&) JUCE_DELETED_FUNCTION;
  173. // This declaration prevents accidental construction from an integer of 0,
  174. // which is possible in some compilers via an implicit cast to a pointer.
  175. explicit Value (void*) JUCE_DELETED_FUNCTION;
  176. };
  177. /** Writes a Value to an OutputStream as a UTF8 string. */
  178. OutputStream& JUCE_CALLTYPE operator<< (OutputStream&, const Value&);
  179. /** This typedef is just for compatibility with old code - newer code should use the Value::Listener class directly. */
  180. typedef Value::Listener ValueListener;
  181. } // namespace juce