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.

226 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. 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. 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. Value::operator const var() const
  113. {
  114. return getValue();
  115. }
  116. void Value::setValue (const var& newValue)
  117. {
  118. value->setValue (newValue);
  119. }
  120. const String Value::toString() const
  121. {
  122. return value->getValue().toString();
  123. }
  124. Value& Value::operator= (const var& newValue)
  125. {
  126. value->setValue (newValue);
  127. return *this;
  128. }
  129. void Value::referTo (const Value& valueToReferTo)
  130. {
  131. if (valueToReferTo.value != value)
  132. {
  133. if (listeners.size() > 0)
  134. {
  135. value->valuesWithListeners.removeValue (this);
  136. valueToReferTo.value->valuesWithListeners.add (this);
  137. }
  138. value = valueToReferTo.value;
  139. callListeners();
  140. }
  141. }
  142. bool Value::refersToSameSourceAs (const Value& other) const
  143. {
  144. return value == other.value;
  145. }
  146. bool Value::operator== (const Value& other) const
  147. {
  148. return value == other.value || value->getValue() == other.getValue();
  149. }
  150. bool Value::operator!= (const Value& other) const
  151. {
  152. return value != other.value && value->getValue() != other.getValue();
  153. }
  154. //==============================================================================
  155. void Value::addListener (Listener* const listener)
  156. {
  157. if (listener != 0)
  158. {
  159. if (listeners.size() == 0)
  160. value->valuesWithListeners.add (this);
  161. listeners.add (listener);
  162. }
  163. }
  164. void Value::removeListener (Listener* const listener)
  165. {
  166. listeners.remove (listener);
  167. if (listeners.size() == 0)
  168. value->valuesWithListeners.removeValue (this);
  169. }
  170. void Value::callListeners()
  171. {
  172. Value v (*this); // (create a copy in case this gets deleted by a callback)
  173. listeners.call (&Listener::valueChanged, v);
  174. }
  175. OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const Value& value)
  176. {
  177. return stream << value.toString();
  178. }
  179. END_JUCE_NAMESPACE