OpenShot Audio Library | OpenShotAudio 0.4.0

Loading...
Searching...
No Matches
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
26 namespace juce
27{
28
29//==============================================================================
57 template <typename Type>
58 class CachedValue : private ValueTree::Listener
59{
60 public:
61 //==============================================================================
66 CachedValue();
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 //==============================================================================
132 Value getPropertyAsValue();
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
177 void forceUpdateOfCachedValue();
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
189 private:
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//==============================================================================
210 template <typename Type>
211 inline CachedValue<Type>::CachedValue() = default;
212
213 template <typename Type>
214 inline CachedValue<Type>::CachedValue (ValueTree& v, const Identifier& i, UndoManager* um)
215 : targetTree (v), targetProperty (i), undoManager (um),
216 defaultValue(), cachedValue (getTypedValue())
217{
218 targetTree.addListener (this);
219}
220
221 template <typename Type>
222 inline 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
229 template <typename Type>
230 inline Value CachedValue<Type>::getPropertyAsValue()
231{
232 return targetTree.getPropertyAsValue (targetProperty, undoManager);
233}
234
235 template <typename Type>
236 inline bool CachedValue<Type>::isUsingDefault() const
237{
238 return ! targetTree.hasProperty (targetProperty);
239}
240
241 template <typename Type>
242 inline CachedValue<Type>& CachedValue<Type>::operator= (const Type& newValue)
243{
244 setValue (newValue, undoManager);
245 return *this;
246}
247
248 template <typename Type>
249 inline 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
258 template <typename Type>
259 inline void CachedValue<Type>::resetToDefault()
260{
261 resetToDefault (undoManager);
262}
263
264 template <typename Type>
265 inline void CachedValue<Type>::resetToDefault (UndoManager* undoManagerToUse)
266{
267 targetTree.removeProperty (targetProperty, undoManagerToUse);
268 forceUpdateOfCachedValue();
269}
270
271 template <typename Type>
272 inline void CachedValue<Type>::referTo (ValueTree& v, const Identifier& i, UndoManager* um)
273{
274 referToWithDefault (v, i, um, Type());
275}
276
277 template <typename Type>
278 inline void CachedValue<Type>::referTo (ValueTree& v, const Identifier& i, UndoManager* um, const Type& defaultVal)
279{
280 referToWithDefault (v, i, um, defaultVal);
281}
282
283 template <typename Type>
284 inline void CachedValue<Type>::forceUpdateOfCachedValue()
285{
286 cachedValue = getTypedValue();
287}
288
289 template <typename Type>
290 inline 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
301 template <typename Type>
302 inline 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
310 template <typename Type>
311 inline void CachedValue<Type>::valueTreePropertyChanged (ValueTree& changedTree, const Identifier& changedProperty)
312{
313 if (changedProperty == targetProperty && targetTree == changedTree)
314 forceUpdateOfCachedValue();
315}
316
317} // namespace juce
juce::CachedValue
Definition juce_CachedValue.h:59
juce::CachedValue::isUsingDefault
bool isUsingDefault() const
Definition juce_CachedValue.h:236
juce::CachedValue::getDefault
Type getDefault() const
Definition juce_CachedValue.h:140
juce::CachedValue::CachedValue
CachedValue()
juce::CachedValue::getValueTree
ValueTree & getValueTree() noexcept
Definition juce_CachedValue.h:181
juce::CachedValue::getPropertyID
const Identifier & getPropertyID() const noexcept
Definition juce_CachedValue.h:184
juce::CachedValue::operator=
CachedValue & operator=(const Type &newValue)
Definition juce_CachedValue.h:242
juce::CachedValue::forceUpdateOfCachedValue
void forceUpdateOfCachedValue()
Definition juce_CachedValue.h:284
juce::CachedValue::operator*
const Type & operator*() const noexcept
Definition juce_CachedValue.h:108
juce::CachedValue::operator->
const Type * operator->() const noexcept
Definition juce_CachedValue.h:113
juce::CachedValue::setValue
void setValue(const Type &newValue, UndoManager *undoManagerToUse)
Definition juce_CachedValue.h:249
juce::CachedValue::getUndoManager
UndoManager * getUndoManager() noexcept
Definition juce_CachedValue.h:187
juce::CachedValue::resetToDefault
void resetToDefault()
Definition juce_CachedValue.h:259
juce::CachedValue::referTo
void referTo(ValueTree &tree, const Identifier &property, UndoManager *um)
Definition juce_CachedValue.h:272
juce::CachedValue::getPropertyAsValue
Value getPropertyAsValue()
Definition juce_CachedValue.h:230
juce::CachedValue::operator==
bool operator==(const OtherType &other) const
Definition juce_CachedValue.h:119
juce::CachedValue::setDefault
void setDefault(const Type &value)
Definition juce_CachedValue.h:160
juce::CachedValue::operator!=
bool operator!=(const OtherType &other) const
Definition juce_CachedValue.h:128
juce::CachedValue::get
Type get() const noexcept
Definition juce_CachedValue.h:105
juce::Identifier
Definition juce_Identifier.h:39
juce::UndoManager
Definition juce_UndoManager.h:52
juce::ValueTree::Listener
Definition juce_ValueTree.h:481
juce::ValueTree
Definition juce_ValueTree.h:72
juce::ValueTree::addListener
void addListener(Listener *listener)
Definition juce_ValueTree.cpp:969
juce::Value
Definition juce_Value.h:51
juce::VariantConverter
Definition juce_JSONSerialisation.h:478