Audio plugin host https://kx.studio/carla
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.

305 lines
7.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. class SharedValueSourceUpdater : public ReferenceCountedObject,
  18. private AsyncUpdater
  19. {
  20. public:
  21. SharedValueSourceUpdater() : sourcesBeingIterated (nullptr) {}
  22. ~SharedValueSourceUpdater() { masterReference.clear(); }
  23. void update (Value::ValueSource* const source)
  24. {
  25. sourcesNeedingAnUpdate.add (source);
  26. if (sourcesBeingIterated == nullptr)
  27. triggerAsyncUpdate();
  28. }
  29. void valueDeleted (Value::ValueSource* const source)
  30. {
  31. sourcesNeedingAnUpdate.removeValue (source);
  32. if (sourcesBeingIterated != nullptr)
  33. sourcesBeingIterated->removeValue (source);
  34. }
  35. WeakReference<SharedValueSourceUpdater>::Master masterReference;
  36. private:
  37. typedef SortedSet<Value::ValueSource*> SourceSet;
  38. SourceSet sourcesNeedingAnUpdate;
  39. SourceSet* sourcesBeingIterated;
  40. void handleAsyncUpdate() override
  41. {
  42. const ReferenceCountedObjectPtr<SharedValueSourceUpdater> localRef (this);
  43. {
  44. const ScopedValueSetter<SourceSet*> inside (sourcesBeingIterated, nullptr, nullptr);
  45. int maxLoops = 10;
  46. while (sourcesNeedingAnUpdate.size() > 0)
  47. {
  48. if (--maxLoops == 0)
  49. {
  50. triggerAsyncUpdate();
  51. break;
  52. }
  53. SourceSet sources;
  54. sources.swapWith (sourcesNeedingAnUpdate);
  55. sourcesBeingIterated = &sources;
  56. for (int i = sources.size(); --i >= 0;)
  57. if (i < sources.size())
  58. sources.getUnchecked(i)->sendChangeMessage (true);
  59. }
  60. }
  61. }
  62. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SharedValueSourceUpdater)
  63. };
  64. static WeakReference<SharedValueSourceUpdater> sharedUpdater;
  65. Value::ValueSource::ValueSource()
  66. {
  67. }
  68. Value::ValueSource::~ValueSource()
  69. {
  70. if (asyncUpdater != nullptr)
  71. static_cast <SharedValueSourceUpdater*> (asyncUpdater.get())->valueDeleted (this);
  72. }
  73. void Value::ValueSource::sendChangeMessage (const bool synchronous)
  74. {
  75. const int numListeners = valuesWithListeners.size();
  76. if (numListeners > 0)
  77. {
  78. if (synchronous)
  79. {
  80. const ReferenceCountedObjectPtr<ValueSource> localRef (this);
  81. asyncUpdater = nullptr;
  82. for (int i = numListeners; --i >= 0;)
  83. if (Value* const v = valuesWithListeners[i])
  84. v->callListeners();
  85. }
  86. else
  87. {
  88. SharedValueSourceUpdater* updater = static_cast <SharedValueSourceUpdater*> (asyncUpdater.get());
  89. if (updater == nullptr)
  90. {
  91. if (sharedUpdater == nullptr)
  92. {
  93. asyncUpdater = updater = new SharedValueSourceUpdater();
  94. sharedUpdater = updater;
  95. }
  96. else
  97. {
  98. asyncUpdater = updater = sharedUpdater.get();
  99. }
  100. }
  101. updater->update (this);
  102. }
  103. }
  104. }
  105. //==============================================================================
  106. class SimpleValueSource : public Value::ValueSource
  107. {
  108. public:
  109. SimpleValueSource()
  110. {
  111. }
  112. SimpleValueSource (const var& initialValue)
  113. : value (initialValue)
  114. {
  115. }
  116. var getValue() const
  117. {
  118. return value;
  119. }
  120. void setValue (const var& newValue)
  121. {
  122. if (! newValue.equalsWithSameType (value))
  123. {
  124. value = newValue;
  125. sendChangeMessage (false);
  126. }
  127. }
  128. private:
  129. var value;
  130. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SimpleValueSource)
  131. };
  132. //==============================================================================
  133. Value::Value()
  134. : value (new SimpleValueSource())
  135. {
  136. }
  137. Value::Value (ValueSource* const v)
  138. : value (v)
  139. {
  140. jassert (v != nullptr);
  141. }
  142. Value::Value (const var& initialValue)
  143. : value (new SimpleValueSource (initialValue))
  144. {
  145. }
  146. Value::Value (const Value& other)
  147. : value (other.value)
  148. {
  149. }
  150. Value& Value::operator= (const Value& other)
  151. {
  152. value = other.value;
  153. return *this;
  154. }
  155. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  156. Value::Value (Value&& other) noexcept
  157. : value (static_cast <ReferenceCountedObjectPtr <ValueSource>&&> (other.value))
  158. {
  159. }
  160. Value& Value::operator= (Value&& other) noexcept
  161. {
  162. value = static_cast <ReferenceCountedObjectPtr <ValueSource>&&> (other.value);
  163. return *this;
  164. }
  165. #endif
  166. Value::~Value()
  167. {
  168. if (listeners.size() > 0)
  169. value->valuesWithListeners.removeValue (this);
  170. }
  171. //==============================================================================
  172. var Value::getValue() const
  173. {
  174. return value->getValue();
  175. }
  176. Value::operator var() const
  177. {
  178. return value->getValue();
  179. }
  180. void Value::setValue (const var& newValue)
  181. {
  182. value->setValue (newValue);
  183. }
  184. String Value::toString() const
  185. {
  186. return value->getValue().toString();
  187. }
  188. Value& Value::operator= (const var& newValue)
  189. {
  190. value->setValue (newValue);
  191. return *this;
  192. }
  193. void Value::referTo (const Value& valueToReferTo)
  194. {
  195. if (valueToReferTo.value != value)
  196. {
  197. if (listeners.size() > 0)
  198. {
  199. value->valuesWithListeners.removeValue (this);
  200. valueToReferTo.value->valuesWithListeners.add (this);
  201. }
  202. value = valueToReferTo.value;
  203. callListeners();
  204. }
  205. }
  206. bool Value::refersToSameSourceAs (const Value& other) const
  207. {
  208. return value == other.value;
  209. }
  210. bool Value::operator== (const Value& other) const
  211. {
  212. return value == other.value || value->getValue() == other.getValue();
  213. }
  214. bool Value::operator!= (const Value& other) const
  215. {
  216. return value != other.value && value->getValue() != other.getValue();
  217. }
  218. //==============================================================================
  219. void Value::addListener (ValueListener* const listener)
  220. {
  221. if (listener != nullptr)
  222. {
  223. if (listeners.size() == 0)
  224. value->valuesWithListeners.add (this);
  225. listeners.add (listener);
  226. }
  227. }
  228. void Value::removeListener (ValueListener* const listener)
  229. {
  230. listeners.remove (listener);
  231. if (listeners.size() == 0)
  232. value->valuesWithListeners.removeValue (this);
  233. }
  234. void Value::callListeners()
  235. {
  236. if (listeners.size() > 0)
  237. {
  238. Value v (*this); // (create a copy in case this gets deleted by a callback)
  239. listeners.call (&ValueListener::valueChanged, v);
  240. }
  241. }
  242. OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const Value& value)
  243. {
  244. return stream << value.toString();
  245. }