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

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