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.

229 lines
5.4KB

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