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.

juce_CachedValue.h 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. //==============================================================================
  22. /**
  23. This class acts as a typed wrapper around a property inside a ValueTree.
  24. A CachedValue provides an easy way to read and write a ValueTree property with
  25. a chosen type. So for example a CachedValue<int> allows you to read or write the
  26. property as an int, and a CachedValue<String> lets you work with it as a String.
  27. It also allows efficient access to the value, by caching a copy of it in the
  28. type that is being used.
  29. You can give the CachedValue an optional UndoManager which it will use when writing
  30. to the underlying ValueTree.
  31. If the property inside the ValueTree is missing, the CachedValue will automatically
  32. return an optional default value, which can be specified when initialising the CachedValue.
  33. To create one, you can either use the constructor to attach the CachedValue to a
  34. ValueTree, or can create an uninitialised CachedValue with its default constructor and
  35. then attach it later with the referTo() methods.
  36. Common types like String, int, double which can be easily converted to a var should work
  37. out-of-the-box, but if you want to use more complex custom types, you may need to implement
  38. some template specialisations of VariantConverter which this class uses to convert between
  39. the type and the ValueTree's internal var.
  40. */
  41. template <typename Type>
  42. class CachedValue : private ValueTree::Listener
  43. {
  44. public:
  45. //==============================================================================
  46. /** Default constructor.
  47. Creates a default CachedValue not referring to any property. To initialise the
  48. object, call one of the referTo() methods.
  49. */
  50. CachedValue();
  51. /** Constructor.
  52. Creates a CachedValue referring to a Value property inside a ValueTree.
  53. If you use this constructor, the fallback value will be a default-constructed
  54. instance of Type.
  55. @param tree The ValueTree containing the property
  56. @param propertyID The identifier of the property
  57. @param undoManager The UndoManager to use when writing to the property
  58. */
  59. CachedValue (ValueTree& tree, const Identifier& propertyID,
  60. UndoManager* undoManager);
  61. /** Constructor.
  62. Creates a default Cached Value referring to a Value property inside a ValueTree,
  63. and specifies a fallback value to use if the property does not exist.
  64. @param tree The ValueTree containing the property
  65. @param propertyID The identifier of the property
  66. @param undoManager The UndoManager to use when writing to the property
  67. @param defaultToUse The fallback default value to use.
  68. */
  69. CachedValue (ValueTree& tree, const Identifier& propertyID,
  70. UndoManager* undoManager, const Type& defaultToUse);
  71. //==============================================================================
  72. /** Returns the current value of the property. If the property does not exist,
  73. returns the fallback default value.
  74. This is the same as calling get().
  75. */
  76. operator Type() const noexcept { return cachedValue; }
  77. /** Returns the current value of the property. If the property does not exist,
  78. returns the fallback default value.
  79. */
  80. Type get() const noexcept { return cachedValue; }
  81. /** Dereference operator. Provides direct access to the property. */
  82. Type& operator*() noexcept { return cachedValue; }
  83. /** Dereference operator. Provides direct access to members of the property
  84. if it is of object type.
  85. */
  86. Type* operator->() noexcept { return &cachedValue; }
  87. /** Returns true if the current value of the property (or the fallback value)
  88. is equal to other.
  89. */
  90. template <typename OtherType>
  91. bool operator== (const OtherType& other) const { return cachedValue == other; }
  92. /** Returns true if the current value of the property (or the fallback value)
  93. is not equal to other.
  94. */
  95. template <typename OtherType>
  96. bool operator!= (const OtherType& other) const { return cachedValue != other; }
  97. //==============================================================================
  98. /** Returns the current property as a Value object. */
  99. Value getPropertyAsValue();
  100. /** Returns true if the current property does not exist and the CachedValue is using
  101. the fallback default value instead.
  102. */
  103. bool isUsingDefault() const;
  104. /** Returns the current fallback default value. */
  105. Type getDefault() const { return defaultValue; }
  106. //==============================================================================
  107. /** Sets the property. This will actually modify the property in the referenced ValueTree. */
  108. CachedValue& operator= (const Type& newValue);
  109. /** Sets the property. This will actually modify the property in the referenced ValueTree. */
  110. void setValue (const Type& newValue, UndoManager* undoManagerToUse);
  111. /** Removes the property from the referenced ValueTree and makes the CachedValue
  112. return the fallback default value instead.
  113. */
  114. void resetToDefault();
  115. /** Removes the property from the referenced ValueTree and makes the CachedValue
  116. return the fallback default value instead.
  117. */
  118. void resetToDefault (UndoManager* undoManagerToUse);
  119. /** Resets the fallback default value. */
  120. void setDefault (const Type& value) { defaultValue = value; }
  121. //==============================================================================
  122. /** Makes the CachedValue refer to the specified property inside the given ValueTree. */
  123. void referTo (ValueTree& tree, const Identifier& property, UndoManager* um);
  124. /** Makes the CachedValue refer to the specified property inside the given ValueTree,
  125. and specifies a fallback value to use if the property does not exist.
  126. */
  127. void referTo (ValueTree& tree, const Identifier& property, UndoManager* um, const Type& defaultVal);
  128. /** Force an update in case the referenced property has been changed from elsewhere.
  129. Note: The CachedValue is a ValueTree::Listener and therefore will be informed of
  130. changes of the referenced property anyway (and update itself). But this may happen
  131. asynchronously. forceUpdateOfCachedValue() forces an update immediately.
  132. */
  133. void forceUpdateOfCachedValue();
  134. //==============================================================================
  135. /** Returns a reference to the ValueTree containing the referenced property. */
  136. ValueTree& getValueTree() noexcept { return targetTree; }
  137. /** Returns the property ID of the referenced property. */
  138. const Identifier& getPropertyID() const noexcept { return targetProperty; }
  139. private:
  140. //==============================================================================
  141. ValueTree targetTree;
  142. Identifier targetProperty;
  143. UndoManager* undoManager;
  144. Type defaultValue;
  145. Type cachedValue;
  146. //==============================================================================
  147. void referToWithDefault (ValueTree&, const Identifier&, UndoManager*, const Type&);
  148. Type getTypedValue() const;
  149. void valueTreePropertyChanged (ValueTree& changedTree, const Identifier& changedProperty) override;
  150. void valueTreeChildAdded (ValueTree&, ValueTree&) override {}
  151. void valueTreeChildRemoved (ValueTree&, ValueTree&, int) override {}
  152. void valueTreeChildOrderChanged (ValueTree&, int, int) override {}
  153. void valueTreeParentChanged (ValueTree&) override {}
  154. JUCE_DECLARE_NON_COPYABLE (CachedValue)
  155. };
  156. //==============================================================================
  157. template <typename Type>
  158. inline CachedValue<Type>::CachedValue() : undoManager (nullptr) {}
  159. template <typename Type>
  160. inline CachedValue<Type>::CachedValue (ValueTree& v, const Identifier& i, UndoManager* um)
  161. : targetTree (v), targetProperty (i), undoManager (um),
  162. defaultValue(), cachedValue (getTypedValue())
  163. {
  164. targetTree.addListener (this);
  165. }
  166. template <typename Type>
  167. inline CachedValue<Type>::CachedValue (ValueTree& v, const Identifier& i, UndoManager* um, const Type& defaultToUse)
  168. : targetTree (v), targetProperty (i), undoManager (um),
  169. defaultValue (defaultToUse), cachedValue (getTypedValue())
  170. {
  171. targetTree.addListener (this);
  172. }
  173. template <typename Type>
  174. inline Value CachedValue<Type>::getPropertyAsValue()
  175. {
  176. return targetTree.getPropertyAsValue (targetProperty, undoManager);
  177. }
  178. template <typename Type>
  179. inline bool CachedValue<Type>::isUsingDefault() const
  180. {
  181. return ! targetTree.hasProperty (targetProperty);
  182. }
  183. template <typename Type>
  184. inline CachedValue<Type>& CachedValue<Type>::operator= (const Type& newValue)
  185. {
  186. setValue (newValue, undoManager);
  187. return *this;
  188. }
  189. template <typename Type>
  190. inline void CachedValue<Type>::setValue (const Type& newValue, UndoManager* undoManagerToUse)
  191. {
  192. if (cachedValue != newValue || isUsingDefault())
  193. {
  194. cachedValue = newValue;
  195. targetTree.setProperty (targetProperty, VariantConverter<Type>::toVar (newValue), undoManagerToUse);
  196. }
  197. }
  198. template <typename Type>
  199. inline void CachedValue<Type>::resetToDefault()
  200. {
  201. resetToDefault (undoManager);
  202. }
  203. template <typename Type>
  204. inline void CachedValue<Type>::resetToDefault (UndoManager* undoManagerToUse)
  205. {
  206. targetTree.removeProperty (targetProperty, undoManagerToUse);
  207. forceUpdateOfCachedValue();
  208. }
  209. template <typename Type>
  210. inline void CachedValue<Type>::referTo (ValueTree& v, const Identifier& i, UndoManager* um)
  211. {
  212. referToWithDefault (v, i, um, Type());
  213. }
  214. template <typename Type>
  215. inline void CachedValue<Type>::referTo (ValueTree& v, const Identifier& i, UndoManager* um, const Type& defaultVal)
  216. {
  217. referToWithDefault (v, i, um, defaultVal);
  218. }
  219. template <typename Type>
  220. inline void CachedValue<Type>::forceUpdateOfCachedValue()
  221. {
  222. cachedValue = getTypedValue();
  223. }
  224. template <typename Type>
  225. inline void CachedValue<Type>::referToWithDefault (ValueTree& v, const Identifier& i, UndoManager* um, const Type& defaultVal)
  226. {
  227. targetTree.removeListener (this);
  228. targetTree = v;
  229. targetProperty = i;
  230. undoManager = um;
  231. defaultValue = defaultVal;
  232. cachedValue = getTypedValue();
  233. targetTree.addListener (this);
  234. }
  235. template <typename Type>
  236. inline Type CachedValue<Type>::getTypedValue() const
  237. {
  238. if (const var* property = targetTree.getPropertyPointer (targetProperty))
  239. return VariantConverter<Type>::fromVar (*property);
  240. return defaultValue;
  241. }
  242. template <typename Type>
  243. inline void CachedValue<Type>::valueTreePropertyChanged (ValueTree& changedTree, const Identifier& changedProperty)
  244. {
  245. if (changedProperty == targetProperty && targetTree == changedTree)
  246. forceUpdateOfCachedValue();
  247. }
  248. } // namespace juce