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.

223 lines
5.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-9 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. #include "../core/juce_StandardHeader.h"
  19. BEGIN_JUCE_NAMESPACE
  20. #include "juce_Value.h"
  21. //==============================================================================
  22. Value::ValueSource::ValueSource()
  23. {
  24. }
  25. Value::ValueSource::~ValueSource()
  26. {
  27. }
  28. void Value::ValueSource::sendChangeMessage (const bool synchronous)
  29. {
  30. if (synchronous)
  31. {
  32. for (int i = valuesWithListeners.size(); --i >= 0;)
  33. {
  34. Value* const v = valuesWithListeners[i];
  35. if (v != 0)
  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. ~SimpleValueSource()
  60. {
  61. }
  62. const var getValue() const
  63. {
  64. return value;
  65. }
  66. void setValue (const var& newValue)
  67. {
  68. if (newValue != value)
  69. {
  70. value = newValue;
  71. sendChangeMessage (false);
  72. }
  73. }
  74. private:
  75. var value;
  76. SimpleValueSource (const SimpleValueSource&);
  77. const SimpleValueSource& operator= (const SimpleValueSource&);
  78. };
  79. //==============================================================================
  80. Value::Value()
  81. : value (new SimpleValueSource())
  82. {
  83. }
  84. Value::Value (ValueSource* const value_)
  85. : value (value_)
  86. {
  87. jassert (value_ != 0);
  88. }
  89. Value::Value (const var& initialValue)
  90. : value (new SimpleValueSource (initialValue))
  91. {
  92. }
  93. Value::Value (const Value& other)
  94. : value (other.value)
  95. {
  96. }
  97. const Value& Value::operator= (const Value& other)
  98. {
  99. value = other.value;
  100. return *this;
  101. }
  102. Value::~Value()
  103. {
  104. if (listeners.size() > 0)
  105. value->valuesWithListeners.removeValue (this);
  106. }
  107. //==============================================================================
  108. const var Value::getValue() const
  109. {
  110. return value->getValue();
  111. }
  112. void Value::setValue (const var& newValue)
  113. {
  114. value->setValue (newValue);
  115. }
  116. const String Value::toString() const
  117. {
  118. return value->getValue().toString();
  119. }
  120. const Value& Value::operator= (const var& newValue)
  121. {
  122. value->setValue (newValue);
  123. return *this;
  124. }
  125. void Value::referTo (const Value& valueToReferTo)
  126. {
  127. if (valueToReferTo.value != value)
  128. {
  129. if (listeners.size() > 0)
  130. {
  131. value->valuesWithListeners.removeValue (this);
  132. valueToReferTo.value->valuesWithListeners.add (this);
  133. }
  134. value = valueToReferTo.value;
  135. callListeners();
  136. }
  137. }
  138. bool Value::refersToSameSourceAs (const Value& other) const
  139. {
  140. return value == other.value;
  141. }
  142. bool Value::operator== (const Value& other) const
  143. {
  144. return value == other.value || value->getValue() == other.getValue();
  145. }
  146. bool Value::operator!= (const Value& other) const
  147. {
  148. return value != other.value && value->getValue() != other.getValue();
  149. }
  150. //==============================================================================
  151. void Value::addListener (Listener* const listener)
  152. {
  153. if (listener != 0)
  154. {
  155. if (listeners.size() == 0)
  156. value->valuesWithListeners.add (this);
  157. listeners.add (listener);
  158. }
  159. }
  160. void Value::removeListener (Listener* const listener)
  161. {
  162. listeners.removeValue (listener);
  163. if (listeners.size() == 0)
  164. value->valuesWithListeners.removeValue (this);
  165. }
  166. void Value::callListeners()
  167. {
  168. Value valueCopy (*this); // Use a copy in case this object gets deleted by a callback
  169. for (int i = listeners.size(); --i >= 0;)
  170. {
  171. Listener* const l = listeners[i];
  172. if (l != 0)
  173. l->valueChanged (valueCopy);
  174. }
  175. }
  176. END_JUCE_NAMESPACE