OpenShot Audio Library | OpenShotAudio 0.4.0
juce_CachedValue.h
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
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 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24*/
25
26namespace juce
27{
28
29//==============================================================================
57template <typename Type>
59{
60public:
61 //==============================================================================
67
78 CachedValue (ValueTree& tree, const Identifier& propertyID,
79 UndoManager* undoManager);
80
91 CachedValue (ValueTree& tree, const Identifier& propertyID,
92 UndoManager* undoManager, const Type& defaultToUse);
93
94 //==============================================================================
100 operator Type() const noexcept { return cachedValue; }
101
105 Type get() const noexcept { return cachedValue; }
106
108 const Type& operator*() const noexcept { return cachedValue; }
109
113 const Type* operator->() const noexcept { return &cachedValue; }
114
118 template <typename OtherType>
119 bool operator== (const OtherType& other) const
120 {
121 return cachedValue == other;
122 }
123
127 template <typename OtherType>
128 bool operator!= (const OtherType& other) const { return ! operator== (other); }
129
130 //==============================================================================
133
137 bool isUsingDefault() const;
138
140 Type getDefault() const { return defaultValue; }
141
142 //==============================================================================
144 CachedValue& operator= (const Type& newValue);
145
147 void setValue (const Type& newValue, UndoManager* undoManagerToUse);
148
152 void resetToDefault();
153
157 void resetToDefault (UndoManager* undoManagerToUse);
158
160 void setDefault (const Type& value) { defaultValue = value; }
161
162 //==============================================================================
164 void referTo (ValueTree& tree, const Identifier& property, UndoManager* um);
165
169 void referTo (ValueTree& tree, const Identifier& property, UndoManager* um, const Type& defaultVal);
170
178
179 //==============================================================================
181 ValueTree& getValueTree() noexcept { return targetTree; }
182
184 const Identifier& getPropertyID() const noexcept { return targetProperty; }
185
187 UndoManager* getUndoManager() noexcept { return undoManager; }
188
189private:
190 //==============================================================================
191 ValueTree targetTree;
192 Identifier targetProperty;
193 UndoManager* undoManager = nullptr;
194 Type defaultValue;
195 Type cachedValue;
196
197 //==============================================================================
198 void referToWithDefault (ValueTree&, const Identifier&, UndoManager*, const Type&);
199 Type getTypedValue() const;
200
201 void valueTreePropertyChanged (ValueTree& changedTree, const Identifier& changedProperty) override;
202
203 //==============================================================================
204 JUCE_DECLARE_WEAK_REFERENCEABLE (CachedValue)
205 JUCE_DECLARE_NON_COPYABLE (CachedValue)
206};
207
208
209//==============================================================================
210template <typename Type>
211inline CachedValue<Type>::CachedValue() = default;
212
213template <typename Type>
215 : targetTree (v), targetProperty (i), undoManager (um),
216 defaultValue(), cachedValue (getTypedValue())
217{
218 targetTree.addListener (this);
219}
220
221template <typename Type>
222inline CachedValue<Type>::CachedValue (ValueTree& v, const Identifier& i, UndoManager* um, const Type& defaultToUse)
223 : targetTree (v), targetProperty (i), undoManager (um),
224 defaultValue (defaultToUse), cachedValue (getTypedValue())
225{
226 targetTree.addListener (this);
227}
228
229template <typename Type>
231{
232 return targetTree.getPropertyAsValue (targetProperty, undoManager);
233}
234
235template <typename Type>
237{
238 return ! targetTree.hasProperty (targetProperty);
239}
240
241template <typename Type>
243{
244 setValue (newValue, undoManager);
245 return *this;
246}
247
248template <typename Type>
249inline void CachedValue<Type>::setValue (const Type& newValue, UndoManager* undoManagerToUse)
250{
251 if (! exactlyEqual (cachedValue, newValue) || isUsingDefault())
252 {
253 cachedValue = newValue;
254 targetTree.setProperty (targetProperty, VariantConverter<Type>::toVar (newValue), undoManagerToUse);
255 }
256}
257
258template <typename Type>
260{
261 resetToDefault (undoManager);
262}
263
264template <typename Type>
265inline void CachedValue<Type>::resetToDefault (UndoManager* undoManagerToUse)
266{
267 targetTree.removeProperty (targetProperty, undoManagerToUse);
268 forceUpdateOfCachedValue();
269}
270
271template <typename Type>
273{
274 referToWithDefault (v, i, um, Type());
275}
276
277template <typename Type>
278inline void CachedValue<Type>::referTo (ValueTree& v, const Identifier& i, UndoManager* um, const Type& defaultVal)
279{
280 referToWithDefault (v, i, um, defaultVal);
281}
282
283template <typename Type>
285{
286 cachedValue = getTypedValue();
287}
288
289template <typename Type>
290inline void CachedValue<Type>::referToWithDefault (ValueTree& v, const Identifier& i, UndoManager* um, const Type& defaultVal)
291{
292 targetTree.removeListener (this);
293 targetTree = v;
294 targetProperty = i;
295 undoManager = um;
296 defaultValue = defaultVal;
297 cachedValue = getTypedValue();
298 targetTree.addListener (this);
299}
300
301template <typename Type>
302inline Type CachedValue<Type>::getTypedValue() const
303{
304 if (const var* property = targetTree.getPropertyPointer (targetProperty))
305 return VariantConverter<Type>::fromVar (*property);
306
307 return defaultValue;
308}
309
310template <typename Type>
311inline void CachedValue<Type>::valueTreePropertyChanged (ValueTree& changedTree, const Identifier& changedProperty)
312{
313 if (changedProperty == targetProperty && targetTree == changedTree)
314 forceUpdateOfCachedValue();
315}
316
317} // namespace juce
bool isUsingDefault() const
Type getDefault() const
ValueTree & getValueTree() noexcept
const Identifier & getPropertyID() const noexcept
CachedValue & operator=(const Type &newValue)
const Type & operator*() const noexcept
const Type * operator->() const noexcept
void setValue(const Type &newValue, UndoManager *undoManagerToUse)
UndoManager * getUndoManager() noexcept
void referTo(ValueTree &tree, const Identifier &property, UndoManager *um)
bool operator==(const OtherType &other) const
void setDefault(const Type &value)
bool operator!=(const OtherType &other) const
Type get() const noexcept
void addListener(Listener *listener)