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
39{
40 sendChangeMessage (true);
41}
42
43void 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
54
55 for (int i = numListeners; --i >= 0;)
56 if (Value* const v = valuesWithListeners[i])
57 v->callListeners();
58 }
59 else
60 {
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
105Value::Value (ValueSource* const v) : value (v)
106{
107 jassert (v != nullptr);
108}
109
110Value::Value (const var& initialValue) : value (new SimpleValueSource (initialValue))
111{
112}
113
114Value::Value (const Value& other) : value (other.value)
115{
116}
117
118Value::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
128Value& 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
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//==============================================================================
152{
153 return value->getValue();
154}
155
156Value::operator var() const
157{
158 return value->getValue();
159}
160
161void Value::setValue (const var& newValue)
162{
163 value->setValue (newValue);
164}
165
167{
168 return value->getValue().toString();
169}
170
171Value& Value::operator= (const var& newValue)
172{
173 value->setValue (newValue);
174 return *this;
175}
176
177void 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
192bool Value::refersToSameSourceAs (const Value& other) const
193{
194 return value == other.value;
195}
196
197bool Value::operator== (const Value& other) const
198{
199 return value == other.value || value->getValue() == other.getValue();
200}
201
202bool Value::operator!= (const Value& other) const
203{
204 return value != other.value && value->getValue() != other.getValue();
205}
206
207//==============================================================================
209{
210 if (listener != nullptr)
211 {
212 if (listeners.size() == 0)
213 value->valuesWithListeners.add (this);
214
215 listeners.add (listener);
216 }
217}
218
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
virtual void handleAsyncUpdate()=0
void cancelPendingUpdate() noexcept
virtual void valueChanged(Value &value)=0
void sendChangeMessage(bool dispatchSynchronously)
bool operator==(const Value &other) const
void setValue(const var &newValue)
void addListener(Listener *listener)
void removeListener(Listener *listener)
bool refersToSameSourceAs(const Value &other) const
Value & operator=(const var &newValue)
void referTo(const Value &valueToReferTo)
var getValue() const
bool operator!=(const Value &other) const
String toString() const