JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
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
29Value::ValueSource::ValueSource()
30{
31}
32
33Value::ValueSource::~ValueSource()
34{
35 cancelPendingUpdate();
36}
37
38void Value::ValueSource::handleAsyncUpdate()
39{
40 sendChangeMessage (true);
41}
42
44{
45 const int numListeners = valuesWithListeners.size();
46
47 if (numListeners > 0)
48 {
49 if (synchronous)
50 {
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//==============================================================================
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:
94 var value;
95
97};
98
99
100//==============================================================================
102{
103}
104
105Value::Value (ValueSource* const v) : value (v)
106{
107 jassert (v != nullptr);
108}
109
111{
112}
113
114Value::Value (const Value& other) : value (other.value)
115{
116}
117
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
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
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
193{
194 return value == other.value;
195}
196
198{
199 return value == other.value || value->getValue() == other.getValue();
200}
201
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
237{
238 return stream << value.toString();
239}
240
241} // namespace juce
The base class for streams that write data to some kind of destination.
A smart-pointer class which points to a reference-counted object.
var getValue() const override
Returns the current value of this object.
void setValue(const var &newValue) override
Changes the current value.
The JUCE String class!
Definition juce_String.h:53
Receives callbacks when a Value object changes.
Definition juce_Value.h:139
Used internally by the Value class as the base class for its shared value objects.
Definition juce_Value.h:180
void sendChangeMessage(bool dispatchSynchronously)
Delivers a change message to all the listeners that are registered with this value.
Represents a shared variant value.
Definition juce_Value.h:51
bool operator==(const Value &other) const
Compares two values.
void setValue(const var &newValue)
Sets the current value.
void addListener(Listener *listener)
Adds a listener to receive callbacks when the value changes.
void removeListener(Listener *listener)
Removes a listener that was previously added with addListener().
bool refersToSameSourceAs(const Value &other) const
Returns true if this object and the other one use the same underlying ValueSource object.
Value & operator=(const var &newValue)
Sets the current value.
void referTo(const Value &valueToReferTo)
Makes this object refer to the same underlying ValueSource as another one.
~Value()
Destructor.
Value()
Creates an empty Value, containing a void var.
var getValue() const
Returns the current value.
bool operator!=(const Value &other) const
Compares two values.
String toString() const
Returns the value as a string.
A variant class, that can be used to hold a range of primitive values.
bool equalsWithSameType(const var &other) const noexcept
Returns true if this var has the same value and type as the one supplied.
#define jassert(expression)
Platform-independent assertion macro.
#define JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(className)
This is a shorthand way of writing both a JUCE_DECLARE_NON_COPYABLE and JUCE_LEAK_DETECTOR macro for ...
#define JUCE_CALLTYPE
This macro defines the C calling convention used as the standard for JUCE calls.
JUCE Namespace.
OutputStream &JUCE_CALLTYPE operator<<(OutputStream &stream, const BigInteger &value)
Writes a BigInteger to an OutputStream as a UTF8 decimal string.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88