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
27namespace juce
28{
29
30//==============================================================================
58template <typename Type>
60{
61public:
62 //==============================================================================
68
80 UndoManager* undoManager);
81
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
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
187private:
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//==============================================================================
208template <typename Type>
209inline CachedValue<Type>::CachedValue() = default;
210
211template <typename Type>
213 : targetTree (v), targetProperty (i), undoManager (um),
214 defaultValue(), cachedValue (getTypedValue())
215{
216 targetTree.addListener (this);
217}
218
219template <typename Type>
221 : targetTree (v), targetProperty (i), undoManager (um),
222 defaultValue (defaultToUse), cachedValue (getTypedValue())
223{
224 targetTree.addListener (this);
225}
226
227template <typename Type>
229{
230 return targetTree.getPropertyAsValue (targetProperty, undoManager);
231}
232
233template <typename Type>
235{
236 return ! targetTree.hasProperty (targetProperty);
237}
238
239template <typename Type>
241{
242 setValue (newValue, undoManager);
243 return *this;
244}
245
246template <typename Type>
247inline 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
256template <typename Type>
258{
259 resetToDefault (undoManager);
260}
261
262template <typename Type>
264{
265 targetTree.removeProperty (targetProperty, undoManagerToUse);
266 forceUpdateOfCachedValue();
267}
268
269template <typename Type>
271{
272 referToWithDefault (v, i, um, Type());
273}
274
275template <typename Type>
277{
278 referToWithDefault (v, i, um, defaultVal);
279}
280
281template <typename Type>
283{
284 cachedValue = getTypedValue();
285}
286
287template <typename Type>
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
299template <typename Type>
300inline 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
308template <typename Type>
309inline void CachedValue<Type>::valueTreePropertyChanged (ValueTree& changedTree, const Identifier& changedProperty)
310{
311 if (changedProperty == targetProperty && targetTree == changedTree)
312 forceUpdateOfCachedValue();
313}
314
315} // namespace juce
Array()=default
bool isUsingDefault() const
Type * operator->() noexcept
Type getDefault() const
ValueTree & getValueTree() noexcept
const Identifier & getPropertyID() const noexcept
CachedValue & operator=(const Type &newValue)
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
Type & operator*() noexcept
void addListener(Listener *listener)