OpenShot Audio Library | OpenShotAudio  0.3.1
juce_CachedValue.h
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2017 - ROLI Ltd.
6 
7  JUCE is an open source library subject to commercial or open-source
8  licensing.
9 
10  By using JUCE, you agree to the terms of both the JUCE 5 End-User License
11  Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
12  27th April 2017).
13 
14  End User License Agreement: www.juce.com/juce-5-licence
15  Privacy Policy: www.juce.com/juce-5-privacy-policy
16 
17  Or: You may also use this code under the terms of the GPL v3 (see
18  www.gnu.org/licenses).
19 
20  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
21  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
22  DISCLAIMED.
23 
24  ==============================================================================
25 */
26 
27 namespace juce
28 {
29 
30 //==============================================================================
58 template <typename Type>
60 {
61 public:
62  //==============================================================================
67  CachedValue();
68 
79  CachedValue (ValueTree& tree, const Identifier& propertyID,
80  UndoManager* undoManager);
81 
92  CachedValue (ValueTree& tree, const Identifier& propertyID,
93  UndoManager* undoManager, const Type& defaultToUse);
94 
95  //==============================================================================
101  operator Type() const noexcept { return cachedValue; }
102 
106  Type get() const noexcept { return cachedValue; }
107 
109  Type& operator*() noexcept { return cachedValue; }
110 
114  Type* operator->() noexcept { return &cachedValue; }
115 
119  template <typename OtherType>
120  bool operator== (const OtherType& other) const { return cachedValue == other; }
121 
125  template <typename OtherType>
126  bool operator!= (const OtherType& other) const { return cachedValue != other; }
127 
128  //==============================================================================
131 
135  bool isUsingDefault() const;
136 
138  Type getDefault() const { return defaultValue; }
139 
140  //==============================================================================
142  CachedValue& operator= (const Type& newValue);
143 
145  void setValue (const Type& newValue, UndoManager* undoManagerToUse);
146 
150  void resetToDefault();
151 
155  void resetToDefault (UndoManager* undoManagerToUse);
156 
158  void setDefault (const Type& value) { defaultValue = value; }
159 
160  //==============================================================================
162  void referTo (ValueTree& tree, const Identifier& property, UndoManager* um);
163 
167  void referTo (ValueTree& tree, const Identifier& property, UndoManager* um, const Type& defaultVal);
168 
176 
177  //==============================================================================
179  ValueTree& getValueTree() noexcept { return targetTree; }
180 
182  const Identifier& getPropertyID() const noexcept { return targetProperty; }
183 
185  UndoManager* getUndoManager() noexcept { return undoManager; }
186 
187 private:
188  //==============================================================================
189  ValueTree targetTree;
190  Identifier targetProperty;
191  UndoManager* undoManager = nullptr;
192  Type defaultValue;
193  Type cachedValue;
194 
195  //==============================================================================
196  void referToWithDefault (ValueTree&, const Identifier&, UndoManager*, const Type&);
197  Type getTypedValue() const;
198 
199  void valueTreePropertyChanged (ValueTree& changedTree, const Identifier& changedProperty) override;
200 
201  //==============================================================================
202  JUCE_DECLARE_WEAK_REFERENCEABLE (CachedValue)
203  JUCE_DECLARE_NON_COPYABLE (CachedValue)
204 };
205 
206 
207 //==============================================================================
208 template <typename Type>
209 inline CachedValue<Type>::CachedValue() = default;
210 
211 template <typename Type>
213  : targetTree (v), targetProperty (i), undoManager (um),
214  defaultValue(), cachedValue (getTypedValue())
215 {
216  targetTree.addListener (this);
217 }
218 
219 template <typename Type>
220 inline CachedValue<Type>::CachedValue (ValueTree& v, const Identifier& i, UndoManager* um, const Type& defaultToUse)
221  : targetTree (v), targetProperty (i), undoManager (um),
222  defaultValue (defaultToUse), cachedValue (getTypedValue())
223 {
224  targetTree.addListener (this);
225 }
226 
227 template <typename Type>
229 {
230  return targetTree.getPropertyAsValue (targetProperty, undoManager);
231 }
232 
233 template <typename Type>
235 {
236  return ! targetTree.hasProperty (targetProperty);
237 }
238 
239 template <typename Type>
241 {
242  setValue (newValue, undoManager);
243  return *this;
244 }
245 
246 template <typename Type>
247 inline void CachedValue<Type>::setValue (const Type& newValue, UndoManager* undoManagerToUse)
248 {
249  if (cachedValue != newValue || isUsingDefault())
250  {
251  cachedValue = newValue;
252  targetTree.setProperty (targetProperty, VariantConverter<Type>::toVar (newValue), undoManagerToUse);
253  }
254 }
255 
256 template <typename Type>
258 {
259  resetToDefault (undoManager);
260 }
261 
262 template <typename Type>
263 inline void CachedValue<Type>::resetToDefault (UndoManager* undoManagerToUse)
264 {
265  targetTree.removeProperty (targetProperty, undoManagerToUse);
267 }
268 
269 template <typename Type>
271 {
272  referToWithDefault (v, i, um, Type());
273 }
274 
275 template <typename Type>
276 inline void CachedValue<Type>::referTo (ValueTree& v, const Identifier& i, UndoManager* um, const Type& defaultVal)
277 {
278  referToWithDefault (v, i, um, defaultVal);
279 }
280 
281 template <typename Type>
283 {
284  cachedValue = getTypedValue();
285 }
286 
287 template <typename Type>
288 inline void CachedValue<Type>::referToWithDefault (ValueTree& v, const Identifier& i, UndoManager* um, const Type& defaultVal)
289 {
290  targetTree.removeListener (this);
291  targetTree = v;
292  targetProperty = i;
293  undoManager = um;
294  defaultValue = defaultVal;
295  cachedValue = getTypedValue();
296  targetTree.addListener (this);
297 }
298 
299 template <typename Type>
300 inline Type CachedValue<Type>::getTypedValue() const
301 {
302  if (const var* property = targetTree.getPropertyPointer (targetProperty))
303  return VariantConverter<Type>::fromVar (*property);
304 
305  return defaultValue;
306 }
307 
308 template <typename Type>
309 inline void CachedValue<Type>::valueTreePropertyChanged (ValueTree& changedTree, const Identifier& changedProperty)
310 {
311  if (changedProperty == targetProperty && targetTree == changedTree)
313 }
314 
315 } // namespace juce
void setDefault(const Type &value)
Type * operator->() noexcept
void addListener(Listener *listener)
Value getPropertyAsValue(const Identifier &name, UndoManager *undoManager, bool shouldUpdateSynchronously=false)
CachedValue & operator=(const Type &newValue)
void referTo(ValueTree &tree, const Identifier &property, UndoManager *um)
void setValue(const Type &newValue, UndoManager *undoManagerToUse)
bool isUsingDefault() const
void removeListener(Listener *listener)
bool operator!=(const OtherType &other) const
Type & operator*() noexcept
const Identifier & getPropertyID() const noexcept
ValueTree & setProperty(const Identifier &name, const var &newValue, UndoManager *undoManager)
Type getDefault() const
ValueTree & getValueTree() noexcept
const var * getPropertyPointer(const Identifier &name) const noexcept
void removeProperty(const Identifier &name, UndoManager *undoManager)
bool operator==(const OtherType &other) const
UndoManager * getUndoManager() noexcept
bool hasProperty(const Identifier &name) const noexcept