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

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