OpenShot Audio Library | OpenShotAudio 0.3.2

Loading...
Searching...
No Matches
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>
59 class CachedValue : private ValueTree::Listener
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 //==============================================================================
130 Value getPropertyAsValue();
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
175 void forceUpdateOfCachedValue();
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>
212 inline CachedValue<Type>::CachedValue (ValueTree& v, const Identifier& i, UndoManager* um)
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>
228 inline Value CachedValue<Type>::getPropertyAsValue()
229{
230 return targetTree.getPropertyAsValue (targetProperty, undoManager);
231}
232
233 template <typename Type>
234 inline bool CachedValue<Type>::isUsingDefault() const
235{
236 return ! targetTree.hasProperty (targetProperty);
237}
238
239 template <typename Type>
240 inline CachedValue<Type>& CachedValue<Type>::operator= (const Type& newValue)
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>
257 inline void CachedValue<Type>::resetToDefault()
258{
259 resetToDefault (undoManager);
260}
261
262 template <typename Type>
263 inline void CachedValue<Type>::resetToDefault (UndoManager* undoManagerToUse)
264{
265 targetTree.removeProperty (targetProperty, undoManagerToUse);
266 forceUpdateOfCachedValue();
267}
268
269 template <typename Type>
270 inline void CachedValue<Type>::referTo (ValueTree& v, const Identifier& i, UndoManager* um)
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>
282 inline void CachedValue<Type>::forceUpdateOfCachedValue()
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)
312 forceUpdateOfCachedValue();
313}
314
315} // namespace juce
juce::CachedValue
Definition: juce_CachedValue.h:60
juce::CachedValue::isUsingDefault
bool isUsingDefault() const
Definition: juce_CachedValue.h:234
juce::CachedValue::operator->
Type * operator->() noexcept
Definition: juce_CachedValue.h:114
juce::CachedValue::getDefault
Type getDefault() const
Definition: juce_CachedValue.h:138
juce::CachedValue::CachedValue
CachedValue()
juce::CachedValue::getValueTree
ValueTree & getValueTree() noexcept
Definition: juce_CachedValue.h:179
juce::CachedValue::getPropertyID
const Identifier & getPropertyID() const noexcept
Definition: juce_CachedValue.h:182
juce::CachedValue::operator=
CachedValue & operator=(const Type &newValue)
Definition: juce_CachedValue.h:240
juce::CachedValue::forceUpdateOfCachedValue
void forceUpdateOfCachedValue()
Definition: juce_CachedValue.h:282
juce::CachedValue::setValue
void setValue(const Type &newValue, UndoManager *undoManagerToUse)
Definition: juce_CachedValue.h:247
juce::CachedValue::getUndoManager
UndoManager * getUndoManager() noexcept
Definition: juce_CachedValue.h:185
juce::CachedValue::resetToDefault
void resetToDefault()
Definition: juce_CachedValue.h:257
juce::CachedValue::referTo
void referTo(ValueTree &tree, const Identifier &property, UndoManager *um)
Definition: juce_CachedValue.h:270
juce::CachedValue::getPropertyAsValue
Value getPropertyAsValue()
Definition: juce_CachedValue.h:228
juce::CachedValue::operator==
bool operator==(const OtherType &other) const
Definition: juce_CachedValue.h:120
juce::CachedValue::setDefault
void setDefault(const Type &value)
Definition: juce_CachedValue.h:158
juce::CachedValue::operator!=
bool operator!=(const OtherType &other) const
Definition: juce_CachedValue.h:126
juce::CachedValue::get
Type get() const noexcept
Definition: juce_CachedValue.h:106
juce::CachedValue::operator*
Type & operator*() noexcept
Definition: juce_CachedValue.h:109
juce::Identifier
Definition: juce_Identifier.h:39
juce::UndoManager
Definition: juce_UndoManager.h:53
juce::ValueTree::Listener
Definition: juce_ValueTree.h:481
juce::ValueTree
Definition: juce_ValueTree.h:73
juce::ValueTree::addListener
void addListener(Listener *listener)
Definition: juce_ValueTree.cpp:969
juce::Value
Definition: juce_Value.h:52
juce::VariantConverter
Definition: juce_Variant.h:351