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.

308 lines
12KB

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