2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #include "../core/juce_StandardHeader.h"
30 #include "juce_Value.h"
32 //==============================================================================
33 Value::ValueSource::ValueSource()
37 Value::ValueSource::~ValueSource()
41 void Value::ValueSource::sendChangeMessage (const bool synchronous
)
45 // (hold a local reference to this object in case it's freed during the callbacks)
46 const ReferenceCountedObjectPtr
<ValueSource
> localRef (this);
48 for (int i
= valuesWithListeners
.size(); --i
>= 0;)
50 Value
* const v
= valuesWithListeners
[i
];
62 void Value::ValueSource::handleAsyncUpdate()
64 sendChangeMessage (true);
67 //==============================================================================
68 class SimpleValueSource
: public Value::ValueSource
75 SimpleValueSource (const var
& initialValue
)
76 : value (initialValue
)
85 void setValue (const var
& newValue
)
87 if (! newValue
.equalsWithSameType (value
))
90 sendChangeMessage (false);
97 JUCE_DECLARE_NON_COPYABLE (SimpleValueSource
);
101 //==============================================================================
103 : value (new SimpleValueSource())
107 Value::Value (ValueSource
* const value_
)
110 jassert (value_
!= nullptr);
113 Value::Value (const var
& initialValue
)
114 : value (new SimpleValueSource (initialValue
))
118 Value::Value (const Value
& other
)
119 : value (other
.value
)
123 Value
& Value::operator= (const Value
& other
)
131 if (listeners
.size() > 0)
132 value
->valuesWithListeners
.removeValue (this);
135 //==============================================================================
136 var
Value::getValue() const
138 return value
->getValue();
141 Value::operator var() const
146 void Value::setValue (const var
& newValue
)
148 value
->setValue (newValue
);
151 String
Value::toString() const
153 return value
->getValue().toString();
156 Value
& Value::operator= (const var
& newValue
)
158 value
->setValue (newValue
);
162 void Value::referTo (const Value
& valueToReferTo
)
164 if (valueToReferTo
.value
!= value
)
166 if (listeners
.size() > 0)
168 value
->valuesWithListeners
.removeValue (this);
169 valueToReferTo
.value
->valuesWithListeners
.add (this);
172 value
= valueToReferTo
.value
;
177 bool Value::refersToSameSourceAs (const Value
& other
) const
179 return value
== other
.value
;
182 bool Value::operator== (const Value
& other
) const
184 return value
== other
.value
|| value
->getValue() == other
.getValue();
187 bool Value::operator!= (const Value
& other
) const
189 return value
!= other
.value
&& value
->getValue() != other
.getValue();
192 //==============================================================================
193 void Value::addListener (ValueListener
* const listener
)
195 if (listener
!= nullptr)
197 if (listeners
.size() == 0)
198 value
->valuesWithListeners
.add (this);
200 listeners
.add (listener
);
204 void Value::removeListener (ValueListener
* const listener
)
206 listeners
.remove (listener
);
208 if (listeners
.size() == 0)
209 value
->valuesWithListeners
.removeValue (this);
212 void Value::callListeners()
214 Value
v (*this); // (create a copy in case this gets deleted by a callback)
215 listeners
.call (&ValueListener::valueChanged
, v
);
218 OutputStream
& JUCE_CALLTYPE
operator<< (OutputStream
& stream
, const Value
& value
)
220 return stream
<< value
.toString();