OpenShot Audio Library | OpenShotAudio: juce_ValueWithDefault.h Source File

OpenShot Audio Library | OpenShotAudio 0.3.0

Loading...
Searching...
No Matches
juce_ValueWithDefault.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//==============================================================================
39 class ValueWithDefault
40{
41public:
42 //==============================================================================
44 ValueWithDefault() = default;
45
47 ValueWithDefault (ValueTree& tree, const Identifier& propertyID, UndoManager* um)
48 : targetTree (tree),
49 targetProperty (propertyID),
50 undoManager (um),
51 defaultValue()
52 {
53 }
54
56 ValueWithDefault (ValueTree& tree, const Identifier& propertyID, UndoManager* um,
57 const var& defaultToUse)
58 : targetTree (tree),
59 targetProperty (propertyID),
60 undoManager (um),
61 defaultValue (defaultToUse)
62 {
63 }
64
71 ValueWithDefault (ValueTree& tree, const Identifier& propertyID, UndoManager* um,
72 const var& defaultToUse, StringRef arrayDelimiter)
73 : targetTree (tree),
74 targetProperty (propertyID),
75 undoManager (um),
76 defaultValue (defaultToUse),
77 delimiter (arrayDelimiter)
78 {
79 }
80
82 ValueWithDefault (const ValueWithDefault& other)
83 : targetTree (other.targetTree),
84 targetProperty (other.targetProperty),
85 undoManager (other.undoManager),
86 defaultValue (other.defaultValue),
87 delimiter (other.delimiter)
88 {
89 }
90
91 //==============================================================================
95 var get() const noexcept
96 {
97 if (isUsingDefault())
98 return defaultValue;
99
100 if (delimiter.isNotEmpty())
101 return delimitedStringToVarArray (targetTree[targetProperty].toString());
102
103 return targetTree[targetProperty];
104 }
105
107 Value getPropertyAsValue() { return targetTree.getPropertyAsValue (targetProperty, undoManager); }
108
110 var getDefault() const { return defaultValue; }
111
113 void setDefault (const var& newDefault)
114 {
115 if (defaultValue != newDefault)
116 {
117 defaultValue = newDefault;
118
119 if (onDefaultChange != nullptr)
120 onDefaultChange();
121 }
122 }
123
125 bool isUsingDefault() const
126 {
127 return ! targetTree.hasProperty (targetProperty);
128 }
129
131 void resetToDefault() noexcept
132 {
133 targetTree.removeProperty (targetProperty, nullptr);
134 }
135
137 std::function<void()> onDefaultChange;
138
139 //==============================================================================
141 ValueWithDefault& operator= (const var& newValue)
142 {
143 setValue (newValue, undoManager);
144 return *this;
145 }
146
148 void setValue (const var& newValue, UndoManager* undoManagerToUse)
149 {
150 if (auto* array = newValue.getArray())
151 targetTree.setProperty (targetProperty, varArrayToDelimitedString (*array), undoManagerToUse);
152 else
153 targetTree.setProperty (targetProperty, newValue, undoManagerToUse);
154 }
155
156 //==============================================================================
158 void referTo (ValueTree& tree, const Identifier& property, UndoManager* um)
159 {
160 referToWithDefault (tree, property, um, var(), {});
161 }
162
166 void referTo (ValueTree& tree, const Identifier& property, UndoManager* um, const var& defaultVal)
167 {
168 referToWithDefault (tree, property, um, defaultVal, {});
169 }
170
171 void referTo (ValueTree& tree, const Identifier& property, UndoManager* um,
172 const var& defaultVal, StringRef arrayDelimiter)
173 {
174 referToWithDefault (tree, property, um, defaultVal, arrayDelimiter);
175 }
176
177 //==============================================================================
179 ValueTree& getValueTree() noexcept { return targetTree; }
180
182 Identifier& getPropertyID() noexcept { return targetProperty; }
183
185 UndoManager* getUndoManager() noexcept { return undoManager; }
186
187 //==============================================================================
188 ValueWithDefault& operator= (const ValueWithDefault& other)
189 {
190 referToWithDefault (other.targetTree, other.targetProperty, other.undoManager,
191 other.defaultValue, other.delimiter);
192
193 return *this;
194 }
195
196private:
197 //==============================================================================
198 ValueTree targetTree;
199 Identifier targetProperty;
200 UndoManager* undoManager = nullptr;
201 var defaultValue;
202
203 String delimiter;
204
205 //==============================================================================
206 void referToWithDefault (const ValueTree& v, const Identifier& i, UndoManager* um,
207 const var& defaultVal, StringRef del)
208 {
209 targetTree = v;
210 targetProperty = i;
211 undoManager = um;
212 defaultValue = defaultVal;
213 delimiter = del;
214 }
215
216 //==============================================================================
217 String varArrayToDelimitedString (const Array<var>& input) const noexcept
218 {
219 // if you are trying to control a var that is an array then you need to
220 // set a delimiter string that will be used when writing to XML!
221 jassert (delimiter.isNotEmpty());
222
223 StringArray elements;
224
225 for (auto& v : input)
226 elements.add (v.toString());
227
228 return elements.joinIntoString (delimiter);
229 }
230
231 Array<var> delimitedStringToVarArray (StringRef input) const noexcept
232 {
233 Array<var> arr;
234
235 for (auto t : StringArray::fromTokens (input, delimiter, {}))
236 arr.add (t);
237
238 return arr;
239 }
240
241 //==============================================================================
242 JUCE_DECLARE_WEAK_REFERENCEABLE (ValueWithDefault)
243};
244
245} // namespace juce
juce::Identifier
Definition: juce_Identifier.h:39
juce::StringArray::fromTokens
static StringArray fromTokens(StringRef stringToTokenise, bool preserveQuotedStrings)
Definition: juce_StringArray.cpp:386
juce::StringRef
Definition: juce_StringRef.h:62
juce::String::isNotEmpty
bool isNotEmpty() const noexcept
Definition: juce_String.h:302
juce::UndoManager
Definition: juce_UndoManager.h:53
juce::ValueTree
Definition: juce_ValueTree.h:73
juce::ValueTree::getPropertyAsValue
Value getPropertyAsValue(const Identifier &name, UndoManager *undoManager, bool shouldUpdateSynchronously=false)
Definition: juce_ValueTree.cpp:846
juce::ValueTree::setProperty
ValueTree & setProperty(const Identifier &name, const var &newValue, UndoManager *undoManager)
Definition: juce_ValueTree.cpp:758
juce::ValueTree::removeProperty
void removeProperty(const Identifier &name, UndoManager *undoManager)
Definition: juce_ValueTree.cpp:780
juce::ValueTree::hasProperty
bool hasProperty(const Identifier &name) const noexcept
Definition: juce_ValueTree.cpp:775
juce::ValueWithDefault
Definition: juce_ValueWithDefault.h:40
juce::ValueWithDefault::getDefault
var getDefault() const
Definition: juce_ValueWithDefault.h:110
juce::ValueWithDefault::resetToDefault
void resetToDefault() noexcept
Definition: juce_ValueWithDefault.h:131
juce::ValueWithDefault::get
var get() const noexcept
Definition: juce_ValueWithDefault.h:95
juce::ValueWithDefault::ValueWithDefault
ValueWithDefault(ValueTree &tree, const Identifier &propertyID, UndoManager *um, const var &defaultToUse)
Definition: juce_ValueWithDefault.h:56
juce::ValueWithDefault::getPropertyAsValue
Value getPropertyAsValue()
Definition: juce_ValueWithDefault.h:107
juce::ValueWithDefault::getPropertyID
Identifier & getPropertyID() noexcept
Definition: juce_ValueWithDefault.h:182
juce::ValueWithDefault::ValueWithDefault
ValueWithDefault(const ValueWithDefault &other)
Definition: juce_ValueWithDefault.h:82
juce::ValueWithDefault::operator=
ValueWithDefault & operator=(const var &newValue)
Definition: juce_ValueWithDefault.h:141
juce::ValueWithDefault::getValueTree
ValueTree & getValueTree() noexcept
Definition: juce_ValueWithDefault.h:179
juce::ValueWithDefault::isUsingDefault
bool isUsingDefault() const
Definition: juce_ValueWithDefault.h:125
juce::ValueWithDefault::referTo
void referTo(ValueTree &tree, const Identifier &property, UndoManager *um)
Definition: juce_ValueWithDefault.h:158
juce::ValueWithDefault::setValue
void setValue(const var &newValue, UndoManager *undoManagerToUse)
Definition: juce_ValueWithDefault.h:148
juce::ValueWithDefault::setDefault
void setDefault(const var &newDefault)
Definition: juce_ValueWithDefault.h:113
juce::ValueWithDefault::onDefaultChange
std::function< void()> onDefaultChange
Definition: juce_ValueWithDefault.h:137
juce::ValueWithDefault::referTo
void referTo(ValueTree &tree, const Identifier &property, UndoManager *um, const var &defaultVal)
Definition: juce_ValueWithDefault.h:166
juce::ValueWithDefault::ValueWithDefault
ValueWithDefault(ValueTree &tree, const Identifier &propertyID, UndoManager *um, const var &defaultToUse, StringRef arrayDelimiter)
Definition: juce_ValueWithDefault.h:71
juce::ValueWithDefault::getUndoManager
UndoManager * getUndoManager() noexcept
Definition: juce_ValueWithDefault.h:185
juce::ValueWithDefault::ValueWithDefault
ValueWithDefault()=default
juce::ValueWithDefault::ValueWithDefault
ValueWithDefault(ValueTree &tree, const Identifier &propertyID, UndoManager *um)
Definition: juce_ValueWithDefault.h:47
juce::Value
Definition: juce_Value.h:52
juce::var
Definition: juce_Variant.h:42
juce::var::getArray
Array< var > * getArray() const noexcept
Definition: juce_Variant.cpp:507