OpenShot Library | OpenShotAudio  0.2.2
juce_Value.cpp
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 
27 namespace juce
28 {
29 
30 Value::ValueSource::ValueSource()
31 {
32 }
33 
34 Value::ValueSource::~ValueSource()
35 {
37 }
38 
39 void Value::ValueSource::handleAsyncUpdate()
40 {
41  sendChangeMessage (true);
42 }
43 
44 void Value::ValueSource::sendChangeMessage (const bool synchronous)
45 {
46  const int numListeners = valuesWithListeners.size();
47 
48  if (numListeners > 0)
49  {
50  if (synchronous)
51  {
52  const ReferenceCountedObjectPtr<ValueSource> localRef (this);
53 
55 
56  for (int i = numListeners; --i >= 0;)
57  if (Value* const v = valuesWithListeners[i])
58  v->callListeners();
59  }
60  else
61  {
63  }
64  }
65 }
66 
67 //==============================================================================
69 {
70 public:
72  {
73  }
74 
75  SimpleValueSource (const var& initialValue)
76  : value (initialValue)
77  {
78  }
79 
80  var getValue() const override
81  {
82  return value;
83  }
84 
85  void setValue (const var& newValue) override
86  {
87  if (! newValue.equalsWithSameType (value))
88  {
89  value = newValue;
90  sendChangeMessage (false);
91  }
92  }
93 
94 private:
95  var value;
96 
97  JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SimpleValueSource)
98 };
99 
100 
101 //==============================================================================
103 {
104 }
105 
106 Value::Value (ValueSource* const v) : value (v)
107 {
108  jassert (v != nullptr);
109 }
110 
111 Value::Value (const var& initialValue) : value (new SimpleValueSource (initialValue))
112 {
113 }
114 
115 Value::Value (const Value& other) : value (other.value)
116 {
117 }
118 
119 Value::Value (Value&& other) noexcept
120 {
121  // moving a Value with listeners will lose those listeners, which
122  // probably isn't what you wanted to happen!
123  jassert (other.listeners.size() == 0);
124 
125  other.removeFromListenerList();
126  value = std::move (other.value);
127 }
128 
129 Value& Value::operator= (Value&& other) noexcept
130 {
131  // moving a Value with listeners will lose those listeners, which
132  // probably isn't what you wanted to happen!
133  jassert (other.listeners.size() == 0);
134 
135  other.removeFromListenerList();
136  value = std::move (other.value);
137  return *this;
138 }
139 
141 {
142  removeFromListenerList();
143 }
144 
145 void Value::removeFromListenerList()
146 {
147  if (listeners.size() > 0 && value != nullptr) // may be nullptr after a move operation
148  value->valuesWithListeners.removeValue (this);
149 }
150 
151 //==============================================================================
153 {
154  return value->getValue();
155 }
156 
157 Value::operator var() const
158 {
159  return value->getValue();
160 }
161 
162 void Value::setValue (const var& newValue)
163 {
164  value->setValue (newValue);
165 }
166 
168 {
169  return value->getValue().toString();
170 }
171 
172 Value& Value::operator= (const var& newValue)
173 {
174  value->setValue (newValue);
175  return *this;
176 }
177 
178 void Value::referTo (const Value& valueToReferTo)
179 {
180  if (valueToReferTo.value != value)
181  {
182  if (listeners.size() > 0)
183  {
184  value->valuesWithListeners.removeValue (this);
185  valueToReferTo.value->valuesWithListeners.add (this);
186  }
187 
188  value = valueToReferTo.value;
189  callListeners();
190  }
191 }
192 
193 bool Value::refersToSameSourceAs (const Value& other) const
194 {
195  return value == other.value;
196 }
197 
198 bool Value::operator== (const Value& other) const
199 {
200  return value == other.value || value->getValue() == other.getValue();
201 }
202 
203 bool Value::operator!= (const Value& other) const
204 {
205  return value != other.value && value->getValue() != other.getValue();
206 }
207 
208 //==============================================================================
210 {
211  if (listener != nullptr)
212  {
213  if (listeners.size() == 0)
214  value->valuesWithListeners.add (this);
215 
216  listeners.add (listener);
217  }
218 }
219 
221 {
222  listeners.remove (listener);
223 
224  if (listeners.size() == 0)
225  value->valuesWithListeners.removeValue (this);
226 }
227 
228 void Value::callListeners()
229 {
230  if (listeners.size() > 0)
231  {
232  Value v (*this); // (create a copy in case this gets deleted by a callback)
233  listeners.call ([&] (Value::Listener& l) { l.valueChanged (v); });
234  }
235 }
236 
237 OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const Value& value)
238 {
239  return stream << value.toString();
240 }
241 
242 } // namespace juce
bool operator!=(const Value &other) const
Compares two values.
Definition: juce_Value.cpp:203
String toString() const
Returns the value as a string.
Definition: juce_Value.cpp:167
A variant class, that can be used to hold a range of primitive values.
Definition: juce_Variant.h:45
void sendChangeMessage(bool dispatchSynchronously)
Delivers a change message to all the listeners that are registered with this value.
Definition: juce_Value.cpp:44
void triggerAsyncUpdate()
Causes the callback to be triggered at a later time.
var getValue() const
Returns the current value.
Definition: juce_Value.cpp:152
The JUCE String class!
Definition: juce_String.h:42
void cancelPendingUpdate() noexcept
This will stop any pending updates from happening.
Receives callbacks when a Value object changes.
Definition: juce_Value.h:142
void setValue(const var &newValue) override
Changes the current value.
Definition: juce_Value.cpp:85
bool operator==(const Value &other) const
Compares two values.
Definition: juce_Value.cpp:198
var getValue() const override
Returns the current value of this object.
Definition: juce_Value.cpp:80
Used internally by the Value class as the base class for its shared value objects.
Definition: juce_Value.h:182
Represents a shared variant value.
Definition: juce_Value.h:55
The base class for streams that write data to some kind of destination.
bool equalsWithSameType(const var &other) const noexcept
Returns true if this var has the same value and type as the one supplied.
A smart-pointer class which points to a reference-counted object.
void referTo(const Value &valueToReferTo)
Makes this object refer to the same underlying ValueSource as another one.
Definition: juce_Value.cpp:178
Value & operator=(const var &newValue)
Sets the current value.
Definition: juce_Value.cpp:172
~Value()
Destructor.
Definition: juce_Value.cpp:140
Value()
Creates an empty Value, containing a void var.
Definition: juce_Value.cpp:102
void setValue(const var &newValue)
Sets the current value.
Definition: juce_Value.cpp:162
virtual void valueChanged(Value &value)=0
Called when a Value object is changed.
void addListener(Listener *listener)
Adds a listener to receive callbacks when the value changes.
Definition: juce_Value.cpp:209
bool refersToSameSourceAs(const Value &other) const
Returns true if this value and the other one are references to the same value.
Definition: juce_Value.cpp:193
void removeListener(Listener *listener)
Removes a listener that was previously added with addListener().
Definition: juce_Value.cpp:220