The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

234 lines
8.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_VALUE_JUCEHEADER__
  19. #define __JUCE_VALUE_JUCEHEADER__
  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. */
  32. class JUCE_API Value
  33. {
  34. public:
  35. //==============================================================================
  36. /** Creates an empty Value, containing a void var. */
  37. Value();
  38. /** Creates a Value that refers to the same value as another one.
  39. Note that this doesn't make a copy of the other value - both this and the other
  40. Value will share the same underlying value, so that when either one alters it, both
  41. will see it change.
  42. */
  43. Value (const Value& other);
  44. /** Creates a Value that is set to the specified value. */
  45. explicit Value (const var& initialValue);
  46. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  47. Value (Value&&) noexcept;
  48. Value& operator= (Value&&) noexcept;
  49. #endif
  50. /** Destructor. */
  51. ~Value();
  52. //==============================================================================
  53. /** Returns the current value. */
  54. var getValue() const;
  55. /** Returns the current value. */
  56. operator var() const;
  57. /** Returns the value as a string.
  58. This is alternative to writing things like "myValue.getValue().toString()".
  59. */
  60. String toString() const;
  61. /** Sets the current value.
  62. You can also use operator= to set the value.
  63. If there are any listeners registered, they will be notified of the
  64. change asynchronously.
  65. */
  66. void setValue (const var& newValue);
  67. /** Sets the current value.
  68. This is the same as calling setValue().
  69. If there are any listeners registered, they will be notified of the
  70. change asynchronously.
  71. */
  72. Value& operator= (const var& newValue);
  73. /** Makes this object refer to the same underlying ValueSource as another one.
  74. Once this object has been connected to another one, changing either one
  75. will update the other.
  76. Existing listeners will still be registered after you call this method, and
  77. they'll continue to receive messages when the new value changes.
  78. */
  79. void referTo (const Value& valueToReferTo);
  80. /** Returns true if this value and the other one are references to the same value.
  81. */
  82. bool refersToSameSourceAs (const Value& other) const;
  83. /** Compares two values.
  84. This is a compare-by-value comparison, so is effectively the same as
  85. saying (this->getValue() == other.getValue()).
  86. */
  87. bool operator== (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. //==============================================================================
  94. /** Receives callbacks when a Value object changes.
  95. @see Value::addListener
  96. */
  97. class JUCE_API Listener
  98. {
  99. public:
  100. Listener() {}
  101. virtual ~Listener() {}
  102. /** Called when a Value object is changed.
  103. Note that the Value object passed as a parameter may not be exactly the same
  104. object that you registered the listener with - it might be a copy that refers
  105. to the same underlying ValueSource. To find out, you can call Value::refersToSameSourceAs().
  106. */
  107. virtual void valueChanged (Value& value) = 0;
  108. };
  109. /** Adds a listener to receive callbacks when the value changes.
  110. The listener is added to this specific Value object, and not to the shared
  111. object that it refers to. When this object is deleted, all the listeners will
  112. be lost, even if other references to the same Value still exist. So when you're
  113. adding a listener, make sure that you add it to a ValueTree instance that will last
  114. for as long as you need the listener. In general, you'd never want to add a listener
  115. to a local stack-based ValueTree, but more likely to one that's a member variable.
  116. @see removeListener
  117. */
  118. void addListener (Listener* listener);
  119. /** Removes a listener that was previously added with addListener(). */
  120. void removeListener (Listener* listener);
  121. //==============================================================================
  122. /**
  123. Used internally by the Value class as the base class for its shared value objects.
  124. The Value class is essentially a reference-counted pointer to a shared instance
  125. of a ValueSource object. If you're feeling adventurous, you can create your own custom
  126. ValueSource classes to allow Value objects to represent your own custom data items.
  127. */
  128. class JUCE_API ValueSource : public SingleThreadedReferenceCountedObject,
  129. public AsyncUpdater
  130. {
  131. public:
  132. ValueSource();
  133. virtual ~ValueSource();
  134. /** Returns the current value of this object. */
  135. virtual var getValue() const = 0;
  136. /** Changes the current value.
  137. This must also trigger a change message if the value actually changes.
  138. */
  139. virtual void setValue (const var& newValue) = 0;
  140. /** Delivers a change message to all the listeners that are registered with
  141. this value.
  142. If dispatchSynchronously is true, the method will call all the listeners
  143. before returning; otherwise it'll dispatch a message and make the call later.
  144. */
  145. void sendChangeMessage (bool dispatchSynchronously);
  146. protected:
  147. //==============================================================================
  148. friend class Value;
  149. SortedSet <Value*> valuesWithListeners;
  150. void handleAsyncUpdate();
  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_JUCEHEADER__