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.h 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. //==============================================================================
  21. /**
  22. Represents a shared variant value.
  23. A Value object contains a reference to a var object, and can get and set its value.
  24. Listeners can be attached to be told when the value is changed.
  25. The Value class is a wrapper around a shared, reference-counted underlying data
  26. object - this means that multiple Value objects can all refer to the same piece of
  27. data, allowing all of them to be notified when any of them changes it.
  28. When you create a Value with its default constructor, it acts as a wrapper around a
  29. simple var object, but by creating a Value that refers to a custom subclass of ValueSource,
  30. you can map the Value onto any kind of underlying data.
  31. Important note! The Value class is not thread-safe! If you're accessing one from
  32. multiple threads, then you'll need to use your own synchronisation around any code
  33. that accesses it.
  34. @tags{DataStructures}
  35. */
  36. class JUCE_API Value final
  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 object and the other one use the same underlying
  85. ValueSource object.
  86. */
  87. bool refersToSameSourceAs (const Value& other) const;
  88. /** Compares two values.
  89. This is a compare-by-value comparison, so is effectively the same as
  90. saying (this->getValue() == other.getValue()).
  91. */
  92. bool operator== (const Value& other) const;
  93. /** Compares two values.
  94. This is a compare-by-value comparison, so is effectively the same as
  95. saying (this->getValue() != other.getValue()).
  96. */
  97. bool operator!= (const Value& other) const;
  98. //==============================================================================
  99. /** Receives callbacks when a Value object changes.
  100. @see Value::addListener
  101. */
  102. class JUCE_API Listener
  103. {
  104. public:
  105. Listener() = default;
  106. virtual ~Listener() = default;
  107. /** Called when a Value object is changed.
  108. Note that the Value object passed as a parameter may not be exactly the same
  109. object that you registered the listener with - it might be a copy that refers
  110. to the same underlying ValueSource. To find out, you can call Value::refersToSameSourceAs().
  111. */
  112. virtual void valueChanged (Value& value) = 0;
  113. };
  114. /** Adds a listener to receive callbacks when the value changes.
  115. The listener is added to this specific Value object, and not to the shared
  116. object that it refers to. When this object is deleted, all the listeners will
  117. be lost, even if other references to the same Value still exist. So when you're
  118. adding a listener, make sure that you add it to a Value instance that will last
  119. for as long as you need the listener. In general, you'd never want to add a listener
  120. to a local stack-based Value, but more likely to one that's a member variable.
  121. @see removeListener
  122. */
  123. void addListener (Listener* listener);
  124. /** Removes a listener that was previously added with addListener(). */
  125. void removeListener (Listener* listener);
  126. //==============================================================================
  127. /**
  128. Used internally by the Value class as the base class for its shared value objects.
  129. The Value class is essentially a reference-counted pointer to a shared instance
  130. of a ValueSource object. If you're feeling adventurous, you can create your own custom
  131. ValueSource classes to allow Value objects to represent your own custom data items.
  132. */
  133. class JUCE_API ValueSource : public ReferenceCountedObject,
  134. private AsyncUpdater
  135. {
  136. public:
  137. ValueSource();
  138. ~ValueSource() override;
  139. /** Returns the current value of this object. */
  140. virtual var getValue() const = 0;
  141. /** Changes the current value.
  142. This must also trigger a change message if the value actually changes.
  143. */
  144. virtual void setValue (const var& newValue) = 0;
  145. /** Delivers a change message to all the listeners that are registered with
  146. this value.
  147. If dispatchSynchronously is true, the method will call all the listeners
  148. before returning; otherwise it'll dispatch a message and make the call later.
  149. */
  150. void sendChangeMessage (bool dispatchSynchronously);
  151. protected:
  152. //==============================================================================
  153. friend class Value;
  154. SortedSet<Value*> valuesWithListeners;
  155. private:
  156. void handleAsyncUpdate() override;
  157. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ValueSource)
  158. };
  159. //==============================================================================
  160. /** Creates a Value object that uses this valueSource object as its underlying data. */
  161. explicit Value (ValueSource* valueSource);
  162. /** Returns the ValueSource that this value is referring to. */
  163. ValueSource& getValueSource() noexcept { return *value; }
  164. private:
  165. //==============================================================================
  166. friend class ValueSource;
  167. ReferenceCountedObjectPtr<ValueSource> value;
  168. ListenerList<Listener> listeners;
  169. void callListeners();
  170. void removeFromListenerList();
  171. // This is disallowed to avoid confusion about whether it should
  172. // do a by-value or by-reference copy.
  173. Value& operator= (const Value&) = delete;
  174. // This declaration prevents accidental construction from an integer of 0,
  175. // which is possible in some compilers via an implicit cast to a pointer.
  176. explicit Value (void*) = delete;
  177. };
  178. /** Writes a Value to an OutputStream as a UTF8 string. */
  179. OutputStream& JUCE_CALLTYPE operator<< (OutputStream&, const Value&);
  180. } // namespace juce