LMMS
Loading...
Searching...
No Matches
juce_CachedValue.h
Go to the documentation of this file.
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,
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 Type& operator*() noexcept { return cachedValue; }
109
113 Type* operator->() noexcept { return &cachedValue; }
114
118 template <typename OtherType>
119 bool operator== (const OtherType& other) const { return cachedValue == other; }
120
124 template <typename OtherType>
125 bool operator!= (const OtherType& other) const { return cachedValue != other; }
126
127 //==============================================================================
129 Value getPropertyAsValue();
130
134 bool isUsingDefault() const;
135
137 Type getDefault() const { return defaultValue; }
138
139 //==============================================================================
141 CachedValue& operator= (const Type& newValue);
142
144 void setValue (const Type& newValue, UndoManager* undoManagerToUse);
145
149 void resetToDefault();
150
154 void resetToDefault (UndoManager* undoManagerToUse);
155
157 void setDefault (const Type& value) { defaultValue = value; }
158
159 //==============================================================================
161 void referTo (ValueTree& tree, const Identifier& property, UndoManager* um);
162
166 void referTo (ValueTree& tree, const Identifier& property, UndoManager* um, const Type& defaultVal);
167
174 void forceUpdateOfCachedValue();
175
176 //==============================================================================
179
182
185
186private:
187 //==============================================================================
193
194 //==============================================================================
195 void referToWithDefault (ValueTree&, const Identifier&, UndoManager*, const Type&);
196 Type getTypedValue() const;
197
198 void valueTreePropertyChanged (ValueTree& changedTree, const Identifier& changedProperty) override;
199
200 //==============================================================================
203};
204
205
206//==============================================================================
207template <typename Type>
208inline CachedValue<Type>::CachedValue() = default;
209
210template <typename Type>
214{
215 targetTree.addListener (this);
216}
217
218template <typename Type>
219inline CachedValue<Type>::CachedValue (ValueTree& v, const Identifier& i, UndoManager* um, const Type& defaultToUse)
221 defaultValue (defaultToUse), cachedValue (getTypedValue())
222{
223 targetTree.addListener (this);
224}
225
226template <typename Type>
228{
229 return targetTree.getPropertyAsValue (targetProperty, undoManager);
230}
231
232template <typename Type>
234{
235 return ! targetTree.hasProperty (targetProperty);
236}
237
238template <typename Type>
240{
241 setValue (newValue, undoManager);
242 return *this;
243}
244
245template <typename Type>
246inline void CachedValue<Type>::setValue (const Type& newValue, UndoManager* undoManagerToUse)
247{
248 if (cachedValue != newValue || isUsingDefault())
249 {
250 cachedValue = newValue;
251 targetTree.setProperty (targetProperty, VariantConverter<Type>::toVar (newValue), undoManagerToUse);
252 }
253}
254
255template <typename Type>
260
261template <typename Type>
262inline void CachedValue<Type>::resetToDefault (UndoManager* undoManagerToUse)
263{
264 targetTree.removeProperty (targetProperty, undoManagerToUse);
266}
267
268template <typename Type>
270{
271 referToWithDefault (v, i, um, Type());
272}
273
274template <typename Type>
275inline void CachedValue<Type>::referTo (ValueTree& v, const Identifier& i, UndoManager* um, const Type& defaultVal)
276{
277 referToWithDefault (v, i, um, defaultVal);
278}
279
280template <typename Type>
285
286template <typename Type>
287inline void CachedValue<Type>::referToWithDefault (ValueTree& v, const Identifier& i, UndoManager* um, const Type& defaultVal)
288{
289 targetTree.removeListener (this);
290 targetTree = v;
292 undoManager = um;
293 defaultValue = defaultVal;
295 targetTree.addListener (this);
296}
297
298template <typename Type>
300{
301 if (const var* property = targetTree.getPropertyPointer (targetProperty))
302 return VariantConverter<Type>::fromVar (*property);
303
304 return defaultValue;
305}
306
307template <typename Type>
308inline void CachedValue<Type>::valueTreePropertyChanged (ValueTree& changedTree, const Identifier& changedProperty)
309{
310 if (changedProperty == targetProperty && targetTree == changedTree)
312}
313
314} // namespace juce
#define noexcept
Definition DistrhoDefines.h:72
Definition juce_CachedValue.h:59
bool isUsingDefault() const
Definition juce_CachedValue.h:233
Type * operator->() noexcept
Definition juce_CachedValue.h:113
Type getDefault() const
Definition juce_CachedValue.h:137
ValueTree & getValueTree() noexcept
Definition juce_CachedValue.h:178
const Identifier & getPropertyID() const noexcept
Definition juce_CachedValue.h:181
ValueTree targetTree
Definition juce_CachedValue.h:188
void forceUpdateOfCachedValue()
Definition juce_CachedValue.h:281
Type cachedValue
Definition juce_CachedValue.h:192
void setValue(const Type &newValue, UndoManager *undoManagerToUse)
Definition juce_CachedValue.h:246
UndoManager * getUndoManager() noexcept
Definition juce_CachedValue.h:184
void referToWithDefault(ValueTree &, const Identifier &, UndoManager *, const Type &)
Definition juce_CachedValue.h:287
void resetToDefault()
Definition juce_CachedValue.h:256
void referTo(ValueTree &tree, const Identifier &property, UndoManager *um)
Definition juce_CachedValue.h:269
Value getPropertyAsValue()
Definition juce_CachedValue.h:227
void setDefault(const Type &value)
Definition juce_CachedValue.h:157
Type defaultValue
Definition juce_CachedValue.h:191
void valueTreePropertyChanged(ValueTree &changedTree, const Identifier &changedProperty) override
Definition juce_CachedValue.h:308
Type getTypedValue() const
Definition juce_CachedValue.h:299
Type get() const noexcept
Definition juce_CachedValue.h:105
UndoManager * undoManager
Definition juce_CachedValue.h:190
Type & operator*() noexcept
Definition juce_CachedValue.h:108
Identifier targetProperty
Definition juce_CachedValue.h:189
Definition juce_Identifier.h:39
Definition juce_UndoManager.h:52
Definition juce_Value.h:51
Definition juce_ValueTree.h:479
Definition juce_ValueTree.h:72
Definition juce_Variant.h:42
unsigned v[N_MAX]
Definition inflate.c:1584
register unsigned i
Definition inflate.c:1575
static PuglViewHint int value
Definition pugl.h:1708
#define JUCE_DECLARE_NON_COPYABLE(className)
#define JUCE_DECLARE_WEAK_REFERENCEABLE(Class)
Definition juce_WeakReference.h:234
Definition carla_juce.cpp:31
static var toVar(const Type &t)
Definition juce_Variant.h:353
static Type fromVar(const var &v)
Definition juce_Variant.h:352
#define const
Definition zconf.h:137