OpenShot Audio Library | OpenShotAudio: juce_Value.cpp Source File

OpenShot Audio Library | OpenShotAudio 0.4.0

Loading...
Searching...
No Matches
juce_Value.cpp
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
29Value::ValueSource::ValueSource()
30{
31}
32
33Value::ValueSource::~ValueSource()
34{
35 cancelPendingUpdate();
36}
37
38void Value::ValueSource::handleAsyncUpdate()
39{
40 sendChangeMessage (true);
41}
42
43 void Value::ValueSource::sendChangeMessage (const bool synchronous)
44{
45 const int numListeners = valuesWithListeners.size();
46
47 if (numListeners > 0)
48 {
49 if (synchronous)
50 {
51 const ReferenceCountedObjectPtr<ValueSource> localRef (this);
52
53 cancelPendingUpdate();
54
55 for (int i = numListeners; --i >= 0;)
56 if (Value* const v = valuesWithListeners[i])
57 v->callListeners();
58 }
59 else
60 {
61 triggerAsyncUpdate();
62 }
63 }
64}
65
66//==============================================================================
67class SimpleValueSource final : public Value::ValueSource
68{
69public:
70 SimpleValueSource()
71 {
72 }
73
74 SimpleValueSource (const var& initialValue)
75 : value (initialValue)
76 {
77 }
78
79 var getValue() const override
80 {
81 return value;
82 }
83
84 void setValue (const var& newValue) override
85 {
86 if (! newValue.equalsWithSameType (value))
87 {
88 value = newValue;
89 sendChangeMessage (false);
90 }
91 }
92
93private:
94 var value;
95
96 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SimpleValueSource)
97};
98
99
100//==============================================================================
101Value::Value() : value (new SimpleValueSource())
102{
103}
104
105 Value::Value (ValueSource* const v) : value (v)
106{
107 jassert (v != nullptr);
108}
109
110 Value::Value (const var& initialValue) : value (new SimpleValueSource (initialValue))
111{
112}
113
114 Value::Value (const Value& other) : value (other.value)
115{
116}
117
118 Value::Value (Value&& other) noexcept
119{
120 // moving a Value with listeners will lose those listeners, which
121 // probably isn't what you wanted to happen!
122 jassert (other.listeners.size() == 0);
123
124 other.removeFromListenerList();
125 value = std::move (other.value);
126}
127
128 Value& Value::operator= (Value&& other) noexcept
129{
130 // moving a Value with listeners will lose those listeners, which
131 // probably isn't what you wanted to happen!
132 jassert (other.listeners.size() == 0);
133
134 other.removeFromListenerList();
135 value = std::move (other.value);
136 return *this;
137}
138
139Value::~Value()
140{
141 removeFromListenerList();
142}
143
144void Value::removeFromListenerList()
145{
146 if (listeners.size() > 0 && value != nullptr) // may be nullptr after a move operation
147 value->valuesWithListeners.removeValue (this);
148}
149
150//==============================================================================
151 var Value::getValue() const
152{
153 return value->getValue();
154}
155
156Value::operator var() const
157{
158 return value->getValue();
159}
160
161 void Value::setValue (const var& newValue)
162{
163 value->setValue (newValue);
164}
165
166 String Value::toString() const
167{
168 return value->getValue().toString();
169}
170
171 Value& Value::operator= (const var& newValue)
172{
173 value->setValue (newValue);
174 return *this;
175}
176
177 void Value::referTo (const Value& valueToReferTo)
178{
179 if (valueToReferTo.value != value)
180 {
181 if (listeners.size() > 0)
182 {
183 value->valuesWithListeners.removeValue (this);
184 valueToReferTo.value->valuesWithListeners.add (this);
185 }
186
187 value = valueToReferTo.value;
188 callListeners();
189 }
190}
191
192 bool Value::refersToSameSourceAs (const Value& other) const
193{
194 return value == other.value;
195}
196
197 bool Value::operator== (const Value& other) const
198{
199 return value == other.value || value->getValue() == other.getValue();
200}
201
202 bool Value::operator!= (const Value& other) const
203{
204 return value != other.value && value->getValue() != other.getValue();
205}
206
207//==============================================================================
208 void Value::addListener (Value::Listener* listener)
209{
210 if (listener != nullptr)
211 {
212 if (listeners.size() == 0)
213 value->valuesWithListeners.add (this);
214
215 listeners.add (listener);
216 }
217}
218
219 void Value::removeListener (Value::Listener* listener)
220{
221 listeners.remove (listener);
222
223 if (listeners.size() == 0)
224 value->valuesWithListeners.removeValue (this);
225}
226
227void Value::callListeners()
228{
229 if (listeners.size() > 0)
230 {
231 Value v (*this); // (create a copy in case this gets deleted by a callback)
232 listeners.call ([&] (Value::Listener& l) { l.valueChanged (v); });
233 }
234}
235
236OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const Value& value)
237{
238 return stream << value.toString();
239}
240
241} // namespace juce
juce::ReferenceCountedObjectPtr
Definition juce_ReferenceCountedObject.h:247
juce::String
Definition juce_String.h:53
juce::Value::Listener
Definition juce_Value.h:139
juce::Value::Listener::valueChanged
virtual void valueChanged(Value &value)=0
juce::Value::ValueSource
Definition juce_Value.h:180
juce::Value::ValueSource::sendChangeMessage
void sendChangeMessage(bool dispatchSynchronously)
Definition juce_Value.cpp:43
juce::Value
Definition juce_Value.h:51
juce::Value::operator==
bool operator==(const Value &other) const
Definition juce_Value.cpp:197
juce::Value::setValue
void setValue(const var &newValue)
Definition juce_Value.cpp:161
juce::Value::addListener
void addListener(Listener *listener)
Definition juce_Value.cpp:208
juce::Value::removeListener
void removeListener(Listener *listener)
Definition juce_Value.cpp:219
juce::Value::refersToSameSourceAs
bool refersToSameSourceAs(const Value &other) const
Definition juce_Value.cpp:192
juce::Value::operator=
Value & operator=(const var &newValue)
Definition juce_Value.cpp:171
juce::Value::referTo
void referTo(const Value &valueToReferTo)
Definition juce_Value.cpp:177
juce::Value::~Value
~Value()
Definition juce_Value.cpp:139
juce::Value::Value
Value()
Definition juce_Value.cpp:101
juce::Value::getValue
var getValue() const
Definition juce_Value.cpp:151
juce::Value::operator!=
bool operator!=(const Value &other) const
Definition juce_Value.cpp:202
juce::Value::toString
String toString() const
Definition juce_Value.cpp:166
juce::var
Definition juce_Variant.h:42