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.

233 lines
5.5KB

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