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.

290 lines
7.2KB

  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. class SharedValueSourceUpdater : public ReferenceCountedObject,
  19. private AsyncUpdater
  20. {
  21. public:
  22. SharedValueSourceUpdater() : insideCallback (false) {}
  23. typedef ReferenceCountedObjectPtr<SharedValueSourceUpdater> Ptr;
  24. void update (Value::ValueSource* source)
  25. {
  26. sourcesToUpdate.add (source);
  27. if (! insideCallback)
  28. triggerAsyncUpdate();
  29. }
  30. static SharedValueSourceUpdater* getOrCreateSharedUpdater()
  31. {
  32. Ptr& p = getSharedUpdater();
  33. if (p == nullptr)
  34. p = new SharedValueSourceUpdater();
  35. return p;
  36. }
  37. static void releaseIfUnused()
  38. {
  39. if (Ptr& p = getSharedUpdater())
  40. if (p->getReferenceCount() == 1)
  41. p = nullptr;
  42. }
  43. private:
  44. ReferenceCountedArray<Value::ValueSource> sourcesToUpdate;
  45. bool insideCallback;
  46. static Ptr& getSharedUpdater()
  47. {
  48. static Ptr updater;
  49. return updater;
  50. }
  51. void handleAsyncUpdate()
  52. {
  53. int maxLoops = 10;
  54. const ScopedValueSetter<bool> inside (insideCallback, true, false);
  55. const Ptr localRef (this);
  56. while (sourcesToUpdate.size() > 0 && --maxLoops >= 0)
  57. {
  58. ReferenceCountedArray<Value::ValueSource> sources;
  59. sources.swapWithArray (sourcesToUpdate);
  60. for (int i = 0; i < sources.size(); ++i)
  61. sources.getObjectPointerUnchecked(i)->sendChangeMessage (true);
  62. }
  63. }
  64. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SharedValueSourceUpdater);
  65. };
  66. Value::ValueSource::ValueSource()
  67. {
  68. }
  69. Value::ValueSource::~ValueSource()
  70. {
  71. asyncUpdater = nullptr;
  72. SharedValueSourceUpdater::releaseIfUnused();
  73. }
  74. void Value::ValueSource::sendChangeMessage (const bool synchronous)
  75. {
  76. const int numListeners = valuesWithListeners.size();
  77. if (numListeners > 0)
  78. {
  79. if (synchronous)
  80. {
  81. asyncUpdater = nullptr;
  82. const ReferenceCountedObjectPtr<ValueSource> localRef (this);
  83. for (int i = numListeners; --i >= 0;)
  84. if (Value* const v = valuesWithListeners[i])
  85. v->callListeners();
  86. }
  87. else if (asyncUpdater == nullptr)
  88. {
  89. SharedValueSourceUpdater* const updater = SharedValueSourceUpdater::getOrCreateSharedUpdater();
  90. asyncUpdater = updater;
  91. updater->update (this);
  92. }
  93. }
  94. }
  95. //==============================================================================
  96. class SimpleValueSource : public Value::ValueSource
  97. {
  98. public:
  99. SimpleValueSource()
  100. {
  101. }
  102. SimpleValueSource (const var& initialValue)
  103. : value (initialValue)
  104. {
  105. }
  106. var getValue() const
  107. {
  108. return value;
  109. }
  110. void setValue (const var& newValue)
  111. {
  112. if (! newValue.equalsWithSameType (value))
  113. {
  114. value = newValue;
  115. sendChangeMessage (false);
  116. }
  117. }
  118. private:
  119. var value;
  120. JUCE_DECLARE_NON_COPYABLE (SimpleValueSource);
  121. };
  122. //==============================================================================
  123. Value::Value()
  124. : value (new SimpleValueSource())
  125. {
  126. }
  127. Value::Value (ValueSource* const value_)
  128. : value (value_)
  129. {
  130. jassert (value_ != nullptr);
  131. }
  132. Value::Value (const var& initialValue)
  133. : value (new SimpleValueSource (initialValue))
  134. {
  135. }
  136. Value::Value (const Value& other)
  137. : value (other.value)
  138. {
  139. }
  140. Value& Value::operator= (const Value& other)
  141. {
  142. value = other.value;
  143. return *this;
  144. }
  145. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  146. Value::Value (Value&& other) noexcept
  147. : value (static_cast <ReferenceCountedObjectPtr <ValueSource>&&> (other.value))
  148. {
  149. }
  150. Value& Value::operator= (Value&& other) noexcept
  151. {
  152. value = static_cast <ReferenceCountedObjectPtr <ValueSource>&&> (other.value);
  153. return *this;
  154. }
  155. #endif
  156. Value::~Value()
  157. {
  158. if (listeners.size() > 0)
  159. value->valuesWithListeners.removeValue (this);
  160. }
  161. //==============================================================================
  162. var Value::getValue() const
  163. {
  164. return value->getValue();
  165. }
  166. Value::operator var() const
  167. {
  168. return value->getValue();
  169. }
  170. void Value::setValue (const var& newValue)
  171. {
  172. value->setValue (newValue);
  173. }
  174. String Value::toString() const
  175. {
  176. return value->getValue().toString();
  177. }
  178. Value& Value::operator= (const var& newValue)
  179. {
  180. value->setValue (newValue);
  181. return *this;
  182. }
  183. void Value::referTo (const Value& valueToReferTo)
  184. {
  185. if (valueToReferTo.value != value)
  186. {
  187. if (listeners.size() > 0)
  188. {
  189. value->valuesWithListeners.removeValue (this);
  190. valueToReferTo.value->valuesWithListeners.add (this);
  191. }
  192. value = valueToReferTo.value;
  193. callListeners();
  194. }
  195. }
  196. bool Value::refersToSameSourceAs (const Value& other) const
  197. {
  198. return value == other.value;
  199. }
  200. bool Value::operator== (const Value& other) const
  201. {
  202. return value == other.value || value->getValue() == other.getValue();
  203. }
  204. bool Value::operator!= (const Value& other) const
  205. {
  206. return value != other.value && value->getValue() != other.getValue();
  207. }
  208. //==============================================================================
  209. void Value::addListener (ValueListener* const listener)
  210. {
  211. if (listener != nullptr)
  212. {
  213. if (listeners.size() == 0)
  214. value->valuesWithListeners.add (this);
  215. listeners.add (listener);
  216. }
  217. }
  218. void Value::removeListener (ValueListener* const listener)
  219. {
  220. listeners.remove (listener);
  221. if (listeners.size() == 0)
  222. value->valuesWithListeners.removeValue (this);
  223. }
  224. void Value::callListeners()
  225. {
  226. Value v (*this); // (create a copy in case this gets deleted by a callback)
  227. listeners.call (&ValueListener::valueChanged, v);
  228. }
  229. OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const Value& value)
  230. {
  231. return stream << value.toString();
  232. }