LMMS
Loading...
Searching...
No Matches
juce_Value.cpp
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
32
37
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//==============================================================================
68{
69public:
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:
95
97};
98
99
100//==============================================================================
104
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
143
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;
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
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
237{
238 return stream << value.toString();
239}
240
241} // namespace juce
ostream & operator<<(ostream &out, const MidiEvent &ev)
Definition InMgr.cpp:9
void triggerAsyncUpdate()
Definition juce_AsyncUpdater.cpp:62
void cancelPendingUpdate() noexcept
Definition juce_AsyncUpdater.cpp:74
Definition juce_OutputStream.h:38
Definition juce_ReferenceCountedObject.h:247
Definition juce_Value.cpp:68
var getValue() const override
Definition juce_Value.cpp:79
void setValue(const var &newValue) override
Definition juce_Value.cpp:84
var value
Definition juce_Value.cpp:94
SimpleValueSource()
Definition juce_Value.cpp:70
SimpleValueSource(const var &initialValue)
Definition juce_Value.cpp:74
Definition juce_String.h:53
Definition juce_Value.h:139
Definition juce_Value.h:180
void handleAsyncUpdate() override
Definition juce_Value.cpp:38
SortedSet< Value * > valuesWithListeners
Definition juce_Value.h:204
void sendChangeMessage(bool dispatchSynchronously)
Definition juce_Value.cpp:43
ValueSource()
Definition juce_Value.cpp:29
friend class Value
Definition juce_Value.h:203
~ValueSource() override
Definition juce_Value.cpp:33
Definition juce_Value.h:51
ListenerList< Listener > listeners
Definition juce_Value.h:225
void setValue(const var &newValue)
Definition juce_Value.cpp:161
void removeFromListenerList()
Definition juce_Value.cpp:144
void addListener(Listener *listener)
Definition juce_Value.cpp:208
void removeListener(Listener *listener)
Definition juce_Value.cpp:219
bool refersToSameSourceAs(const Value &other) const
Definition juce_Value.cpp:192
void callListeners()
Definition juce_Value.cpp:227
ReferenceCountedObjectPtr< ValueSource > value
Definition juce_Value.h:224
void referTo(const Value &valueToReferTo)
Definition juce_Value.cpp:177
~Value()
Definition juce_Value.cpp:139
Value()
Definition juce_Value.cpp:101
var getValue() const
Definition juce_Value.cpp:151
String toString() const
Definition juce_Value.cpp:166
Definition juce_Variant.h:42
bool equalsWithSameType(const var &other) const noexcept
Definition juce_Variant.cpp:639
int * l
Definition inflate.c:1579
unsigned v[N_MAX]
Definition inflate.c:1584
register unsigned i
Definition inflate.c:1575
static PuglViewHint int value
Definition pugl.h:1708
#define jassert(expression)
#define JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(className)
#define JUCE_CALLTYPE
Definition carla_juce.cpp:31